Creating Buttons With TKinter - Python Tkinter GUI Tutorial #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys my name is John elder from Cody me calm and in this video I'm going to teach you how to create buttons with kinder and python alright guys my name is John elder from Cody me calm and like I said in this video we're gonna learn all about buttons so it doesn't really matter what kind of program you're building what kind of graphical user interface chances are you're gonna need a button for something probably lots of buttons so that's what we're going to look at in this video but before we get started if you like this video and want to see more like it be sure to smash the like button below subscribe to the channel and be sure to check out Cody me comm where I have dozens of courses hundreds of videos that teach you to code use coupon code youtube to get $22 off membership that's all my courses videos and books for a one-time fee of just $27 which is completely insane so in the last video we looked at the grid system how to position things around so I'm gonna come up here to our grid - I'm just gonna rename this and let's just call this buttons dot hi okay so let's play around with this a little bit let's just get rid of all of this yeah let's get rid of this - okay so to create a button in Kenter is pretty simple a button like everything in Kenter is a widget so we want to create a button widget so let's just call this one my button I guess and we just call a button and this function right now inside of here we need to tell it you know where we want this so we want it in a root and then what text right so let's say click me right so like everything in kinter once we define the thing now we have to actually put it up on the screen so let's just go my button not packed we'll just pack it in there just for example purposes so go ahead and save this and let's head over here to our terminal and remember I'm in c4 slash GUI it's just the directory I created you can you know save these anywhere you want so let's go Python and then buttons dot py and pull this over my other monitor and we see there it is click me now when we click on this thing it doesn't do anything because we haven't told it to do anything yet so before we do that I want to show you one more kind of cool thing right up here when you're defining this thing you can pull a state so state equals let's call this disabled right so if we say this come back here and here load and drag this guy over you can see now the button is disabled it won't even click right it's sort of grayed out a little bit so that's kind of cool one other thing we can look at before we learn how to actually have these things do something let's get rid of this we can sort of change the size of these so we can pad X and we can pad Y first let's look at pad X so let's say 50 right sort of an arbitrary thing come back here and reload this guy now let's pull this over and you can see now the button is wider right X think of like an X Y axis right so the X is sort of horizontal and the Y is vertical Y is up-and-down and X is left and right right so that's cool so we can change this to any size we want I just put 50 next let's go had y equals 50 just see what this looks like save this and run it pull this over and say see now we have a big square button hard to tell see if I click it there we go right so that's kind of neat right um let's see just for fun let's get rid of the pad X see what this looks like save it and run it so okay so it's a tall skinny button I don't know why you would ever want to do this say the truth you're probably never gonna want to resize a button anyway but you may just for you know aesthetic reasons sometimes need to make your button a little bigger or smaller so that's how you do that so we can change the size with pad X and pad Y we can disable and enable with the state now we need this to actually do something how do we get a button to do something in kinder well let's close this it's actually very very easy what we do is create a function any old function to do anything you want so let's say we're creating a function here so we go define my click and then it's just a function like any other function in Python and inside of here you can have it do absolutely anything you want so let's create remember our my label and let's call this label oops there we go and we want it to be in route and we want the text to say I could type there we go look I clicked button Hey okay so we've created a label now we need to put it on the screen so let's just go mind me my label dot pack and I know grid system is better but this is just for example and we just want to throw this up on the screen anyway so that's good to go so as you know with a function it doesn't get executed until it gets called and that's not a kinder thing that's just a Python thing it's really just an object-oriented programming thing so you know the program starts here it executes this line it executes this line it sees this and it doesn't actually execute it it reads it into memory so then later on if you want to run it you can call it and it'll run but it doesn't actually do anything and we can prove that by saving this real quick and we can run this and let's pull this guy back over and so you know when we click here nothing actually happens because we're not telling it to execute yet so to actually tell it's execute is really really simple we just go let's go command equals and then name the function my click okay so if we save this come back here and run this guy one more time zoom click this oh look I clicked a button appears actually if we keep clicking it keeps putting it up on the screen that's a discussion for another time but yeah it really doesn't matter what you want the button to do right if you're if you built a form right and you've got a submit button to submit the form via email to something ok same thing we're just gonna create a function and then in the function you'll write different code to do whatever you want to do but just that simple so it's this command and now here's something to sort of keep in mind also let's pull this guy back up again real quick and close it now normally when you call a function with Python you call it with those things like right here we're calling this PAC function right and we put the parentheses when we did the grid stuff we were going my button grid and then given that right so any time in Python when you call a function you call the parentheses like that it's just normal but here you don't if we save this and run it I don't think this is gonna work might no it doesn't let's pull this over when the program start it ran it automatically it's so it already has it up there and when we click the button it doesn't execute it again like it did in the last little bit we did so you're gonna forget and do that because what you're doing here is calling this function and whenever you call a function as a program or as a Python programmer especially you know you need to put these little parentheses so if you do you'll get an area you won't get an error it just won't work right as we just saw so just remember to keep those off of there when you're using the command buttons and that's pretty much it now I think you can also change the color of buttons it's been a while since I've done this because who changes the colors but I think it's F G foreground color I could be wrong so let's try let's change this to blue I'm not even sure this is gonna work I don't even remember but since we're playing with buttons here no so let's see what we need to do is put this in parenthesis maybe let's save this and run it it should have been more prepared and actually tried this yes so here we have now the text is blue right so that's kind of cool now that's FG for foreground color we can also do I guess be G for background color and let's go red I don't know this is going to be pretty ugly I think yeah pull this over now so now the button itself becomes red you know I don't know why you would want to do that but if you did you could do that in and the colors you you can use just you know any old color I'm not sure let's see let's play around here I don't know if we can do like hex color codes that is so I'm gonna close this program first and then run it again come on yeah that looks like that worked is that a white button look white to you the background color I think that's white yeah it's the only hex color code I can remember offhand what's another one ffffff oh boy I don't know what is zero zero zero zero zero zero is that black I can't remember I think I might be black yeah so that answers that yeah you can use hex color codes in there so that's handy so if you're a web developer and you're you know doing CSS and you're used to using hex color codes or whatever that is I guess it's hex color codes those color codes right there with the little hash tag in front of them you can use those in this FG and BG so that's pretty much all I want to talk about with buttons buttons are pretty simple and straightforward as we you know continue on with this tutorial we'll use buttons a lot in the coming videos and will use a lot more complicated functions than this my click function that we used right here obviously this is just for example purposes to show you how to call a function from a button but it's pretty simple so that's all for this video if you liked it be sure to smash the like button below subscribe to the channel and check out Cottam e-comm where you can use coupon code youtube to get $22 off membership so you pay just $27 to access all of my courses hundreds of videos and the PDFs of all my best-selling coding books join over 50,000 students learning to code just like you my name is John elder from khou-tv calm and we'll see you in the next video
Info
Channel: Codemy.com
Views: 144,570
Rating: undefined out of 5
Keywords: python, tkinter, gui, python tkinter, tkinter python, python gui, tkinter gui, gui python, graphical user interfaces python, tkinter grid system, grid system tkinter, python grid system, build graphical user interfaces with python, how to build graphical user interfaces with python, how to python tkinter, buttons with tKinter, tKinter Buttons, tkinter python buttons
Id: yuuDJ3-EdNQ
Channel Id: undefined
Length: 11min 0sec (660 seconds)
Published: Mon Jan 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.