A Digital Clock built with Object Oriented Python and tkinter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to take a look at how we can produce the same digital clock as the previous video in the TK into playlists but doing so using a computer program that's object orientated in nature the last video in the TK inter playlist looked at how we could write a computer program to produce this digital clock um in a moment I'm going to have a quick look at that program again and then for the rest of the video I'm going to show how we can produce the identical clock you're looking at here but done so from the perspective of the object orientated paradigm in other words I'm going to create a class and we're going to create an instance of that class which will produce this digital clock this is the computer program that we looked at in the previous video and it was responsible for the digital clock we've just observed if we consider the overview of this we know we have to import modules and here you can see we have the definition of a function and here we have some program statements and you can see that this program statement will call this function this line creates an instance of a TK inter window consequently we could say that this is belonging to the object orientated paradigm but in fact the way as a programmer I am writing this code I'm doing so from the procedural paradigm because here you can see I'm using a function and the code here at this line is responsible for calling this function so I'm building up a computer program that is procedural in nature because you can see that we're dealing with functions not methods and classes which is what I'm gonna move on to later in this video let's do a walk through this computer program this is the line to execute first and this creates an instance of the window this line will set the title of that window to this string digital clock on this line we're creating an instance of the label that takes in as one of the parameters this which is the name of the window created on this line consequently the label created on this line is associated with this window and of course here you can see we set the font to Arial size 80 the background color to black and the foreground color to red this line will then position the label at row 0 column 0 then we will move on to this line which will call this function the first line of this function will execute and this will actually go and get the current time and we passing this string to this method and of course we're going to get back hours minutes and seconds the current time is then bound to this string returned from this method and on this line we assign that string to the text property of the label that was created on this line so the label will now be displaying the current time this is the next program statement to execute and what we're doing we're setting off a timer that will run for 200 milliseconds and after those 200 milliseconds we will invoke this method and of course the method you can see use display underscore time which indeed is this method here but as soon as this has executed the clock starts and we then leave this function and we go to here into the main loop now while in the main loop when the clock that was started in the background setup by this number here gets to zero as soon as it gets to zero what will happen is this will be executed which is of course this function this function will then gone read the time again it'll put the current time to the label and this will then fire off the clock running in the background so because this is 200 milliseconds the clock will be updated every 200 milliseconds and eventually of course it'll click over to a second and you can see the seconds go up and then the minutes go up and then the hours go up in other words we have a digital clock now I appreciate I've run through this rather quickly bear in mind that I've actually covered this in the previous video in this playlist so I'll put a link in the description to that previous video so you can go and look at that first if my explanation here was a little too quick before I move on let's have a quick overview of this program again here you can see I'm importing the modules here you can see I've got a function that will display the time and here the program statements that are responsible for setting up the window with the label that is going to be responsible for showing the time change as the program executes what I'm going to do now is to show you the object orientated version of this but I want to remind you of what in here first you can see we have these three lines and I'll be coming back to look at these but I'll first of all be looking at this code here which we can see sets up a window set up a title creates a label positions our label and then calls the display time and then moves into the main loop so we're going to be considering this code alongside the code for the object orientated way of implementing the same digital clock the same as it looks when we actually execute it and I'll be pulling up this section of the code so we can compare this section to the object orientated program I'm going to show you and are we pulling up this section of code on the same slide as the object orientated program so we can compare directly this is the object orientated solution and if we look at what it's got in common with the one we've just been considering which is the one that belongs to the procedural paradigm we can see we're still importing the same modules if we come here you can see that I'm still creating an instance of the window and on this line I'm still going into the main loop but look here you can see there's just one line that has replaced quite a bit of the code that we saw before let's have a look at that bit of code here we set the title we create the label we position the label we call this a time so in other words this line from the procedural program has survived here and this line has survived here but these lines have been removed and in their place I just have this one line here now I'm going to talk about what that one line does in a moment if I now have a look at the code that appears now it is display time and if you have a look those are the three lines that we've just discussed for the previous procedural program that produced parts of the program for the new running of the digital clock and you can see here that I have a method called display time and if you look it to has three program statements and those three program statements mirror these here but I'll be breaking down each line in turn because a quick inspection you should see here we start to introduce this notion of self I've removed the program code that came from the procedural solution because I just want to walk through this example here the first thing that we can see is we import the various modules and here I'm declaring a class and the class is called digital clock now here we have a double underscore init method we always will find ourselves needing to put this into classes we produce ourselves when you create an instance of a class in other words an object which you can see is happening on this line that my underscore digital underscore clock is assigned this and this is the name of this class and consequently what this will do will create an instance of the digital clock and this will be the name bound to that instance and when we declare the class as you can see here we will have one of these methods now just as an aside when you create an instance of any class as shown here the first piece of code to execute is underscore underscore new underscore underscore sometimes refer to as dunder nu now that is the method defined in pi responsible for creating the instance what this does is initialize the instance that's being created but that's something for another video I think it's important when you build your own classes you realize you're gonna need one of these so if we consider the class definition you can see we have two methods we have this method and we have this method here let's just remind you a class is a template from which we are able to produce objects in other words instances of this class were able to produce digital clocks in this case when we talk about these instances we have to remember that they are instances in their own right so here for example I've got the definition of a digital clock there's nothing to stop me in my code asking for four copies of the digital clock give me four digital clocks and display them that's what we can do when we have an object orientated program as shown here I can ask for as many instances of this digital clock as I want in the code down here in this case I'm only asking for the one on this line so you can think of a class as a factory that pumps out objects every time you want one so you say right I want one of those clocks okay to put a line like this in I want another one okay put another line like this one it so you get objects being produced by this which you can think of as a factory producing things now everything that comes out of a factory it will be identical let's say a particular car comes off the production line each one were bound to have a number identifying it now when you create an instance of a class in other words an object that object will have an ID and every object created will have an ID now in cpython the ID will actually be the address of where the object appears in the computer's memory but for the purpose of this explanation it's just a number and every object will have a number and every object will have a different number from each other this line is responsible for producing an instance of this class and when that instance is created the done the new method is called and this new method will create the object and will be responsible for obtaining the unique identifier for that object the dunder new method then calls this method and gives it the ID and the ID will be stored here now you may be saying well I can say this done the new method here well no that's correct your responsibility is the programmer is to produce this this goes on behind the scenes of Python but so what we can say is when you create an instance as this line is showing you this self here you can think of us automatically receiving the ID of the object that's being created so the object will now be identified by this name self instead of you having to think about what the number is this will hold that number self so every time an object is created that object will be responsible for gaining access to self and in each case the object will have its own unique identifier which is stored in self now if you are a little baffled by this notion of an ID and self please note that I've done a two of three videos on self elsewhere on this YouTube channel and I would recommend you search the channel for the keyword self and have a look at those videos if you're unsure but just think of it like this this class is a factory that's capable of producing instances ie object and every one of those objects will have a unique identifier and that unique identifier will be stored in self so let's do a run-through of this computer program this is the first line to execute and we know this is going to create a window that I've decided to call my underscore window so this will be the name bound to the instance of the window that this has created this line will create an instance of the digital clock and if you have a look in brackets what were thing in is the name that's bound to the instance of the window that was created on this line now where does this go why am i passing this in well what I'm doing I'm saying I want you to produce a clock that's going to be on this window so this then gets passed to here so this method identifies this window created on this line by this name so in other words this created the window this passed the name bounced of that window to here and now the window is known by this name and of course this self is there because it as I've already said we can think of as being automatically given the ID of the object that this line is creating now here on this line I'm taking this name putting it here so this now is the name bound to the instance of the window that was passed in here and you can see that I'm assigning that to this name and what you can see I've put in front is self . and that's because I want this window which was in fact the window created here to be bound to the instance of this class in other words this is the name of the window for the instance of the digital clock the object of the digital clock that this line has created on this line I'm now referring to this window and you can see I'm altering its title to digital clock on this line I'm creating an instance of the label and I'm giving now the name clock underscore label but of course because I have to identify this label with the instance I've just created I have to put the word self in front of it and here you can see I'm sending a message to this instance which is the clock label and the word selves in front of it because again we're referring to the instance of the class that I'm producing hence the need to put self in front but we're still dealing with the label that was created on this line and you can see that's the name bound to the label so I've had to you the name again here and I'm invoking this grid method which will put the label at row zero and column zero and here what you can see I'm doing I'm calling the display time which is this method here but note in front of this line you can see we have the word self because this is the method that is going to belong to the specific instance created on this line so the word self has to appear you can now see that I've added this snippet of code to the slide and this snippet of code was taken from the procedural program and if we considered the first line you can see that that appears here in the object orientated program and these lines here do not appear in this position they have effectively been replaced by this one line so let's look to see where this is gone this line where we set the title of the window well come up here and you can see this is how where we set the title here we're passing in digital clock no difference of what we passed in here but note what I've got in front of this is self dot the window now that here was my window now the reason why I haven't got my window here is because if you look at when I created the instance of the digital clock this is the name of the window that was created on this line and that window is passed to here and then I assigned that windows name which was bound to the incidence of the window to this name here so this is the name I use here but to all intents and purposes this line is doing exactly what this line did it's just that in essence this name has been changed to this name within the initialization routine if I now come to here you can see I'm creating an instance of the label and if I come to the object orientated program I'm doing exactly the same of course if you look at the name here of the label it's clock underscore labor whereas here there's the clock underscore label but of course I had the word self in front of it so in effect I've changed this name to this name within the initialization routine but furthermore if you come here and you look at the name of the window that the labels gonna be associated with it's my underscore window now compare that to this and you can see that itself . the underscore window but this name here is the name that i have effectively given this and of course this got its name from here in other words I created this window I passed that window to here this line takes that window and gives it this name that's essentially what happened of course what we are doing we're passing around object references there's a whole video on that if you wish to have a look at it on parameter passing and how when we pass parameters in Python were passing around object references if we now come onto this line where we position the label that was created here when we come up here you can see we're doing the same thing we're using the grid method as we did here and we can see we've got row equal zero and column equal zero in the procedural code and I've got exactly the same thing here of course when you come back to the procedural code you can see this is clock underscore label but of course it's got a new name here it hasn't it it's got clock underscore label as part of the name but of course we have to shove this self full stop in front of it but in essence this line and this line are positioning the label on the window now if we consider this line in the procedural code you can see it is calling the display on the score time function whereas if we come to the object orientated program you can see here it looks like we're doing the same we're calling the display underscore time but in this case it will be the method because it appears within the class but look in front we have to put the self dot because we'll be referring to the current instance of the digital clock and then we find ourselves leaving this method and of course where do we return to so how have I had to alter this code here to get it to work in the object orientated world well you can see this line and this are identical you can see that these lines have been moved to here and the difference is you can see I've had to use the word self in all of these positions and if you look at the name of the window we had it called my underscore window here well as here you can see we have it called self . the underscore window because that's the name here that we set we pass this name to here it appeared here within the initialization method we then assigned it to here so this is the name effectively of the window that was created on this line so if we look at the overview of the initialization method we know we have to have self e're so it will receive the unique identifier for all of the objects of this class that's produced and you can see the self has appeared here it also appeared here because we can see that the window has got this name as shown on this slide here this is the code taken from the procedural program and you can see it's the display time and now if I look at the object orientated program you can see I've got the display time here now what's the difference well let's have a look at the procedural one you can see I've got bracket here well is if I come to the object orientated one I've got brackets here but it's got the word selfie why is he got the word self there because we're saying that this method is going to be associated with the instance that's been created on this slide and this holds that unique identifier for the object that's created on this line where this line when I say has the object it has a specific instance of the class the digital clock class now we know this line is responsible for getting the current time because it invokes this passing in this string and of course this will return the string of hours minutes and seconds and this is then the name bound to that string which holds the and time if I compare this line with this one what's the difference well have a look at this side no difference well come over here and you can see the name well I've have to put the word self in front of it I've kept this name here but of course I've put the word self in front of it because the current time has to be identified for this instance if I come here now and look at this and compare this with this bit of the code here you can see it's similar but of course because I'm in a definition of a class I have to put this word self dot in front of it if I come onto this line you can see that I'm going to my window and I'm filing off a clock that goes in the background for 200 milliseconds using this after method here and of course it will call the display time after 200 milliseconds if you come here what a vile turd this is called my underscore window whereas here it's called self dot the underscore window well that's because if you remember this line here passed in my underscore window too here so we effectively change the name by which we reference the window in this class and that this name here but of course we have to tie this name to the specific instance we've created this specific object so we put the word self in front of it so you can see that the difference between this and this is we've have to put self in these places let's make sure we know about where we have to position self let's just go to this line and you can see here and I've got current time well as if you come here you can see there is the current time but the word self has had to appear in front of it if you look here on this line you can see that after 200 milliseconds we execute this and if we come to here you can see we have here self dot display underscore time whereas here it was just display underscore time we had to put the word self here because we're talking about the need to invoke this method here now you may say well have a look at this name that's displayed on the score time which is the name of this shouldn't this have the word self in front of it no most definitely not but it does need it to be here because what we're saying is we want to be executing the version of this that appears in the current instance where the current instance is identified by the ID that's posture here and that ID is then used throughout everywhere in this code and here in this position is no exception so this is responsible for invoking this well you see we have the name being display underscore time and so that this method knows which instance it belongs to it has to be passed this which is the ID of the instance that this method is now going to end up belonging to now if at this point you are thinking crying out loud I'm not going to bother writing things that are object orientated in nature there's all of this self everywhere I'm not quite sure I know what it means well don't do that object orientated programming is a key feature of Python my background I started off in assembly language I then moved to see and I went on to a number of other languages on my way to ending up enjoying Python as a language for writing code and when I initially moved on to object orientated program a long time ago I used to sit there thinking I mean this is a pain what's going on why am I having no I just don't get it and soon as the penny dropped however it meant that when I then got involved in writing systems which involved systems analysis design of systems and coding of systems I found myself moving away from procedural paradigms to object orientated paradigms because it became a more natural way of thinking about systems and I find myself when I write my own code doing nearly everything in the object orientated paradigm my advice is you really do need to know both and the other one that's quite important is the functional paradigm but that's something I won't be coming onto for quite a long time but bear in mind this may seem overwhelming in terms of I've got all of these cells everywhere why am i bothering doing this when I had the digital clock working very very straightforwardly I had a function and a bit of code and it worked here I'm going to produce exactly the same runtime but I've had to produce a method that initializes an object I've got to produce another method here that replaces the function I had before and I've got to insert all of these cells but the result is you will have a class and that class will give you a digital clock and all you have to send to that class is the window that you want the digital clock to appear on consequently if you want 10 clocks you just pass to each instance that you want each clock that you want the window and say yeah there's a window give me a digital clock no I'm not going to do it but as an exercise have a think about how you would have more than one digital clock using the procedural approach to coding and see if you can avoid using global variables when you do that because you really should never use global variables there are occasions when you need to but even then I would use a singleton class because I would stay with the object orientated paradigm but if there is a worry in your mind well I think I'll just stick to procedural coding I would strongly advise against understand obviously the procedural paradigm but also work on the object orientated paradigm bear in mind that I've done a lot of videos on the object orientated paradigm what I've gone into some detail about self what I'm going to do now I'm going to remove these snippets of code from the procedural paradigm and I'm going to do a walk through the object orientated program which gives us the digital clock right let's do a walk through for this computer program these modules are imported we then come onto this line which creates an instance of a window that will be known by this name so this name is bound to that instance this line will create an instance of the digital clock class in other words an object passing in this my underscore window which is the name bound to the window this my window is past to here it is then on this line assigned to this so the window that was created here is now known by this name we reference that name unchanged the title of it to digital clock we then come to this line we create an instance of a label associated with this window which is the window declared and created here but got renamed to this on this line and here you can see we set the font the background color and the foreground color we then come onto this line where we position this label with this method and then we call this and what this will do is call this method this line creates the current time to be associated with this name so this is the name bound to the current time we then take that name and we assign it to the text property of the label that was created here during the initialization method and then we find that the current time will be displayed in that label we then come to here and we use this method to set a clock running in the background for 200 milliseconds then this finishes executing and consequently we return to here into the main loop now the main loop now sits there waiting for events to occur and of course as a user I could click on the minimize to maximize or shutdown the digital clock but also because of this line I've set a clock running in the background after 200 milliseconds this main loop gets interrupted and event takes place the event being this counted down from 200 milliseconds and because of this line the code to execute is this now this name refers to this method consequently we're gonna get the current time again we set the text property of the label to the current time and then we execute this which starts the clock running in the background and of course we go then back to the main Loup within the main loop after 200 milliseconds this executes again another 200 milliseconds this executes again and we have this process taking place so the wrong time is exactly the same if we looked at the digital clock you wouldn't see any difference but behind the scenes what you've seen happened I create a window I create an instance of the digital clock associating it with the window that was created on this line I then enter the main loop and of course what you can see is when we initialized we fired off the display time and then this set up a background cloth that caused the main loop to be interrupted to have an event occur every 200 milliseconds that it after service which meant executing this which is this method what we have that's different we have the initialization method here building the clock we have this updating the label on the clock that was built here this initially fires the display time and we have a runtime which is identical to the runtime we got one we did it procedurally now the main change is we've had to use this word self everywhere as you can see in the places that I'm highlighting now so here is the computer program we've been discussing and what I'm going to do now is to fire it off by pressing f5 and you can see that we get this digital clock and it's happily counting up in seconds as you can see let's consider this computer program which is almost identical to the program we've been looking at so far here you can see I'm importing the modules this is the digital clock definition that we've looked at so far in this video and here you can see that I am creating an instance of a window and on this line I'm creating an instance of the digital clock that takes in the window created on this line so I'll get a digital clock being displayed here you can see I've created another window that in fact I've called another underscore window and that will create a window and on this line you can see I'm creating an instance of the digital clock that I'm passing the window that was created on this line and this is the name that's going to be bound so that instance of the digital clock consequently if I run this computer program which I can do by pressing f5 on the keyboard we will get the following and you can see I've got two digital clocks running so hopefully you can see by putting the code for the production of a digital clock into a class as shown here I was able to create an instance of the digital clock here and another instance of the digital clock here and all I had to do was to have two windows created unto this one I passed in this window and to this one I passed in this window of course if I wanted three digital clocks hopefully you'll be able to decide what you would need to do to have three instances of the digital clock running and I'll leave that as an exercise for you also as an exercise see if you can get two digital clocks or maybe three digital clocks running if you were to use the procedural approach through the production of the digital clock check out the supporting website for these videos in addition why not follow me on Twitter as is you a tweet every time I upload a new video
Info
Channel: John Philip Jones
Views: 4,018
Rating: undefined out of 5
Keywords: Python tkinter Object Oriented Digital Clock, Digital Clock, Python Digital Clock, Python tkinter Digital Clock, Object oriented digital clock, A Digital Clock built with Object Oriented Python and tkinter
Id: TiTkgTrHw5g
Channel Id: undefined
Length: 34min 34sec (2074 seconds)
Published: Wed Oct 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.