[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

hi, im learning tkinter and watching some python videos. I have

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 1

File: 2016-08-21 17.00.43.jpg (700KB, 2048x1152px) Image search: [Google]
2016-08-21 17.00.43.jpg
700KB, 2048x1152px
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
>>56199025
>>
>>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
Thread posts: 11
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.