An overview of the tkinter buttons (+using them with tkinter variables)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this section we are going to create some buttons specifically we are going to create this app here we have the normal button this one we have already seen then we have checkbox buttons and we have radio buttons how they work in detail I'll explain while I make this section but these are the main buttons we are going to work with so there are three major kinds of button we have a button a check button and a radio button and a really important thing here is that to use them properly you are going to need taken the variables there's stuff I have covered in the last section but well let's Jump Right In and let's have a look here I have a new python file and I already have some imports and I'm creating the basic window the last thing I need is I have to run the entire thing and this I do with window dot main Loop which means if I run the entire thing now we can see we have a basic window I do want to make two minor changes first of all I want to create a title and I call this one just buttons besides that I also want to set the geometry of this app there's no particular reason for it I just think it looks better I want to set the width to 600 and the height to 400. if I run this now we have a slightly larger window which means now we can work with the buttons and let's start with the most basic button this one we have already seen let me just create it again I want to have a button and this I get with ttk DOT button once again we need a master and this is going to be the window now I have used this named argument here quite a bit and you don't actually have to use that instead what you can do is just pass in window or whatever Master you want to have for this widget as the first argument in here the first argument is always going to be the master that way you don't have to write so much besides that for the button you need some text for the name of the button let's say a simple button finally we have to pack the button so we can see it and there we go we have a simple button that we can click on although right now since the button doesn't have any function attached to it it doesn't do anything that we can change quite easily and let me put the function right below the button so I keep everything organized I want to create a function let's call it button function in here we don't have any parameters and I just want to print e basic button this I now have to attach to this button via the command so in here I want to pass in the function if I run the entire thing now I can click on the button and we can see a basic button besides that instead of using a proper function here you could also use a Lambda function like so so Lambda and print and we have a basic button running this again once again I can click on the button and we get the same result once again if you don't understand Lambda functions definitely check out a dedicated video for it it's really important to understand for gui's I am going to use it quite a bit you might be wondering what if you have a function let me return to button function what if you have a function with parameters How Could You incorporate that because when we are calling the function here we can't call this function so we couldn't pass in arguments if you understand how functions work this should be quite easy I'm going to cover this in the next section in a bit more detail for now just think about it and maybe you can figure it out already it's not that difficult but well this is all we need for the basic button we have seen all of this already so there shouldn't be anything new so far although there's one more thing that we can do and that is the button can also have a text variable let me create one as a separate variable I once again want to have a TK and a string VAR don't forget to call it and I want to assign it let's call it button so string and this we can pass in here for the button string if I run the entire thing now we have an empty button the reason for that is that this button string is now overwriting the text and since there's nothing inside of the string VAR we have a button without any text so it looks like it's just a basic button that we can change by setting a basic value for the string bar I'm going to call this one a button with string VAR and let me fix the typo there we go if I run this now we have a button with stringvar and this is basically all you need for a basic button it really doesn't do that much more later on when we talk about layouts and styling there's going to be a bit more but for the basic button this is pretty much all you are ever going to need so with that we can come to the next kind of button and that is a check button this one let me create a basic check button I'm going to call it check this one we create with ttk and check button in here as always we're going to need the master which in my case is always the window then we have the text like the normal button I want to call this one check box one and that is all we need for now so let me pack this checkbox and see what we get there we have checkbox one and in here I have a checkbox I can click and unclick this also I click this quite fast away if I restart this by default we have this other kind of value which is I guess neutral besides that we have on and off but once we click on it we can't go back to the neutral position in another argument you can pass in here is command and this works like in the normal button meaning you could just add any kind of function you want in my case I'm going to go with Lambda and just print check button this function is going to be run every time we are pressing on this check button let me run this again and now anytime I am clicking on the checkbox we get check button which is pretty useful and now you might be wondering how could we get the value or in other words how do we know if this check button is on or off we couldn't do something like check dot get if I run this and click on the checkbox we get the check button object has no attribute get quite unfortunate so we couldn't use that one let me undo all of this and I just want to print the word check button to store and check the value of check we need a tkinter variable here I want to have a check VAR the value here is actually quite open-ended we could for example use a TK string VAR the one we have already seen and this we now have to connect to this checkbox and that we do I guess I could put all of this over multiple lines so it's a bit easier to read like so a checkbox doesn't have a text variable instead we have just a variable and this is going to work the same so in here I want to add my check VAR the reason why it's not called text variable is because this variable doesn't set the text of this checkbox instead the check bar is going to store the value if this box is ticked or not and that I can now check so whenever I'm clicking on this box I want to get my check VAR and now I can use get if I run the app now you can see we already have one difference and that difference is that this box right now is not neutral anymore instead it is empty or unticked and if I click on it we get one or we get zero and those values since we can only store string vars is going to be a string I guess I can demonstrate this the value we get returned I want to put inside of type and if I run this again click on the checkbox we're getting a string returned it only looks like we get zeros and ones but we are storing them inside of a string variable so they are being converted to a string although that doesn't have to be because we could instead of a string VAR use an INT VAR the result is going to be the same if I run this thing now and click on checkbox we still get zeros and ones however now if I use type on what we're getting returned once again what we're storing now are integer values and that should illustrate quite well that we are working with different data types we have an invar for the teak enter variable and we have a string VAR while those two are different the most basic thing you have to understand is that we are just storing basic data types of python inside of slightly more complicated containers that's all that's happening here you could also add in here a Boolean VAR and if I run this we have the same result except now we're getting Boolean values and this might be the data type that makes the most sense here so if I run the same thing again we're getting true or false depending if this thing is ticked or not and this is why it is really important to understand tick inter variables to work with buttons or the check button if you want to check the value you basically always need a tkinter variable for Simplicity for this one I want to keep on working with an integer variable this one let me run this again we always getting once or zero although we could change that because inside of this we have what is called an on value and we have an off value and those well I think they're quite self-explanatory on value is the value that's being returned when this value is turned on or when the box is checked this in our case should be a number because we are working with integer for the storage let's say the on value could be 10 and the off value could be 5. if I now run the entire thing again I can click on the checkbox and we get 10 and 5 depending if it's on or off or checked or unchecked and if you want to change the start value you would have to work inside of the intvar so in here if I set value to 10 and run it typing again now the Box by default is checked cool so this is working quite well which means now we can work on the Third Kind of button and that is going to be a radio button or rather radio buttons because you basically always want to use multiple I want to start by creating Radio 1 and this we get with ttk DOT radio button for this once again we need a master which is going to be the window and then we need text and this I want to call radio button one since I do want to have two radio buttons I'm going to duplicate this and change to 1 to a 2 both for the name and for the variable and now I just want to pack Radio 1 and Radio 2 so we can actually see them like so and if I run the entire thing now we have two radio buttons but now you can see something weird let me restart the entire thing so I can explain what I'm doing right now both radio button 1 and radio button 2 are unchecked but if I click on either of them all of a sudden both are being ticked so something weird is happening here this super important thing you have to understand about radio buttons is that each of the widgets has to have a value that is going to be just another named arguments so in here we have a value the value here could be basically anything let's say for the first button I want to have a radio 1 and for the other button I want to have the integer 2. now if I run this again I can click on each button and they don't trigger together anymore if you don't set a value the default value is going to be zero and if you have the same default value for every single radio button tkinder gets a bit confused you basically always have to set a custom value here now other than that you can use this radio button like the buttons we have seen already which means you could also add a command like so and this once again could be just a function or a Lambda function let me go with Lambda because all I want to do is print something and there's something I want to print is the value of this radio 1. and once again I want to put all of this over multiple lines so things are a tiny bit easier to read like so we once again have the problem that we couldn't use Radio 1 dot get if I run this and click on radio button one we get that the radio button object has no attribute get instead what we have to do is kind of the same thing we have done for the check variable we once again have to create another let me call this one a radio VAR we have to create a tkinter variable and for this one we have a bit of a problem because the value for this one is a string and the value for this one radio button 2 is an integer the best way for that I find is to use a string VAR and this one is going to convert any kind of integer into a string and that way you can work with it and this you have to set as the variable or this radio one button so radio VAR there we go and now you can use the radio VAR and get the value meaning if I print the entire thing and I click on ready button one we get radio 1. that is the value we specified in here for the value so far we basically have a check button what makes radio buttons different is that the idea is you have this takened a variable in multiple radio buttons let me add it for this button here radio button 2. I want to have a variable as well and this is going to be the same radio variable which means that this radio button and this radio button are both connected to this radio bar here which by default isn't particularly useful if I run the entire thing again I can click on one and we always get radio 1. where this system becomes really useful is when I get the radio VAR from somewhere else let's say all the way in this button function I want to print radio VAR and get the value if I now run the entire thing again I can click on radio button 1 and click on the button we get radiovar again and if I click on radio button 2 we get 2 or the value inside of radio button 2. and the really important thing here is if I reorganize everything a tiny bit the basic idea of a radio button is that only one button is ever activated meaning we could have multiple buttons and only one of them would ever be true and then the value is going to be stored inside of this ticket variable and we could get it for example with a button a checkbox is different to that because inside of a checkbox you can have multiple values that are ticked or unticked whereas for a radio button only one value is ever ticked I can actually demonstrate this as well if I wanted to create a second checkbox let me create one right below I want to have check two and let me be consistent with the naming here this check should be a one and a one here check 2 is going to be another ttk check button the master is going to be the window the text is going to be check box 2. and let's say for the command I want to have another Lambda function that for now it's just going to print test and all of this I also have to pack so check two dot pack if I run this again now for the checkbox we can have multiple values and each of those are essentially self-contained whereas for the radio button we only have one value ticked this is quite different although that being said if you really wanted to you could connect those two check buttons quite easily using the check VAR for example if I click on check bar instead of printing something I could also get check VAR and then set the value to the off value let's say a 5. this would be 5 in this case I can now run this and right now checkbox one is ticked but if I click on checkbox 2 checkbox one is not ticked anymore and I can re-tick it and click on checkbox two checkbox one will always be false that way you can connect these two buttons if you want to but by default they are not connected whereas for radio buttons they are always connected although the connection here happens via the T Kenta variable and then here sometimes you can get into minor problems for example if I set the value to 1 for both of them and run the typing again if I now click on one of them both are going to be ticked because they both have the same value what basically happens is ticket that checks what is inside of the string variable right now this could be a one and then it checks if this value is identical to the value inside of each widget if it is it's going to check this radio button if it is not it is not going to check it so if these values aren't unique you are going to have some slightly weird Behavior something you do want to keep in mind but other than that this is kind of all you need for the radio buttons so with that we have radio buttons we have check buttons and we have the basic buttons with that I want to do an exercise and then we are done and the exercise here is going to be a tiny bit more extensive let me copy and paste it in it's going to look like so I want you guys to create another check button and two radio buttons and they are going to be very connected for the radio button each radio button should have the value A or B if you click on either of the radio button you are printing the value of the check button the one you're going to create in a second and also if you take either radio button you're also unchecking the radio button so as soon as you click on the radio button you get the value of the checkbox and you uncheck the checkbox and then for the check button if you tick the check button you print the currently selected value for the radio value also you want to use a t Kanda Boolean value for the check button try to implement all of this yourself and see how far you get first of all I want to create the radius and let me call this exercise radius so it's a bit easier to read now in here I want to have exercise Radio 1 and I want to have exercise radio 2. both of those are going to be created in basically the same way I want to have ttk and radio button the master as always is going to be the window then we have text and in here let me call them radio and this is going to be radio a and radio B that way we have a bit of an indication if we're working with a or b to actually get that value we have to set well the value and this one is going to be either a or it is going to be B to store this value we need a tkinter variable let me call this the radio string and this one is going to be TK string VAR I'm going to use string bar because A and B are both going to be strings if they were 0 or true or false this would be a different kind of takened a variable Auto stringvar is the one you're probably going to use the most besides that I want to create a function that we can work with and this let me call it radio function in here no parameters and basically what I want to do when I'm clicking on either of these I first of all want to set a command so I'm connecting them with the radio function like so when I'm calling this function however I want to get the current value of the check button and I also want to untick the current check button the problem we have right now is we don't have a check button so we have to create that one first I want to create an exercise check and this is going to be ttk and check button in here we need a window the text is going to be the exercise check and this one is also going to need a variable and this one is going to need an on and off value and we don't have to set an on on off value because 0 and 1 is perfectly fine here and there's one thing I did forget because we are not placing anything on the window right now which I can do with exercise Radio 1 dot pack then exercise Radio 2 dot pack and then the exercise check dot pack and that is some atrocious spelling before I'm running this I want to add paths inside of the radio function so we're not cutting an error running this now we can see we have a few more things so something is definitely working we now just have to give it some proper functionality and the first thing we are going to need now is to actually add ticket variables so we can work with all of this the exercise radio already has a string variable although I didn't connect it to it this we can do all the way at the end we need a variable and a variable is going to be the radio string the one we created here and let me put the entire thing like so we have our data we have the widgets and we have the layout I think that system makes the most sense besides the radio string I also want to have a check and this is going to be a Boolean value because remember in the exercise we want to use a t content variable for booleans which means in here I want to have TK and Boolean VAR this Boolean VAR I now want to place inside of my exercise check and here I need a variable and this is going to be the check Boolean variable with that I can access the value inside of my radios and inside of my check buttons which means I can finally work on the radio function in here I want to get the value of the check button that is currently selected and I'm just going to print it so we're going to keep things simple I want to get my check Bool and I want to get the value and that is all I need for that part if I now run the entire thing again and I click i1 radio a on Radio b we are getting false and that seems correct because the exercise check is not checked right now if I do check it and click on either radio A or B we're getting true so this is looking pretty good next up I want to set the value of the check button to false that way it's not going to be checked this I do with checkpool and set and in here I want to pass in false if I run this again now I can click on the exercise check and if I click either on radio A or B it is going to be unchecked so that is going to cover the first part of the exercise for the next part we have to work on the check button and we only really have one bit left and that is that ticking the check button prints the value of the radio button value that is going to be so simple we can do all of this inside of a Lambda function so in here I want to have Lambda and this is going to print the radio string and I want to get the value the last thing I need is this should be command and this is going to be very hard to read let me put all of this over multiple lines like so I assume you have a much wider monitor than I do so you probably don't have to do this but in my case I want this to be visible on even smaller monitors so I'm a bit more constrained there we go this is a bit easier to read we have our exercise Radio 1 Radio 2 and the check and the check now has a Lambda function that prints the current value of the radio buttons so I can run the entire thing and now if I click on let's say radio a I have the exercise check and I can do this for B and we get B this is working pretty well and with that we have the different kinds of button at least the basic ones that you can work with in tkinder on top of that I think you have a pretty good understanding at this point about taking their variables I am going to keep on using them quite a bit so definitely make sure that you have at least a basic idea about how they work
Info
Channel: Atlas
Views: 6,657
Rating: undefined out of 5
Keywords:
Id: mPQdJDVtev0
Channel Id: undefined
Length: 26min 28sec (1588 seconds)
Published: Sat Dec 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.