How to Inherit from a Python tkinter Frame

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to introduce the concept of inheritance with respect to the TK inter-frame class let's consider this computer program here and we can see on this line that we're creating an instance of the frame class if we look in the brackets we can see we have things like this height is assigned 150 which is assigned 150 relief is assigned raised and so on now if we consider what this means Heights for example well that's going to set the height of the frame but I'm assigning 152 height now this means within this class which I haven't got access to right I can't see inside it because I'm not the programmer of the frame class it comes with TK inter but I can say with some confidence that within it it has something capable of starring this 150 you see the 150 will be transferred to this which is defined inside this class so here for example I'm passing in raised to relief and I can say that inside the definition of this frame there'll be something that which is capable of receiving raised here you can see B G which is background color it stands for background color is assigned red so inside this frame there is something capable of receiving this red so when I refer to these options here and say that the values I'm passing in are assigned to something inside the class frame here what we really need to think about is this yes frame does have all of the definitions of the things that are going to store these values but what we're doing on this line is creating an instance so it would be fair enough to say that height of 150 will be given to this object and when I say this object remember frame underscore a in this case is bound to an instance of this class ie this is bound to an object of the frame class now if we look at this line we're creating another instance of the frame class and this instance will be bound to this name rame underscore b and when we consider all of the options here what we need to realize is that this value here of 154 the height will be given to the object that this is responsible for referencing so this one here for example which is a relief equals something that will be given to this instance so frame a and frame B although both of them are based upon the frame class internally they will have different values as shown here for the relief and as shown here for the background color they just coincidentally happen to have the same height and width and the same border width but what we need to appreciate is that this line creates an instance that is bound to the named frame underscore a this is creating an instance that is bound to the name frame underscore B and these values well these are the options whose values are sent to those instances of the frame class let's now consider this line and we can see we're referring to the instance of the frame class that's referenced by the name frame underscore a and we're invoking the grid method and this will position this frame at row 0 column 0 of this window why this window because that's the window we passed in here when we created the instance of the frame class and this one what we can see that go into position the other instance of the frame we created at row 0 and column 1 so when we look at the runtime for this program what we're going to see is this and as expected if you look at both of the frames you can see they're the same size 150 by 150 if you look at this one here you can see that that is red that the reason it's red is because we set BG here to red and we can clearly see that this one is green because this one here has been set to green and if you compare both of them you can see that this one is raised and this one is sunken and that's because we can see here we have the relief of each being set to something different ie raised and sunken this one we can see is in row 0 column 0 which is what we did with this line of code and of course this one is in row 0 column 1 because of this line of code here now if we consider what we've just done we've taken the frame class and if we look at the execution space we know we're going to be creating an instance of a frame and we're creating another instance of the same class and when we saw the runtime we got this and the fact that these frames look slightly different it's because the attributes of this one and the attributes of this one have been set differently clearly we can see ones red ones green and ones raised and one is sunken so when we look to these two instances here these two objects this one has its background property for example set to red well as this one has it set to green they both have the same size so inside here this one has its own copy of 150 for the height and 150 for the width and this one here has its own copy of 150 for the height and 150 for the width now that's key to understanding object orientated programming you see we're not saying this stores the 150 the instances store the 150 this instance stores the color red and this instance internally stores the color green so when we talk about creating instances of frames we can see that we have the same kind of widget on the screen it's just that we've set him up to look slightly different let's consider the frame class for a moment now this is something that comes with TK inter and you've seen how we can use it I've created a red and a green frame and put them on a window now what features this frame gives us I would have to look up in the help and simply have a go at do directory function on the frame to see what attributes it has I'm not responsible for coding this frame but I am responsible for using it as the programmer if I decide however that the frame doesn't quite give me everything I want because remember we can use the frame class the frame object the frame instance to build up complex widgets widgets that will contain a button a label and various other methods tied to it that will perform some kind of task if I therefore see this as the basis of what I require for one of my applications I can decide to create my own frame which I'm calling my frame that I'm gonna give properties and methods to but the key here is we could have something refer to as inheritance and inheritance is shown by this open headed arrow here meaning that my frame inherits from frame and in this area I now can add properties and methods the key here is that this will have all of the properties of methods that have been defined here so if this has X number of attributes this has at least X number of attributes plus all the additional ones that I as a programmer decide to add to my frame let's consider the terminology surrounding the use of inheritance when we look at this diagram here we can see that my frame and frame are example of classes and my frame is inheriting from frame now when we have this relationship it is usual to refer to this as the superclass and this one here as the subclass so my frame is the subclass because it is inheriting from frame and frame is the superclass as it is being inherited from all the words used to describe this relationship is parent class and child class and you will often hear the term base class and derived class the is one for example that's used when discussing vb.net when we're dealing with Python I recommend that you go with super class and subclass now the relationship you can see in this diagram is dealing with classes my frame is a class on frame as a class and my frame is inheriting from frame what we need to appreciate is my frame is a class it's not an object so if we consider the execution space what we need to realize is if I wish to use the frame that as a programmer I am going to create a Eve the definitions inside the my frame class I will want to create instances of that class and here you can see I'm producing two instances based upon the my frame class and what's important is that both of these instances will be able to have access to the attributes we declared in this class here and the additional attributes that we defined in this class by me as the programmer so these two have at least all of the attributes declared here plus the attributes declared here but it is important to realize that you do have this class relationship but we then still produce instances of the class that as a programmer and responsible for creating based upon the frame class that was given to us by TK inter to emphasize the point let's consider the frame class the one given to us by TK enter the one as a programmer I can use but I now decide that I want to build up an application based upon this class and I decide that this is going to be called my frame and it's going to inherit from frame if I now look at the execution space as the coder I can decide that I want to create an instance of the frame class the thing that TK interns givers and I'm going to create another instance but equally I create instances of my frame the cluster I was responsible for coding and I'm showing two being created now these things here that we need to consider you see this object is based on this class likewise this one this will have X attributes what we can say about this object it will have at least X attributes it will have all of the attributes that this one has but this one will have all of the additional ones that were declared in here by me the programmer now that means that this class here that I'm coding I could add buttons to labels methods and I can have an application built up in a frame and I can then position that frame on a window so whereas we're looking at the execution space here and looking at a number of objects these objects are related to each other by the inheritance that you can see in the class diagram these to have identical attributes these to have at least all of the attributes of these to have plus the additional ones that are declared in here by me the programmer of this class before we look at how we can implement inheritance in Python let's just have a quick look at this computer program here on this line you can see we're creating an instance of the frame class now I can show that by taking the diagram here to represent the class frame now what we're doing if we have a look at these brackets here we're setting the height to 150 the width of 150 the relief to raise the border width to 8 and the background color to red the key however is that this creates an instance of the frame class which I'm showing here appearing in the execution space now of course when we look at the runtime what we're going to see is this and we can see that the frame has been added to this window where the window was created on this line and pass to the frame here pass to the instance that were creating at this point and of course here we can see we've set the height of 150 the width to 150 and if we have a look the background color is red and we can see the background color of the frame is red here we can see that the relief is raised and the border width is eight to introduce inheritance I'm going to take the frame class the one that's given to us by TK inter and I'm going to produce my own frame class called red frame and it's going to inherit from frame and the desire in this case is to allow me to create instances of the red frame that give us exactly what we saw a moment ago a frame that appeared on a window in a red color as its background with a raised relief and a border width of eight and a size of 150 by 150 now clearly I could do that by creating an instance of the frame class and passing in the appropriate values however I want to achieve this result using inheritance so what we can see here I have two classes where one is the superclass and the other is the subclass and if I want in Python to set up this relationship I do it as shown here I use the word class I then choose the name of my choosing as the program a red frame taken from the diagram here and in brackets I put the name of the super class which is frame which is this class so here what we can see this is the subclass and this is the superclass so the red frame inherits from this this is the subclass that's the superclass and of course as we always have in Python we will see the need for this colon here so when we wish to create a relationship that is inheritance this is how you do it you have the name of the subclass here and you have the name of the superclass here what I want the code to do is to create this relationship and then create an instance of the red frame class which I'm showing in the animation as you can see here I create this instance which is going to be based on the red frame class which has a program I'm responsible for producing and of course the red frame class is going to be based on the frame class where the red frame is the subclass and the frame plus is the superclass now at runtime what I want to see is this and you can see that the frame is got a red background it's got a relief that's raised it's 150 by 150 and the border width is going to be 8 but to be clear this frame is going to be an instance of the red frame class not an instance of the frame class with various values passed to it I wish this frame to be an instance of this class the one that I'm going to code using inheritance this is the code for the red frame class now I'm going to come back and describe this at the moment what I now wish to do however is just to come down here and have a look at what we're doing and you can see the frame underscore a is assigned red frame and in brackets we've got my window now what this line is doing it's creating an instance of the red frame class the class that as a programmer I've been responsible for producing now I just want you to compare that with this line of code here and you can see that's how we created an instance of the frame plus and if you have a look I'm using the same name here frame underscore a and in brackets you can see I'm passing in my window I'll have a look in this region you see there's nothing being passed in to the creation of the instance based upon the red frame class whereas here you can see we've passed in quite a few things to hide the width the relief the border width and the background color being red and then here of course you can see that we're positioning the instance of the frame class whereas here you can see we're positioning the red frame in exactly the same position as we placed it when we run the program as shown by these two lines of code let's now turn our attention to this class here which is the subclass and you can see it's called red frame and it's going to be inheriting from this superclass now when as a programmer you decide to declare your own classes you have to realize that you need this method here and this is the double underscore init double underscore method and you can see that we have the d EF and we have the colon here and have a look in brackets and you can see there are two parameters now you one of them is self and the other one I've called the underscore window now self I've covered elsewhere on my youtube channel then it's very important when you're dealing with classes as you know what this is now I am going to explain it here but a fuller explanation appears elsewhere on this channel what I wish to do now is to come down here and have a look at how we create an instance of the class well the instance is created by using the name of the class and then you can see I'm passing in my window now my window is the window created on this line and we pass it in here so that the instance of the class knows to which window it is going to be associated and this is passed to here to the window so the instance of this class knows what window it's going to be associated with because whatever the identity of this is is passed to here this is the reference to my window and that reference is passed to here now what is self well whenever you create an instance of a class as shown here automatically and not seen as a parameter in the brackets here is the ID of the instance created on this line so this receives the ID of the instance being created and it means that the code in here now knows which object it belongs to now this is handy because we can create lots of instances of this class and each instance will have its own ID so this will therefore be representing the ID of all of those instances so instance wand so to speak the ID will be held by this instance to the ID of that instance will be held by this to avoid getting too bogged down with the details regard each instance as having their own copy of this so this is the class and this is the initialization routine for this class and every class needs one of these and this line creates an instance of the class were considering and when that instance is created this receives the ID of that instance and we can see that this self is used here in the code now that means we are referring so the current instance that's been created the current instance of this class this is the argument that's passed to here now that means that the current instance knows which window it's going to be associated with now when the instance is created on this line the code that will execute next is this lot here and of course we'll be dealing with the current instance as specified by this self if I go to this line first you can see that this is saying for the current instance what I wish you to do is to set the height to 150 this is same with the current incidence set the width to 150 this one is saying set the relief two raised the border width to 8 and the self ie the current instance background color is going to be set to red and if I go back to this one you can see this is referring to the current instance here you can see I've got these square brackets and in these square brackets I've got the height and I'm assigning that the 150 now the question you need to ask where does this height come from how come I'm able to gain access to the height property for the frame because that's really what this line is saying well the reason I can get at this is if you remember this class which is called the red frame is the subclass of frame now because it's the subclass it has access to whatever is defined inside here so it does have access to height to width to relief to boarder wit and to the background color so as the programmer of the red framed class I've not been responsible for creating the height the width the relief the border width or the background color because of inheritance I am able to access these directly from within the instance of this class because this class has inherited from the frame where the definition of the height the width the relief the border width and the background color were initially set up to ensure we set up an inheritance relationship between the red frame class and the frame class that was part of TK inter we have to insert this line of code within the initialization routine of the red frame class now what this line of code is responsible for doing is invoking this initialization routine that is part of this class part of the superclass it's not one I've been responsible for coding it comes with the widget to frame widget that's defined inside TK inter and this here is a super method that allows us to gain access to things that have been declared in the superclass and of course the superclass in this case is the frame class so ensuring access height with relief BD and BG it's not sufficient simply to have this line of code here where this is defining the superclass and this obviously by definition is the subclass we have to have this line of code here so let's walk through the execution of this program this creates the instance of the window which we then pass in here and this line creates the instance of the red frame class which means that this will execute the first line to execute is this which executes the initialization routine of the superclass and here we then find ourselves setting the height width relief border width and background color appropriately for the instance of the red frame class then this line or what it will do it'll position the instance we've just created that we have bound the name frame a2 and it'll position it in row 0 column 0 so what we should see when the program executes is this there's our red frame within the window but we must be clear that this red frame has been based on this class here and this class we saw we set the various properties of the instance with this bit of code and of course we then positioned the instance using this line of code here it is important that we don't get these two lines of code confused with the two we've seen before i v's to where here you can see we're creating an instance of the red frame and here we're creating an instance of the frame and when we created the instance of the frame we can say that we set the values here well as when we created the instance of the red frame there was no setting of values in these brackets the setting of the values was done inside this definition because through inheritance we were allowed access to the various attributes that were defined within the frame class let's now consider this computer program which is almost identical to the one we've just considered except I've now got two additional lines of code the first line is this one and this is going to create another instance of the red frame class and this line is going to position that instance so if we have a look at the runtime for this program what we will see is this and we can see we have two red frames this frame is this one here positioned by this line of code and this one is the instance created here positioned by this line of code of course this class being built here is in fact not very useful as it's all it's doing is giving me the ability to produce a red frame of a certain size with a 3d look of course frames are used to build compound widgets and what I mean by that you take a frame and you add to it some entry widgets some buttons and some labels and you click on a button and some processing takes place and the results are displayed in a label from data entered via an entry widget and then what you have is a frame beam built into a class that inherits from the frame class and then you can position that sub class the instances of that sub class on your window where you like now I'll be looking stood at in some videos that are coming up in this playlist let's consider this computer program and you can see he and I have the same definition of the red frame class and it's inheriting from frame so red frame is the subclass and here I create four instances of the subclass and here I position those four instances and what you can see I will have is this and you can see there are four frames each positioned appropriately and each frame that you can see in the runtime is based on the subclass that's been called red frame let's quickly look at this computer program and you can see I'm got the same class being declared here the red frame class which inherits from frame and on this line I'm creating an instance of that class and this line I'm positioning it well here I'm creating an instance of the frame class and I'm setting its values to this and I'm positioning it with this code here and then I'm producing an instance of the red frame class and then I'm positioning that one with this line of code and then I come on to this which create an instance of the frame class with the appropriate values here and then I'm positioning that with this and of course when I look at the runtime this is what you will see and these two widgets were based upon the red frame class and these two were based upon the frame class would be appropriate values passed as you can see in the code here what we seen with the last program is this we had the frame and we created a red frame that inherited from frame and then if we look to the execution space we created an instance of the red frame then an instance of the frame then another instance of the red frame then another instance of the frame and then these instances these objects were then positioned onto the window and you can see that illustrated here if you look at the four objects that exist in the execution space it's important to realize that they all have their own copy of the attributes that have been defined in their respective classes ie the classes on which they have been based so these two here for example have the same attributes that are defined in here but they can but they're not in this case they can be set up to be different values these two well they are two objects that are based upon the red frame class and they have their property set as appropriate each of these objects will have the self parameter identifying them uniquely so this one will use the self that was declared in the initialization of this class and this one will have access to that self that was declared in this class but the values that are in that self are different this one has its own ID and this one has its own ID check out the supporting website for these videos in addition why not follow me on Twitter as is you with tweet every time I upload a new video
Info
Channel: John Philip Jones
Views: 20,550
Rating: undefined out of 5
Keywords: Python Inheritance, tkinter inheritance, Inherit from Frame, Inheriting from the tkinter Frame widget, How to Inherit from a Python tkinter Frame
Id: HTyN25rnla0
Channel Id: undefined
Length: 31min 11sec (1871 seconds)
Published: Fri Jun 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.