Python GUI radiobuttons 🔘

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's your bro here hope you're doing well and in this video i'm going to be teaching you guys how we can create radio buttons in python so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running okay everybody let's create some radio buttons now these are similar to check boxes but you can only select one from any single grouping so let's pretend that i'm buying you lunch and you can only select one item from a menu you can select either a slice of pizza a hamburger or a hot dog so let's begin by creating a list i will call this list food food equals pizza hamburger hot dog and let's create a window window equals tk followed by window dot main loop so to create a radio button we'll need a name so i'll call this radio button radio button equals radio button and we're going to be adding this to our window and we're going to put this within a for loop so we're going to iterate through all of the items within our list so let's create a for loop so we'll save for index in range length of food so this is going to iterate once through all of the elements within our list so when we run this it's going to create three radio buttons for us because we're instantiating one radio button for each item in the list now what i'm going to do is set the text equal to our list of food at a certain index so when we first run this for loop it's going to be zero for the index then one and then two and then so on and so forth for each item within our list and then we just need to pack this so radio button dot pack and then let's take a look at this alright so here are all of the radio buttons that we have currently they're all selected though so we'll need to link these within a single grouping the next thing we'll add is a variable and we'll set this equal to x and we'll need to declare this after we create an instance of our window so x equals int var so this will hold an integer object so these are all within the same grouping however if we try to run this again and select one of these options they all become selected so we're going to need to give each of these individual radio buttons their own value because right now they're all sharing the same value so we can add value equals whatever the index is currently so the first item within our list of food is going to have an index value of zero then one then two so let's try that again and you can see by default it's zero so pizza is already selected then we can select either hamburger or hot dog but we can only select one so that's kind of what we want then so do you guys usually know how i call the config function after each change that i make for an object well that's because i like to add comments just for teaching purposes for what each change does however it might be easier for you instead of calling the config function a bunch of times just to list all of the changes within the parentheses when you create an instance of the object that you're working with however just for teaching purposes i'm going to put this all within separate lines that allows me to add some comments so i think i'm going to start doing that from now on so what this does is that this adds text to radio buttons and this groups radio buttons together if they share the same variable so if you wanted a completely different grouping of radio buttons you can give them a different variable like y and what this line does is that this assigns each radio button a different value all right so let's make some cosmetic changes to our radio buttons so right now they are centered let's anchor these to the west so we'll place this within the pack function so anchor equals w alternatively you can just say capital w without the quotes this would also work too so now they're all lined up let's add some padding so i'm going to add pad x and i'll set this to 25 and this adds padding on x axis let's also change the font so we'll say font equals maybe i'll pick impact for a font style and set the font size to 50. and now we can actually read these because the font is large enough now let's add some images so let's create some photo images you'll want to be sure to create these after you create your instance of your window so let's create a pizza image first pizza image equals photo image file equals the file path or the file name and my file is pizza.png and it kind of looks like this it's just the pizza emoji so pizza dot png and then i'll do the same thing for hamburger and hot dog so this will be hamburger image file equals hamburger.png and then hotdog image and this is what the other emojis look like and then let's create a list of the photo images that we have i'll call this food images equals then the names of these images pizza image hamburger image and hot dog image so then we can set the image for each radio button image equals the name of the list food images followed by the index that we're currently on within our for loop so this adds image to radio button and let's test this cool here's our images now if you want some images as well as text you'll have to use compound so let's add that next so i'm just going to add a comma followed by compound equals let's say left so this will add the image to the left of the text adds image and text i'll just say left side so this will display both an image and text so with these radio buttons you can eliminate these circle indicators and there's actually a way to do that so what we'll add if you want to get rid of those is indicator on equals zero so this will i should say eliminate circle indicators so it's going to change these to these push buttons however they are going to be of uneven size you can set the width if you want to so we'll do that so we'll say width equals what about 75 i'm not sure if that's a good size or not okay that's way too small let's try 375. so yeah you can have some push buttons if you want to if you don't like the circle indicators so this sets width of radio buttons now let's actually set a function to uh be called when we click one of these buttons so let's call this function maybe order like we're ordering one of these items so d e f we'll call this order like we're ordering something and what we'll do is just a few if statements because i'm feeling lazy so we'll just say if x that's the value that's going to be stored because each of these radio buttons is grouped together by the variable x if x dot get is equal to zero that's our first index for pizza what we'll display is print you oops you ordered pizza then we'll just add else if x dot get is equal to one we will print you ordered a hamburger and then else if two you ordered a hot dog else print huh so there's probably a more efficient way to write this however i was just feeling lazy and this is probably the best way to keep this simple and easy to understand so let's try this now oh however we need to set the command for our radio buttons so command equals then the function name which is order and make sure you do not add the parentheses so don't do this do that and this will set command of radio button to function all right let's test this so if we click on pizza it says you ordered pizza you ordered a hamburger and you ordered a hot dog okay everybody so that's the basics of radio buttons if you would like a copy of all this code i'll post all of this in the comments down below but yeah that's the basics of radio buttons in python hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 3,229
Rating: undefined out of 5
Keywords: tkinter gui tutorial, python gui tkinter, tkinter buttons, tkinter button tutorial, tkinter buttons tutorial, tkinter how to add buttons python, python buttons gui, tkinter menu commands, tkinter radio button tutorial, tkinter radiobutton get value, tkinter radio button example, tkinter radiobutton python
Id: 5IVvj99yxsc
Channel Id: undefined
Length: 12min 21sec (741 seconds)
Published: Thu Aug 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.