Python Tkinter Primer Note

background

--Visual Studio (VB.NET), Android Studio (Java), XCode (Swift) are easy to develop / develop GUI with standard GUI designer (Desktop App is migrated from VB.NET to Java, C # is Unity I only use it) --In Java, I built a GUI with AWT / Swing without using a designer (Layout) ――Since the main language has changed from Java to Python, I've been making only CUI tools / Web, so I want to make a Desktop GUI as well.

It is not high performance (there are inconveniences in terms of transparency and standard functions), but since it is standard, I will summarize it for myself as a memorandum.

When I try to exit with \ # Ctrl + C, it's annoying that it doesn't respond until I touch the Window.

-tkinter --- Python interface for Tcl / Tk — Python 3.8.1 documentation

Creating a Window

import tkinter as tk

root = tk.Tk() # Window
root.title('Hello Window') # Window title
root.geometry('%dx%d' % (400, 400)) # Window size (width, height)

#Initialize and place the widget here

root.mainloop()

Block with root.mainloop (). Exit the Main loop when the Window is closed.

Main loop

To be precise, Tk seems to manage Tkinter's main loop rather than Window. You can create the second and third windows with tk.Toplevel when making a multi-window to use modal etc. (Form = VB.NET, Frame = Java equivalent). In the case of Java, I didn't care because I think that the thread that executes the loop for GUI stands up (it stays alive even if the Main Thread that executes the main function ends), but in the case of Tkinter it is explicit. It seems to be managed in a targeted manner.

If you don't want Tk itself to exist as a Root window (using tk.Toplevel), you can remove it with root.withdraw (). However, the logic of "Window closed = End Main loop = End program" will not work (reminiscent of Swing's DefaultCloseOperation), so root.destroy (reminds me of Swing's DefaultCloseOperation) when the application ends. ) Call.

Creating an application with the root window hidden

Display a window with tk.Toplevel so that the main loop ends when a specific window is closed.

import tkinter as tk

root = tk.Tk()
root.withdraw()

window = tk.Toplevel(root)
window.title('Hello Window')
window.geometry('%dx%d' % (400, 400))
window.protocol('WM_DELETE_WINDOW', root.destroy)

#Initialize and place the widget here

root.mainloop()

The closing of window can be detected bywindow.protocol ('WM_DELETE_WINDOW', handler_func)(corresponding to WindowListener \ #windowClosing of Swing).

window.protocol('WM_DELETE_WINDOW', root.destroy)

After all, in an application like a single Window + modal, it would be simpler to treat Tk as the main Window obediently.

Placement of Frame

frame = tk.Frame(root)
# frame = tk.Frame(root, bg='#FF00FF')
# frame['bg'] = '#FF00FF'
frame.place(x=8, y=8, width=200, height=200)

Panel(VB.NET/Java)/UIView(iOS)/ViewGroup(Android)的なやつ。Widgetのコンストラクタにbgとしてカラーコードを渡すと背景色を設定できる。初期化後に背景色を設定したい場合はディスクリプタ経由(widget['bg'])で設定できる。

-python tkinter color setting method --memopy -The one who controls the descriptor controls Python --Qiita

Label Label(VB.NET/Java)/UILabel(iOS)/TextView(Android)的なやつ。

label = tk.Label(root, text='Hello')
# label['text'] = 'Another text'
label.place(x=8, y=8)

-Python GUI tool tkinter cheat sheet --Qiita

Button and event handler

def onHelloClicked(event):
    print(event) # <ButtonPress event num=BUTTON_NUM x=MOUSE_X y=MOUSE_Y>
    # print(event.x, event.y, event.num)

button = tk.Button(root, text='Hello')
# button = tk.Button(root, text='Hello', width=10)
# button['text'] = 'Another text'

button.bind('<Button-1>', onHelloClicked)
# button.bind('<Button-2>', onHelloClicked)
# button.bind('<Button-3>', onHelloClicked)
# button.bind('<Button>', onHelloClicked)

button.place(x=8, y=8)
# button.place(x=8, y=8, width=60)
# button.place(x=8, y=8, width=60, height=31)

You can pass width to the constructor of Widget, but the unit is not pixel and it is difficult to understand (number of characters?).

Event handlers can be registered with bind. <Button-*> represents a mouse button click (confusing, but not related to the widget's tk.Button). Left-click on <Button-1>, middle-click on <Button-2>, and right-click on <Button-3>. The button number is entered in ʻevent.num (ʻevent.num = 1 when left-clicking, ʻevent.num = 3when right-clicking. It is confusing, but not the number of clicks). You can also get all mouse button clicks by specifying