Classes with tKinter - Python Tkinter GUI Tutorial #43

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here for economy common in this video we're gonna look at using classes with kinter and Python alright guys like I said in this video we're gonna start to look at classes for kinter but before we get started if you like this video I want to see more like it be sure to some hash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out code me calm arrive dozens of courses with hundreds of videos that teach you to code use coupon code youtube one to get $30 off membership that's all my courses videos and books for one-time fee it just $49 which is insanely cheap okay so up until now with Kenter we've been just you know putting things on the screen and making little apps but as you get more sophisticated with building out your graphical user interfaces your software you're gonna have to sort of keep track of things better and a great way to do that is by using classes now classes with python or a sort of more advanced topic they're not super difficult they're just more advanced they're not really beginner stuff and I've been sort of trying to tailor these kinter videos to more beginner levels so this is definitely the next step up so in this video we're just gonna start to look at classes we're not gonna dive into them really deep we're just gonna you know sort of set the framework and then we'll build on that from there but classes with kinter are actually very easy you just define your class create your initialization function method whatever you want to call it and you know change a couple little things from what we've been sort of used to doing up until now so that's we're gonna look at in this video so before we get started very very quickly I have very fancy t-shirts for sale if you like the codeme t-shirt and would like to buy one for yourself I've got a link in the comments below it's just tea spring calm for /co to me - calm question mark PID equals looks like 369 or you could just let's just try this yeah just gotta code me slash calm calm and you've got this basic he's you're here we've got hoodies so if you're interested in looking very very stylish go grab a coat of a calm t-shirt I make a couple of bucks with every time you buy them and then not very much but they're fun to have so anyway so I've created a new file in our GUI directory that we've been using up until now and it's just the same kinter basic code we've been doing always you know we define our root window we've got a title little icon and we set our sizes 400 by 4 hundred down here we have our main loop so up until now all this is the same so to use classes with Kenter we just start by defining a class so we can call class and name it anything you want I'm just gonna name it elder because we're not creating anything specific right now we're just learning about how to use them so name way anything you want at this point in the future you're gonna name a more descriptively if you're gonna build a calculator app you would name it class calculator maybe or calculator layout or whatever be just be descriptive so we start out by defining an initial eight and this an int function and this is just underscore underscore and knit underscore underscore and it's really how you start any sort of class in Python and then inside of here we have to pass self as you always do with classes and then we also have to pass something else we have to pass this main route window so you could call it route I guess but that's kind of confusing to have two routes going around so you might call it main or master or you know something like that so alright so inside of here I'm gonna use a frame we have really talked about frames with frames are just what they sound like they're you know boxes that hold things they're usually invisible you can make them visible by giving them borders but there's just a way to keep track of certain elements and kind of group them all together so I'll just create a frame here so my frame and we're gonna set this equal to it's a frame and instead of normally we would have passed root because this is our root window but now we're passing master because this is right here master is root because we're passing root into here as you'll see in just a minute when we initialize this thing so okay my frame frame master and let's just pack this on the screen for now and let's say inside of here we want to I don't know just create a button so now to create a button in the past we would have just named it my button equals you know that we would have put it in rude the text would have been something like you know click me and then if we wanted a command you know we would have put it right there now that's how we used to do it now instead of passing the name my button we have to call cell . my button because we're passing self into this initialization function and if you don't know an init function this is just the function that gets called every time your class runs it gets called automatically so it's just the normal thing you do with classes whenever you created any class they all have an init function right off the bat and it's just the first thing that gets done automatically when your class runs right so so we're passing self my button and instead of root we want master because now on inside of our class anytime we want to refer a root we refer to it as master because that's what we named it right here if you didn't name this main we would have put main here if you named it root we would put root here but whatever you named it right here that's what this that's what goes here so finally we want to put this on the screen so let's go self dot my button dot pack we'll just pack this for now and let's give this a pad Y of twenty or something just push it down a little bit okay so that's really all there is to it now we've got this clicker function so we should create that really quickly so just below here we're just gonna go define clicker and then we also want to pass self you always want to pass itself unto these things and then just do whatever you want to do I don't want to get into this so let's just print onto the screen look at you got that dot you clicked a button okay and this will just print that to the terminal so all right we're almost done now this is correct but we now have to call this class if we actually want to run it and to do that we just outside of the class anywhere ahead of the root that main loop thing here we just call it so we could call this anything we want let's just call it a for elder and then we just call the class elder now we want to pass in a root right we're passing in our root window right here that's how this it's passed in so the root gets passed into the class when we run it here master becomes root and then it just flows from there so okay let's go ahead and save this and head back over to our GUI let's run this and we have an error what did we do wrong a button ah this is a button all right that looks good and also here I noticed I've done something wrong here we're calling this clicker function we need to pass self dot clicker okay okay so let's go ahead and save that give this another try it's Monday morning you can tell all right so click me we could get nothing seems to have happen but when we close this down here on the terminal it says look at you you click the button and it has worked let's try this again without the self just to see what happens if save this come back over here run it again we get an error clicker is not defined so anytime you're calling functions inside of your class you've always got to call it a self a click and that's the same here right so we've got self my button solve my button passing self here any time you're gonna do stuff inside your classes now and it's all gonna be self that's sort of one of the main differences with doing classes versus doing you know as we've been doing up until now without using classes and then right down here obviously we're just initializing our class and this is how you initialize any kind of class in Python right you just give it a name and you've now created an object to class any object right and we can do object these things to it if we wanted to there's nothing really to do to it in this instance except for run it which is what we've done here so that's fun so this these are classes just very simple and it's it's kind of a nice way to you know structure your app using classes if you're used to using classes Kent herbs pretty simple and that's cool so in the next couple of videos we'll probably get into this in more detail because there's more stuff to learn with this right here we're just you know running this function and printing this to the screen which is not that interesting we're obviously going to want to do a more intricate things and so we'll start to look at how to do those type of things in coming videos so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm by a very cool code me calm t-shirt and check out cut Aamir Khan where you can use coupon code youtube one to get $30 off membership so you pay just $49 to access all my courses hundreds of videos and the PDFs of all my best-selling coding books doing over 85,000 students learning into code just like you my name is John elder from Kota be calm and we'll see in the next video
Info
Channel: Codemy.com
Views: 47,400
Rating: undefined out of 5
Keywords: tkinter class structure, tkinter classes, tkinter classes and methods, tkinter with classes python, tKinter Class, using classes in tkinter, using classes python tkinter, python classes tkinter, how to use a class with tkinter, tkinter classes python, python tkinter, python gui, tkinter tutorial
Id: Jl1xsH6MR1g
Channel Id: undefined
Length: 9min 19sec (559 seconds)
Published: Mon Mar 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.