Questions and answers for a basic tkinter GUI

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:

class Application(tk.Frame):

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:

  1. The method name.
  2. Call the parent (or super) class. In this case, it is tkinter.Frame, also known as tk.Frame since we imported tkinter as tk.
  3. Give the window a title.
  4. Use the grid layout manager for the window.
  5. 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.

  1. The name of the method.
  2. create a single button with the text of ‘Quit’ and the command to destroy the root, which, in this case, is the entire window.
  3. 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:

  1. create an instance of tk.Tk() and store it in the object variable named root.
  2. create an instance of our class called Application and send it the variable that we just created from tk.Tk().
  3. 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.

Advertisements

Published by

Marisa

I am a writer of words, a thinker of thoughts, a changer of genders, and a queerer of life. I am an antagonist of the ordinary; and while I do tolerate it, I also look at it with contempt.

2 thoughts on “Questions and answers for a basic tkinter GUI”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s