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.
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.
face.png
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.
The lizard is listening to your mood. After typing in the text box, press the Say button to answer.
I was able to answer happy.
All you need for your application
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