Tkinter and Object Oriented Programming

Posted by


Object Oriented Programming (OOP) is a programming paradigm centered around objects and classes. Tkinter is a popular Python library for creating GUI applications. By combining OOP with Tkinter, you can create more complex and organized GUI applications.

In this tutorial, we will cover the basics of Object Oriented Tkinter and walk through an example of building a simple GUI application using OOP concepts.

Step 1: Import Tkinter

Before we start creating our GUI application, we need to import the Tkinter library. You can do this by adding the following line at the beginning of your Python script:

import tkinter as tk

Step 2: Create a Class for the GUI Application

To create a GUI application using OOP, we will define a class that represents our application. This class will have attributes and methods that define the behavior and appearance of the GUI. Here’s a basic template for our GUI application class:

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

        self.title("My Tkinter Application")

        # Add widgets and other GUI elements here

In this class, we have defined a constructor __init__ method that initializes the main application window (tk.Tk) and sets the title of the window. You can add more widgets, buttons, labels, etc. inside the __init__ method.

Step 3: Add Widgets to the GUI

To add widgets to the GUI, you can create them as attributes of the class and configure them inside the __init__ method. Here’s an example of adding a label and a button to the GUI:

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

        self.title("My Tkinter Application")

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

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

    def on_button_click(self):
        self.label.config(text="Button clicked!")

In this example, we have added a tk.Label widget to display a text message and a tk.Button widget that will change the label text when clicked. The on_button_click method is called when the button is clicked and changes the text of the label.

Step 4: Run the Application

To run the GUI application, you need to create an instance of the MyApp class and call the mainloop method. Here’s how you can do it:

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

Now you can run your script and see the GUI application in action. You should see a window with a label displaying "Hello, World!" and a button that changes the label text when clicked.

This is just a basic example of using Object Oriented Tkinter to create a GUI application. You can customize and expand your application by adding more widgets, implementing event handlers, and organizing your code using classes and methods.

I hope this tutorial helps you understand the basics of Object Oriented Tkinter and inspires you to create your own GUI applications using OOP principles. Happy coding!

0 0 votes
Article Rating

Leave a Reply

23 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sovereignlivingsoul
1 day ago

good video, exactly what i was looking for, i am going to try and implement what you demonstrated in the coding of the program i am currently building, i would have appreciated it more if you had included how to input the data instead of hard coding it in, i have a solution for that, but i am positive yours would have been much cleaner, thanks again, i am getting a positional argument error

@charlo193
1 day ago

thank you , keep going !

@andromilk2634
1 day ago

Is there a reason you passed a master to tk.Toplevel? (genuine question of a beginner) I saw in the options that it was optionnal, so couldn't you just have passed the username as the only argument without needing the master? Thanks

@rickyusuf8696
1 day ago

Hi Jobin, I subscribed to your channel because of a previous video that I watched so I decided to check your other videos out, and to my surprise, this video resolved the very issue that I was having! Thank you so much! You are a great teacher.

@2002budokan
1 day ago

Why do you prefer class inheritence instead of composition, is there any reason that I don't know yet? I ask this question, because all component based web applications construct their components via composition further I know how difficult to maintain inheritence based approach from ancient MFC applications. But all OO tkinter courses prefer class inheritence, maybe there is a good reason. Consider how difficult to switch from Tkinter to CustomTkinter if you prefer class inheritence.

@marcelus4614
1 day ago

Excellent video, thanks man!!

@hardkorebhaktaofbob
1 day ago

Bro you are the Bob Ross of Python Instruction!!!! Thanks! So so much!!!

@kingwill03
1 day ago

Good video Jobin. Thank you.

@fredflintstone8048
1 day ago

Very good job of explaining instantiating classes and passing in variables.

@alanbrody3223
1 day ago

I'd like to have some more info about the frame you create in each class, why do you do that? Thanks, you have some very good content.

@JENSLIAMVISTA
1 day ago

SHEEEEESHHHHHH the most effective teacherrrrr! SANA GANYAN RIN SI ROMAN HAHAHAHAHHAHA

@dgh25
1 day ago

Why pass username as an argument? You could just go self.username = master.username…

@johnlorefice976
1 day ago

I watched a few other videos from different people to clear up some of my confusion, and none of them broke it down and explained why I'm doing certain things. This video was very direct and explained what the other 5 videos failed to do. Great job and presentation!

@onlyeyeno
1 day ago

Clear and concise, and well presented. Thanks for taking the time and effort of sharing 🙂
Best regards.

@cursos.prometec
1 day ago

Hi Jobin, nice channel, itsd really useful for begginers like me. I am trying to make tkinter program reading an MQTT calback function to get values. Could you help explaining the basics for linking an external callback to tkinter normal loop event ?
Thanks for all

@olebaky9182
1 day ago

learned a lot here. thanks

@carlospitcher4335
1 day ago

This series of videos is clearly underrated.
But in just a couple of days you passed from 460 to 570 subs… I hope you hit 1000 by dec 15th
God Speed

@robinsonzapata1
1 day ago

Great tutorial! many thanks

@jaypower17
1 day ago

I love the way you teach ! very nice

@mhl1740
1 day ago

Thats the best Channel for Infos to TkInter I found on YouTube! You are the only one who shows how to organize code in a professional way. Thank you!

23
0
Would love your thoughts, please comment.x
()
x