Create a simple GUI app in Python

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.

What to use

The configuration I have confirmed to work is as follows

The IDE uses Visual Studio Code.

Python installation

Please go to the official website to download and install.

Use Tkinter for the time being

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.

Widget placement

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.

Layout settings

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.

Window display

At this point, all you have to do is write the window display command.

mainform.mainloop()

Try to run

Execute the following command from the terminal of Visual Studio Code

python form.py

If the following GUI is displayed, it is successful.

gui001.jpg

Enter your favorite words to try

gui002.jpg

It is OK if the following is displayed on the terminal

The input value is,Autumn taste.

Impressions

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

Create a simple GUI app in Python
Create a function in Python
Create a dictionary in Python
Create a simple momentum investment model in Python
Create a python GUI using tkinter
Create a DI Container in Python
Create a binary file in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Run a simple algorithm in Python
Create a random string in Python
A simple HTTP client implemented in Python
Try drawing a simple animation in Python
Create a JSON object mapper in Python
Create a GUI app with Python's Tkinter
Create a simple web app with flask
Create a Python-GUI app in Docker (PySimpleGUI)
Write a simple greedy algorithm in Python
Launch a Flask app in Python Anywhere
[GPS] Create a kml file in Python
Write a simple Vim Plugin in Python 3
Simple gRPC in Python
Create SpatiaLite in Python
[GUI in Python] PyQt5-Dialog-
Create a Python environment
Set up a simple HTTPS server in Python 3
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
I want to create a window in Python
Create a standard normal distribution graph in Python
How to create a JSON file in Python
Create a virtual environment with conda in Python
You can easily create a GUI with Python
A simple Pub / Sub program note in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Set up a simple SMTP server in Python
Create a package containing global commands in Python
Create a Mac app using py2app and Python3! !!
Until you create a new app in Django
Create a MIDI file in Python using pretty_midi
Create a loop antenna pattern in Python in KiCad
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Take a screenshot in Python
Create a data collection bot in Python using Selenium
Create a Wox plugin (Python)
Write a super simple molecular dynamics program in python
Create a native GUI app with Py2app and Tkinter
[LINE Messaging API] Create a rich menu in Python
Create a (simple) REST server
Create gif video in Python
Make a simple Slackbot with interactive button in python
Create a plugin to run Python Doctest in Vim (2)
[GUI in Python] PyQt5 -Widget-
Create a plugin to run Python Doctest in Vim (1)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Make a bookmarklet in Python
Create a python numpy array
Simple regression analysis in Python
Create a simple textlint server
Introducing GUI: PyQt5 in Python