--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
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.
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.
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.
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
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
-Try using Python's Tkinter-Qiita
# initialize button
# place button
root.update()
print(button.winfo_width(), button.winfo_height())
# root.mainloop()
In my environment, when I passed width to Button in the constructor, winfo_width was 36+ (width-1) * 8
. The default winfo_width (0 characters) was 28 (pixel). If neither width is set, winfo_width will change according to the given character string. winfo_height is 31 (pixel) when no line break is inserted, and when line break is inserted, the line break is reflected in the display, and winfo_height is automatically expanded.
from PIL import Image, ImageTk
# initialize root
image = Image.new('RGB', (128, 128), color=(255, 0, 255)) #Appropriate image
image_tk = ImageTk.PhotoImage(image)
image_view = tk.Label(root, image=image_tk)
image_view.place(x=8, y=8)
# root.mainloop()
-Python GUI tool tkinter cheat sheet --Qiita
entry = tk.Entry(root, width=10)
entry.insert(tk.END, 'Hello') #initial value
print(entry.get()) # 'Hello'
entry.delete(0, tk.END) #clear
print(entry.get()) # ''
entry.place(x=8, y=8)
The entered value can be obtained with ʻentry.get ()`.
-Try using Python's Tkinter-Qiita
text = tk.Text(root, width=30, height=10)
text.insert(tk.END, 'Hello') #initial value
print(text.get('1.0', tk.END)) # 'Hello'
text.delete('1.0', tk.END) #clear
print(text.get('1.0', tk.END)) # ''
text.place(x=8, y=8)
Even if the number of lines exceeded height, the scroll bar did not appear automatically. Also, Ctrl + A did not work (move to the beginning of the line?), And although the cut and paste shortcut and clipboard could be used, the behavior when selecting + pasting (Ctrl + V) was strange (the selected text was not overwritten). Remain). Simple with no context menu.
-Python GUI tool tkinter cheat sheet --Qiita
from tkinter.scrolledtext import ScrolledText
text = ScrolledText(root, width=30, height=10)
#The rest is the same
-Various things you tend to forget about tkinter, part 4
def onKeyPressed(event):
print(event)
# <KeyPress event keysym=a keycode=38 char='a' x=205 y=132>
# <KeyPress event keysym=Shift_L keycode=50 x=442 y=272>
# <KeyPress event state=Shift keysym=A keycode=38 char='A' x=127 y=183>
# print(event.keycode, event.char, event.state)
#state is a number (Modifier)
#Default 0
#Shift 1 (0b0001), Ctrl 4 (0b0100), Alt 8 (0b1000)
# Shift+Alt 9 (0b1001), Win+Shift is 65 (0b01000001).
root.bind('<Key>', onKeyPressed)
-About bind and event (Perl / Tk-Basics)
You can pour in by using pack and grid for Widget placement.
-Introduction to Tkinter: 2. Place Widget
widget.pack_forget()
2020/02/02 Addendum: Even if I applied pack_forget to the one placed in place, it did not disappear well. It looks good to use destroy
.
widget.destroy()
-[Python] Try using Tkinter's canvas --Qiita
root.attributes('-fullscreen', True)
# root.update()
# print(root.winfo_width(), root.winfo_height())
Close with Alt + F4.
-How to display in full screen with Tkinter --Qiita
root.wm_attributes('-topmost', 1)
root.bind('<Escape>', lambda e: root.destroy())
# root.bind('<Key-Escape>', lambda e: root.destroy())
root.bind('<Control-w>', lambda e: root.destroy())
-python tkinter font (font) setting method --memopy
Combobox -Creating a drop-down list [Making a household account book with python tkinter sqlite3] --memorpy
There seems to be FileChooser, ColorPicker, MessageBox, etc.
-[Python GUI tool tkinter cheat sheet --Qiita](https://qiita.com/nanako_ut/items/b5393363b9e21d6342ea#%E3%83%95%E3%82%A1%E3%82%A4%E3%83% AB% E9% 81% B8% E6% 8A% 9E) -Various things you tend to forget about tkinter, part 4
Recommended Posts