[PYTHON] Make GUI apps super easy with tkinter

Preface

The demand for Python has increased significantly these days, probably because of the growing popularity of Python. Python has a strong image of technologies related to artificial intelligence such as machine learning, but you can also create GUI apps. A library just starting out with Python? module? This article is for intermediate users, so it may not be enough for intermediate users, but I hope you will read it.

File preparation

tkinter-gui/ ├ app.py ├ face.png

Create app.py as above. I named the folder tkinter-gui, but anything is fine. For face.png, save the following image and place it in the same location as app.py.

image.png face.png

Write code

app.py


#Loading Tkinter module
import tkinter

#Window generation
root = tkinter.Tk()
root.attributes("-topmost", True)
root.minsize(width=200, height=200)

#Generate Frame widget
frame = tkinter.Frame(root, width=300, height=300, bg="black")
frame.propagate(False)
frame.pack()

#Generate Label Widget for Text
label= tkinter.Label(frame, text="Hello! How are you?", fg="white", bg="black", font=("", 16))
label.pack()

#Generate Label widget for images
import os
png = tkinter.PhotoImage(file=os.path.dirname(__file__)+"/face.png ")
image = tkinter.Label(frame, image=png, bg="black")
image.pack()

#Generate Entry widget
entry = tkinter.Entry(frame, width=20, bg="gray", fg="white")
entry.insert(0, "happy")
entry.pack()

#Click event function
def show_text():
    new_label = tkinter.Label(frame, text=entry.get(), fg="white", bg="black", font=("", 32))
    new_label.pack()
    entry.destroy()
    button.destroy()

#Generate Button widget
button = tkinter.Button(frame, text="Say", bg="gray", fg="yellow", command=show_text)
button.pack()

#Show the window where the widget is placed
root.mainloop()

Write the above code in app.py. After that, when you run app.py, the following window will appear.

image.png

The lizard is listening to your mood. After typing in the text box, press the Say button to answer.

image.png

I was able to answer happy.

Finally

All you need for your application

  1. The program receives input from the user
  2. The program does something with the input
  3. The program returns output to the user

This is the flow.

This time, I implemented only the functions as a really minimum application. If you make various modifications based on the code above, you can also create a calculator application. Understand what each line of code prints on the screen and create your own application.

The content of this article is also explained in detail on the site below. Please see if you like.

[Introduction to Tkinter] Let's make a GUI application with Python!

Recommended Posts

Make GUI apps super easy with tkinter
Easy GUI app with Tkinter Text
Easy to make with syntax
Let's make dice with tkinter
[Super easy] Let's make a LINE BOT with Python.
I tried to make GUI tic-tac-toe with Python and Tkinter
Let's make a GUI with python.
[Python] Super easy test with assert statement
Create a GUI app with Python's Tkinter
Develop Windows apps with Python 3 + Tkinter (Preparation)
Create a GUI executable file created with tkinter
GUI image cropping tool made with Python + Tkinter
I tried super easy linear separation with Chainer
MVC with Tkinter
Develop Windows apps with Python 3 + Tkinter (exe file)
Upload images to S3 with GUI using tkinter
I tried to make Kana's handwriting recognition Part 3/3 Cooperation with GUI using Tkinter
Make your Python environment "easy" with VS Code
Let's make a Mac app with Tkinter and py2app
Make an application using tkinter an executable file with cx_freeze
Make Lambda Layers with Lambda
Easy Grad-CAM with pytorch-gradcam
Make Yubaba with Discord.py
Become Santa with Tkinter
Super Easy VPN WireGuard
[Tkinter] Improve GUI responsiveness
Easy debugging with ipdb
Make slides with iPython
Easy TopView with OpenCV
Make clear about super ()
Creating a GUI as easily as possible with python [tkinter edition]