Here's a classic way to classify Tkinter, a Python GUI tool.
This is an introductory article on how to create an Image Viewer using this template. (Addition) Create Image Viewer with Tkinter
import tkinter as tk
from tkinter import ttk
class Application(tk.Frame):
def __init__(self,master):
super().__init__(master)
self.pack()
self.master.geometry("300x300")
self.master.title("Tkinter with Class Template")
self.create_widgets()
def create_widgets(self):
pass
def callBack(self):
pass
def main():
root = tk.Tk()
app = Application(master=root)#Inherit
app.mainloop()
if __name__ == "__main__":
main()
Create a GUI that performs the following operations when the button is pressed.
import tkinter as tk
from tkinter import ttk
#Generate root instance
root = tk.Tk()
root.geometry("300x300")
root.title("Tkinter without Class")
#Define Button Click Event Function
def say_Hello():
print("Hello World") # on python console
label_hello.configure(text="I Have benn Clicked!")
print(name.get())
label_name.configure(text = name.get())
#Define Button Widget
button_hello = ttk.Button(master=root)
button_hello.configure(text="Hello World")
button_hello.configure(command = say_Hello)
button_hello.pack()
#Define Label Widget
label_hello = ttk.Label(master=root)
label_hello.configure(text = 'A Label')
label_hello.pack()
#Define Enntry Widget
name= tk.StringVar()
entry_name = ttk.Entry(master=root)
entry_name.configure(textvariable = name)
entry_name.pack()
#Define Label Widget2
label_name = ttk.Label(master=root)
label_name.configure(text = 'Please input something in Entry')
label_name.pack()
# Start GUI
root.mainloop()
Here, we will change the procedural programming code in the previous chapter to a template for classification. Be careful with self.
import tkinter as tk
from tkinter import ttk
class Application(tk.Frame):
def __init__(self,master):
super().__init__(master)
self.pack()
self.master.geometry("300x300")
self.master.title("Tkinter with Class")
self.create_widgets()
# Create Widgets function
def create_widgets(self):
#Button
self.button_hello = ttk.Button(self)
self.button_hello.configure(text="Hello World")
self.button_hello.configure(command = self.say_Hello) #do not forget to add self!
self.button_hello.pack()
#Label
self.label_hello = ttk.Label(self)
self.label_hello.configure(text='A Label')
self.label_hello.pack()
#Entry
self.name = tk.StringVar()
self.entry_name = ttk.Entry(self)
self.entry_name.configure(textvariable = self.name)
self.entry_name.pack()
#Label2
self.label_name=ttk.Label(self)
self.label_name.configure(text = 'Please input something in Entry')
self.label_name.pack()
# Event Callback Function
def say_Hello(self):
print("Hello, World") # on python console
self.label_hello.configure(text="I Have benn Clicked!")
print(self.name.get())
self.label_name.configure(text=self.name.get())
def main():
root = tk.Tk()
app = Application(master=root)#Inherit class inheritance!
app.mainloop()
if __name__ == "__main__":
main()
I've found that using classes makes the Python code more structured and easier to read. With this, we will continue to expand. I felt like I was finally able to see the world of Python.
Recommended Posts