We will take the following program as an example:
from tkinter import * root = Tk() btn = Button(root, text="Click me!") btn.config(command=lambda: print("Hello, Tkinter!")) btn.pack(padx=120, pady=30) root.title("My Tkinter app") root.mainloop()
It creates a main window with a button that prints Hello, Tkinter! in the console each time it is clicked. The button is placed with a padding of 120px in the horizontal axis and 30px in the vertical axis. The last statement starts the main loop, which processes user events and updates the GUI until the main window is destroyed:
You can execute the program and verify that it is working as expected. However, all our variables are defined in the global namespace, and the more widgets you add, the more difficult it becomes to reason about the parts where they are used.
These maintainability issues can be addressed with basic OOP techniques, which are considered good practice in all types of Python programs.