Create a python GUI using tkinter

It is a memo of how to use tkinter

Show panel

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()

Set the size of the panel

python


root = tk.Tk()
root.geometry("500x320")
root.mainloop()

Set panel title

python


root = tk.Tk()
root.geometry("250x150")
root.title('Abya')
root.mainloop()

Put the parts

String label

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, ....

button

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()

Responds to button operations

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 event that occurs when it is clicked once.

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()

image.png

Change the display in response to button operations

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()

image.png

Specify the font

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()

Placement of parts

There are three placement methods

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 position and width with grid (row, column, columnspan)

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()

Show / Hide / Exit

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

Create a python GUI using tkinter
GUI creation in python using tkinter 2
About building GUI using TKinter of Python
Create a simple GUI app in Python
Create a GUI app with Python's Tkinter
[Python] Create a Batch environment using AWS-CDK
Create a Python module
Create a Python environment
Create a frame with transparent background with tkinter [Python]
Create a GIF file using Pillow in Python
You can easily create a GUI with Python
Create a web map using Python and GDAL
Create a Mac app using py2app and Python3! !!
Create a MIDI file in Python using pretty_midi
Create a GUI on the terminal using curses
Create a Wox plugin (Python)
Create a function in Python
Create JIRA tickets using Python
Create a python numpy array
Create a directory with python
Create a data collection bot in Python using Selenium
Create a native GUI app with Py2app and Tkinter
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[Python] Create a ValueObject with a complete constructor using dataclasses
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a company name extractor with python using JCLdic
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
GUI programming in Python using Appjar
[Python] Create a file & folder path specification screen with tkinter
I made a Line-bot using Python!
Create a DI Container in Python
Let's make a GUI with python.
Drawing a silverstone curve using python
Create a Python environment on Mac (2017/4)
Create a nested dictionary using defaultdict
Create a virtual environment with Python!
Create a binary file in Python
Create a python environment on centos
Creating a GUI as easily as possible with python [tkinter edition]
Create a Python general-purpose decorator framework
[Python GUI] DICOM contrast adjustment and BMP conversion using Tkinter
Create a Kubernetes Operator in Python
I tried to make a stopwatch using tkinter in python
5 Ways to Create a Python Chatbot
Create a CRUD API using FastAPI
Create a random string in Python
Easily create homemade RPA using Python
Create a C wrapper using Boost.Python
Create a simple scheduled batch using Docker's Python Image and parse-crontab
[Ev3dev] Create a program that captures the LCD (screen) using python
Create a command line tool to convert dollars to yen using Python
Create a Python function decorator with Class
Periodic execution processing when using tkinter [Python3]
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Create a python environment on your Mac
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda