It is a memo of how to use tkinter
If you create an object and do mainloop (), it will enter the standby state while displaying it.
python
import tkinter as tk
root = tk.Tk()
root.mainloop()
python
root = tk.Tk()
root.geometry("500x320")
root.mainloop()
python
root = tk.Tk()
root.geometry("250x150")
root.title('Abya')
root.mainloop()
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='label')
label.pack()
root.mainloop()
The font is specified as family, size, style1, style2, ....
The following is just a button.
python
root = tk.Tk()
root.geometry("250x150")
button = tk.Button(root, text="Button", width=20, height=3)
button.pack()
root.mainloop()
You can set the function to be called when the specified event occurs by executing the bind () method of the tkinter object. The first argument of bind () is the type of event, and the second argument is the function to call. In the following, func () is called in the
python
def func(event):
print('Press the button')
root = tk.Tk()
root.geometry("250x150")
button = tk.Button(root, text="Button", width=20, height=3)
button.bind('<Button-1>', func)
button.place(x=40, y=125)
root.mainloop()
Since the argument of the called side can only receive the event passed by tkinter, when handling a variable inside the function, make it a global variable or put it in class and receive the class property.
python
class TkinterClass:
def __init__(self):
self.count = 0
self.root = tk.Tk()
self.root.geometry("250x160")
label = tk.Label(text='Count the number of presses')
label.pack(pady=10)
self.button_text = tk.StringVar()
self.button_text.set(self.count)
button = tk.Button(self.root, textvariable=self.button_text, font=('', 32),
width=8, height=1, bg='#999999', activebackground="#aaaaaa")
button.bind('<ButtonPress>', self.func1)
button.pack(pady=10)
self.root.mainloop()
def func1(self, event):
self.count += 1
self.button_text.set(self.count)
if __name__ == '__main__':
tkc = TkinterClass()
Specify in the order of font family, font size, style1, style2,…. If there are multiple styles, write the third and subsequent styles.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='label', font=('arial', 20, 'italic', 'overstrike'))
label.pack()
root.mainloop()
The font family can be omitted.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='label', font=('', 20))
label.pack()
root.mainloop()
pack (): put in order grid (): Arrange in a grid place (): Place by specifying coordinates
pack()
If you pack () with no arguments, they will be centered and arranged in order from the top.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.pack()
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.pack()
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.pack()
root.mainloop()
#### Left justified with pack (anchor = tk.W)
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.pack(anchor=tk.W)
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.pack(anchor=tk.W)
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.pack(anchor=tk.W)
root.mainloop()
#### Align both with pack (fill = tk.X)
Fill it to the full width with fill = tk.X.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.pack(fill=tk.X)
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.pack(fill=tk.X)
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.pack(fill=tk.X)
root.mainloop()
#### Pad with pack (padx = 10, pady = 10)
You can specify the vertical and horizontal gaps in pixels with pady and padx.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.pack(fill=tk.X, padx=10, pady=10)
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.pack(fill=tk.X, padx=10, pady=10)
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.pack(fill=tk.X, padx=10, pady=10)
root.mainloop()
grid()
If nothing is specified, it will be added from the upper left to the bottom.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.grid()
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.grid()
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.grid()
root.mainloop()
Specify the position with row and column, and specify the width with rowspan and columnspan.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.grid(row=0, column=0, columnspan=2, padx=10, pady=10, sticky=tk.W+tk.E)
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.grid(row=1, column=0, padx=10, pady=10)
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.grid(row=1, column=1, padx=10, pady=10)
root.mainloop()
place()
Place by specifying the coordinates.
python
root = tk.Tk()
root.geometry("250x150")
label = tk.Label(text='Search 1', font=("", 12), bg='#33ccaa', relief=tk.RIDGE, bd=2)
label.place(x=10, y=10)
label = tk.Label(text='Search 2', font=("", 12), bg='#dd88aa', relief=tk.RIDGE, bd=2)
label.place(x=85, y=40)
label = tk.Label(text='Search 3', font=("", 12), bg='#bbdd44', relief=tk.RIDGE, bd=2)
label.place(x=25, y=90)
root.mainloop()
You can temporarily hide it with withdraw ().
python
root.withdraw()
You can redisplay it with deiconify ().
python
root.deiconify()
destroy () will close the tkinter window and exit root.loopmain ().
python
root.destroy()
Recommended Posts