Let’s look at that extremely simple tkinter GUI application that we used in the previous lesson titled Welcome to tkinter. Since the program was so small, I will simply include it again for convenience.
# From file tk_application.py import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) master.title('Marisa\'s Application') self.grid() self.create_widgits() def create_widgits(self): self.quit_button = tk.Button(self, text='Quit', command=root.destroy) self.quit_button.grid(padx=10, pady=10) root = tk.Tk() app = Application(master=root) app.mainloop()
Now, let’s break it down line by line and see what we have.
The first actual command is nothing more than the standard python import command to give us access to the module.
import tkinter as tk
I prefer to import the tkinter module as tk so that it is easier to reference the attributes.
Next, let’s look at the main class that we are using for our application:
If you are at least passingly acquainted with object-oriented programming, this shouldn’t look overly strange. We use the ‘class’ keyword to indicate that we are creating a class. We follow the ‘class’ keyword with the name of the class: in this case, ‘Application’. Inside the parentheses is the class parent class from which we inherit. That means that our ‘Application’ class now has access to all the attributes that are contained in the tk.Frame class.
Now we need to make the function that will be run when our ‘Application’ class is created. The code:
def __init__(self, master=None): super().__init__(master) master.title('Marisa\'s Application') self.grid() self.create_widgits()
The above method will call be every time that we create a new instance of the ‘Application’ class. Each line does the following:
- The method name.
- Call the parent (or super) class. In this case, it is tkinter.Frame, also known as tk.Frame since we imported tkinter as tk.
- Give the window a title.
- Use the grid layout manager for the window.
- call a different method to actually create the widgets that will be put inside the window.
Now let’s look at the method that will be used to place the widgets inside the window.
def create_widgits(self): self.quit_button = tk.Button(self, text='Quit', command=root.destroy) self.quit_button.grid(padx=10, pady=10)
Let’s look at this method line by line as well.
- The name of the method.
- create a single button with the text of ‘Quit’ and the command to destroy the root, which, in this case, is the entire window.
- put the quit button in the grid that we are using for our window.
There is only one more thing that makes this program complete:
root = tk.Tk() app = Application(master=root) app.mainloop()
These three lines of code actually create and call the classes necessary to create our program. Let’s look at them:
- create an instance of tk.Tk() and store it in the object variable named root.
- create an instance of our class called Application and send it the variable that we just created from tk.Tk().
- tell python to run the main loop for the application. Note that the mainloop() method was inherited during the creation of the Application class.
And that’s it. That is all it takes to create a simple window framework with tkinter.