【Python】GUI用のTkinterのメモTips集 pygubuかも【リンク集】

スポンサーリンク
広告

PythonでちょっとGUI作りたい

ArduinoやESP32とPCの通信とか、Raspberry Piなどで、ちょっとした画面入出力GUIを作りたい時に。。何がいいかな?と。。Qtとかもあるんですが、ほんとちょっとした時に使うようには、ちょっとめんどくさいの。。 で。。 標準ライブラリとして入っているtkinterについてのメモ

ちなみに、Python用のGUIってこんなにたくさんのモジュールやツール類があります。

GuiProgramming - Python Wiki

tkinter

Python 3.9のtkinterのDoccument

tkinter — Python interface to Tcl/Tk — Python 3.9.16 documentation
Graphical User Interfaces with Tk — Python 3.9.17 documentation

昔のVBにかなり近い感じ。。簡単に表示するだけならこれで十分

tkとttkで、ウィジェットが違ったりするので、覚えるの大変かもだけど。。使う時しか覚えてないし(笑)

CTCのこの記事がわりと良さそうです。

2021年4月
第19回 tkinterを使って画面を作る…前にmainloopの役割と使えるウィジェットを確認してみた  

から。。

2022年3月
第30回 tkinterのcanvasに複数の円を縦横移動、リストとディクショナリの合わせ技で記述と管理を楽に  

どんな画面できるかを知るのに。。これ全部読むの大変なので。。

GUIを作るためのGUI(笑)

昔のVBにもついてましたよね。こういうの。。

GitHub - alejandroautalan/pygubu: A simple GUI builder for the python tkinter module
A simple GUI builder for the python tkinter module - GitHub - alejandroautalan/pygubu: A simple GUI builder for the python tkinter module
GitHub - alejandroautalan/pygubu-designer: A simple GUI designer for the python tkinter module
A simple GUI designer for the python tkinter module - GitHub - alejandroautalan/pygubu-designer: A simple GUI designer for the python tkinter module

どちらも、condaからはインストールできないので、Anacondaで一つenvironmentをコピーして作っておいて、その環境の中から、pipでインストールしました。

使い方は、こちら

Pygubu hello world program

ディレクトリがどこかで、起動させるところ違うけど。。僕の場合は、

C:\Users\ユーザー名\anaconda3\envs\Anaconda3pygubu\Scripts\pygubu-designer.exe

でした。

pygubu-designer の exampleを見る といいですね。

Filepathを見る画面作る例  pathchooserdemo.ui を読み込んで

Previewのメニューから Preview in Top level F5 をやって 動かした状況を見て、三角ボタンを押した時の様子。 ファイルセレクトができる画面がでてきますよね。

これを Code タブ で application か code か widgetかを選んで、Generateボタンを押すと、コードかできます。

これを、コピペすれば作れます。。

少なくとも。。できた気になります(笑)

TIPS集

リンク

Entries in Python-tkinter category - Python Code Notes
Information by python engineer
Graphical User Interfaces with Tk — Python 3.9.17 documentation

Comboboxに、セレクトされる値を入れる

comport_now はList形式 で 文字の場合

comport_now =[“COM1″,”COM2″,”COM3”] とかの場合 Class Newproject でcombobox_compが定義されているとした場合

app = NewprojectApp()
app.combobox_comp.values= tk.StringVar()
app.combobox_comp["values"]=comport_now

app.combobox_comp.set(”値”)

では、1個しか入れられないみたい。そういう使い方みたいです。

COMポート・Serial 通信は、こちらがわかりやすい

Python §05: pyserialでシリアル通信する方法を解説【初心者向け】
pythonでシリアル通信をするには、pyserialライブラリーを使うのが早道。ポートの自動サーチも簡単にできてしまいます。この記事では、ポート番号の設定、送信、受信の方法を書いています。

Event type ボタンを押したり、Comboboxを選んだり

import tkinter as tk
print(tk.EventType.__members__)

とすると、出てきます。

Comboboxとか、ttkにはまた特別なバーチャルイベントがあるのでそちらは、元のドキュメントを参照すると書いてあります。

tkinter.ttk — Tk themed widgets
Source code: Lib/tkinter/ttk.py The tkinter.ttk module provides access to the Tk themed widget set, introduced in Tk 8.5. It provides additional benefits includ...

PyserialとTkinterのサンプル

https://github.com/tom2rd/PythonTest/blob/main/tkinterpyserialtest.py

コメントを残していただけるとありがたいです

コメント

  1. seonghunchoe より:

    pygubu いいですね。私もよく使っています。Tkinterに関しては現時点でこれがBestかと思います。

Loading Facebook Comments ...
%d人のブロガーが「いいね」をつけました。