Python has recently been active in fields such as AI and analysis, but it can also be used to create ordinary desktop applications.
This time, we will use the GUI library "Tkinter" that comes standard with Python to create a simple GUI application that writes the entered values to the terminal.
The configuration I have confirmed to work is as follows
The IDE uses Visual Studio Code.
Please go to the official website to download and install.
Click "New File" in Visual Studio Code, name it "form.py" and save it.
Write a declaration at the beginning of the program that tkinter will be used.
from tkinter import *
from tkinter import ttk
Next, define the main form as follows.
mainform = Tk()
mainform.title('GUI app testing')
This mainform.title ('test GUI app') will be the title of the form.
Next, place the widget.
In tkinter, labels, text boxes, etc. are called widgets, and the operation contents etc. can be defined for each.
This time, we will create a frame, a label, a text input box, and a button.
Enter as follows.
frame1 = ttk.Frame(mainform, padding=16)
label1 = ttk.Label(frame1, text='Please enter the text.')
t = StringVar()
entry1 = ttk.Entry(frame1, textvariable=t)
button1 = ttk.Button(
frame1,
text='OK',
command=lambda: print('The input value is, %s.' % t.get()))
--ttk.Frame sets the frame --ttkLabel sets the label --ttk.Entry is the text box setting --ttk.Button is the button setting.
The button can also be set to call a module defined in another location.
Place the created widget.
This time, I will simply arrange them in a horizontal row.
frame1.pack()
label1.pack(side=LEFT)
entry1.pack(side=LEFT)
button1.pack(side=LEFT)
They are arranged side by side from the left.
At this point, all you have to do is write the window display command.
mainform.mainloop()
Execute the following command from the terminal of Visual Studio Code
python form.py
If the following GUI is displayed, it is successful.
Enter your favorite words to try
It is OK if the following is displayed on the terminal
The input value is,Autumn taste.
As a GUI application, the number of lines is small and it can be easily created.
I also like the fact that I can make VB-like (logic is tied to the form).
How about rewriting the system currently made with VB or Excel to Python?
Python works on both Linux and Mac, so it's very convenient.
Recommended Posts