Johan von Adden
Johan von Adden

Reputation: 81

What is the difference between an object, and a tkinter object assigned to it?

I'm trying to understand a program using an OOP approach, but that doesn't seem to be going to well.

I'm able to follow the code below (that I found online) quite a bit, but I have some question about the relationship between root and my_gui (found below the class definition).

import tkinter as tk

class MyFirstGUI:

    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = tk.Label(master, text="This is our first GUI!")
        self.label.pack()

        self.greet_button = tk.Button(master, text="Greet", command=self.greet)
        self.greet_button.pack()

    def greet(self):
        print("Greetings!")

root = tk.Tk()
my_gui = MyFirstGUI(root)
root.mainloop()

I know that the label and button objects in the init method each go into the master object (which is the root object), but what confuses me is differentiating my_gui and root after the init method has been executed.

  1. After the init method of the class executes, does my_gui and root become one and the same thing, or is root now a subset of my_gui?
  2. If they are essentially the same being after the init function executes, then there is no need for the my_gui object, is there?
  3. What exactly is self.master = master doing?

I guess what I really want to know is the relationship between my_gui and root.

Upvotes: 1

Views: 181

Answers (2)

Nae
Nae

Reputation: 15345

"What is the difference between an object, and a tkinter object assigned to it?"

I assume by that you mean the part:

root = tk.Tk()
my_gui = MyFirstGUI(root)

Here root isn't exactly assigned to an object but rather is passed as an argument for an object. Then that argument is assigned as an attribute to that object. Which does mean that root now is a subset of my_gui as far as the python hierarchy is concerned.

my_gui object makes use of root object in the way that it, first of all, assigns it as an attribute, self.master, and then it creates widgets as children to it.

"If they are essentially the same being after the init function executes, then there is no need for the my_gui object, is there?"

They are not the same, they are the instances of unique classes.

You can think of MyFirstGUI as one constructor class for GUIs. In the above case, it is constructing for root but it can also construct for some(Toplevel) widgets root may later on have:

my_toplevel = tk.Toplevel(root)
my_gui2 = MyFirstGUI(my_toplevel)

It could've even constructed for Label and/or Button widget that root currently has, which are also attributes to my_gui, if master.title(...) line was removed.

"What exactly is self.master = master doing?"

That line takes a method variable (master) and assigns it as an attribute to every particular object created with MyFirstGUI class. It is then available for modifying/using under all of the sub-scopes in MyFirstGUI(and the scope its object, my_gui, is available). An example of its use would be that in the __init__ there is a label created using the line:

self.label = tk.Label(master, text="This is our first GUI!")

while that wouldn't be possible under another method, for example greet, if self.master = master wasn't there. As in having:

def greet(self):
    print("Greetings!")
    self.label2 = tk.Label(master, text="Greetings!")
    self.label2.pack()

and calling my_gui.greet() would return NameError: name 'master' is not defined error while having:

def greet(self):
    print("Greetings!")
    self.label2 = tk.Label(self.master, text="Greetings!")
    self.label2.pack()

and again calling my_gui.greet() would work perfectly as expected.

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 386275

The only relationship between root and my_gui is that my_gui uses root. Otherwise they are simply two variables referencing two different objects.

After the init method of the class executes, does my_gui and root become one and the same thing, or is root now a subset of my_gui?

Neither. root is not a subset of my_gui, it's just one of many things that my_gui knows about. It is not a subset of my_gui, it is merely used by my_gui.

If they are essentially the same being after the init function executes, then there is no need for the "my_gui" object, is there?

They are not essentially the same. my_gui is an instance of MyFirstGUI, root is an instance of Tk. They are two separate things, and in this specific example, both are required.

What exactly is "self.master = master" doing?

It's remembering the parameter that was passed in to it, so that it can refer to it later.

Upvotes: 2

Related Questions