hi, im learning tkinter and watching some python videos.
I have two questions from this video i was watching on youtube called "objext oriented programming crash course woth python 3".
The questions are the commented lines in my picture.
Any help would be appreciated.
Tk is the class in the tkinter module
self refers to the instance object that calls __init__ when a new instance has been created
>>56198625
why is tk.Tk being passed as the argument in the class?
>>56198680
Its the class its inheriting from
>>56198495
Not trying too hard, huh? But at least you want to learn, so...
1)
tk is a module.
tk.Tk is a class in that module.
When you use 'class SeaOfBTCapp(tk.Tk)' you are saying SeaOfBTCapp will "inherit" all methods and attributes defined in the class tk.Tk. In other words: the SeaOfBTCapp class will have all the methods and attributes of tk.Tk, without rewriting them. Python has multiple inheritance but for reasons I dont grasp yet, you should avoid it, if you can.
2)
'self' tells the method that has it as argument that it can access everything inside the object created when you instantiate the class. 'self' is just a placeholder for the object itself that will be created when you use 'SeaOfBTCapp()'.
Now, after you have defined the class, each time you use 'SeaOfBTCapp()' a new object is created. The instant you create and object the special __init__(self) method is automatically called and since it has 'self' as argument it can already access all the contents of the object (methods and attributes) using the 'self' variable name.
In this particular case objects created from the SeaOfBTCapp class can access methods and attributes of tk.Tk using 'self.method()' or 'self.attribute()'.
Btw, "methods" and "attribute" are just other names respectively for "functions" and "variables" bound to classes/objects.
Thanks. I understand everything except self.
So is the point of self just so if we want to make multiple objects?
ex.
app=SeaOfBTCapp()
check=SeaOfBTCapp()
also why is line 4 there, the youtuber says we are initializing tkinter i dont get it isnt tkintr alreafy imported
>>56199412
Let's do and example.
I'm using 'object' instead of 'tk.Tk' because we don't need inheritance to understand 'self'.class SeaOfBtCapp(object):
def __init__(self, number):
self.mynumber = number
def print_my_number(self):
print(self.mynumber)
app = SeaOfBTCapp(5) # __init__ is called, number is 5. 'self.my_number' in this case is app.my_number
check = SeaOfBTCapp(6) # __init__ is called, number is 6. 'self.my_number' in this case is check.my_number
app.print_my_number() # prints: 5
check.print_my_number()) # prints: 6
app.my_number = 0
check.my_number = 1
app.print_my_number() # prints: 0
check.print_my_number() # prints: 1
'self' refers to the object that owns the method when called.
If the print_my_number method didn't have 'self' as argument it wouldn't be able to access the object and its attribute 'my_number'. Same thing goes for __init__.
As for line 4, I don't know tkinter, but it must be just the way tkinter programmers have chosen to do things. They could have designed it differently if they wanted.
>>56199803
I messed up the closing code tag, but you get the idea.
>>56198495
if you can't rotate a picture get the fuck out of here and never come back
nice, i shall let you know what line 4 means should this thread survives