-2

I was learning the tkinter package in Python and I didn't understand next code:

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

As I understand self refers to the class and when the code says self.hi_there I expect a global variable in that class that has to be declared previously. How is hi_there created?

Also what is the use of having "master=None" in the __init__ method? Wouldn't it be the same if I skip =None part since I do app = Application(master=root)?

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
akerbeltz
  • 39
  • 1
  • 9
  • Not sure why you think they don't exist. – cs95 Aug 27 '17 at 23:06
  • It’s created at `self.hi_there = tk.Button(self)`. – Ry- Aug 27 '17 at 23:06
  • And it's created as a global variable ? what's the meaning of self here? why not simply put "hi_there = tk.Button(self)" – akerbeltz Aug 27 '17 at 23:09
  • These questions can be answered by working through a basic python tutorial. None of what you ask is specifically related to tkinter. – Bryan Oakley Aug 27 '17 at 23:16
  • No. `self` is **not like a global variable at all**. It is the *instance namespace*, *not the local or global* ones. You should read through some tutorials on OOP in python. Also, Python does not have variable declarations. – juanpa.arrivillaga Aug 27 '17 at 23:27

2 Answers2

1

self refers to the instance. self.hi_there is an instance variable. By doing app = Application(master=root) you create an instance of Application and save it to app. In your case self is app.

You don't need to declare properties before you create them (though it's considered to be a good practice to create them in __init__).

Consider example:

class A()
    pass

a = A()
a.prop = 2
print(a.prop) #=> 2

About master=None -- indeed, you can use just master if it's your code and you know you will pass it.

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
  • but in that example it doesn't change anything if i simply create the widgets like `hi_there = tk.Button(self)` instead of `self.hi_there = tk.Button(self)` Whats the meaning of self here? – akerbeltz Aug 27 '17 at 23:18
  • I dont know how to explain, i tried the code without the self at the begining at each widget and it doesn't change anything (that i notice). Why not simply put "hi_there = tk.Button(self)"? – akerbeltz Aug 27 '17 at 23:33
1

self refers to the instance of the class and therefor self.hi_there = foo will create a new instance variable and assign it to foo like:

class Test:
    def __init__(self):
        self.foo = 'bar'

a = Test()
print(a.foo)

# output:
# bar

And master=None is setting a default None value to master unless you supply that value, for example:

app = Application() # here master will be None
app = Application(master=root) # here master will be root

This can be used with any function, here is another example:

def plus(num=0):
    return num+num

print(plus(1))
print(plus())

# output:
# 2
# 0
Mohd
  • 5,523
  • 7
  • 19
  • 30