Create Python Tkinter GUIs using classes – Intermediate Object-Oriented Programming Tutorial #7

Posted by


In this tutorial, we will learn how to build Python Tkinter GUIs with classes. We will be using the object-oriented programming (OOP) paradigm to create a more organized and modular GUI application. This tutorial is intermediate level, so we will assume you have a basic understanding of Python and Tkinter.

Let’s get started by creating a simple GUI application using classes in Python.

Step 1: Setting up the Environment
First, make sure you have Python installed on your computer. You can download Python from the official website (https://www.python.org/downloads/) and install it according to your operating system.

Next, we need to install the Tkinter library. Tkinter comes pre-installed with Python, so you don’t need to install it separately.

Step 2: Creating the GUI Class
Open your favorite code editor or Python IDE and create a new Python file. Let’s name it gui_app.py.

import tkinter as tk

class GUIApp(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("GUI Application")
        self.geometry("400x300")

        self.label = tk.Label(self, text="Hello, World!")
        self.label.pack()

if __name__ == "__main__":
    app = GUIApp()
    app.mainloop()

In this code snippet, we created a class GUIApp that inherits from tk.Tk, the main Tkinter window class. Inside the class, we defined the __init__ constructor method where we set the window title, size, and created a label widget displaying "Hello, World!". Finally, we created an instance of the GUIApp class and started the main event loop using app.mainloop().

Step 3: Running the GUI Application
Save the gui_app.py file and run it from your command line or Python IDE. You should see a simple GUI window displaying the text "Hello, World!".

Step 4: Adding Widgets and Event Handling
Now let’s add more widgets and event handling to our GUI application.

import tkinter as tk

class GUIApp(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("GUI Application")
        self.geometry("400x300")

        self.label = tk.Label(self, text="Hello, World!")
        self.label.pack()

        self.button = tk.Button(self, text="Click Me", command=self.on_click)
        self.button.pack()

    def on_click(self):
        self.label.config(text="Button Clicked!")

if __name__ == "__main__":
    app = GUIApp()
    app.mainloop()

In this updated code, we added a Button widget with the text "Click Me" and associated it with the on_click method, which changes the label text to "Button Clicked!" when the button is clicked.

Step 5: Creating Modular GUI Classes
To make our code more modular and maintainable, we can create separate classes for different sections of our GUI application. Let’s create a MenuFrame class that contains a menu bar with options.

class MenuFrame(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.menu_bar = tk.Menu(self)

        file_menu = tk.Menu(self.menu_bar, tearoff=0)
        file_menu.add_command(label="Open")
        file_menu.add_command(label="Save")
        file_menu.add_separator()
        file_menu.add_command(label="Exit", command=self.quit_window)

        self.menu_bar.add_cascade(label="File", menu=file_menu)

        master.config(menu=self.menu_bar)

    def quit_window(self):
        self.master.destroy()

In this code snippet, we created a MenuFrame class that inherits from tk.Frame. Inside the constructor method, we defined a menu bar with "File" options like Open, Save, and Exit. We also associated the Exit option with the quit_window method, which closes the application window.

Step 6: Integrating the Menu Frame
Now let’s integrate the MenuFrame class into our main GUIApp class.

class GUIApp(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("GUI Application")
        self.geometry("400x300")

        self.menu_frame = MenuFrame(self)
        self.menu_frame.pack(side="top", fill="x")

        self.label = tk.Label(self, text="Hello, World!")
        self.label.pack()

        self.button = tk.Button(self, text="Click Me", command=self.on_click)
        self.button.pack()

    def on_click(self):
        self.label.config(text="Button Clicked!")

if __name__ == "__main__":
    app = GUIApp()
    app.mainloop()

In this final code snippet, we added an instance of the MenuFrame class to our GUIApp class and packed it at the top of the window. Now our GUI application has a menu bar with options to Open, Save, and Exit.

Congratulations! You have successfully built a Python Tkinter GUI application using classes and object-oriented programming techniques. Feel free to explore and add more widgets and functionality to your GUI application. OOP programming allows you to create more complex and structured GUIs with ease.