Binding Dropdown Menus and Combo Boxes - Python Tkinter GUI Tutorial #45

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys John elder here from khou-tv common in this video I'm going to show you how to bind drop-down boxes and combo boxes with kinder and python I guess like I said in this video we're gonna look at binding drop-down boxes and combo boxes but before we get started if you like this video I want to see more like it be sure to smash the like button below subscribe to the channel give me a thumbs up for the YouTube algorithm and check out Cottam e-comm where i have dozens of courses with hundreds of videos that teach you to code use coupon code youtube line to get $30 off membership that's all my courses videos and books for one-time fee just $49 which is insanely cheap ok in the last video we looked at binding things to widgets so you don't have to necessarily click a button in order to activate a thing you can do a mouse event you keyboard event any sort of event we looked at in the last video if you missed that video check the playlist in the comments below in this video I want to look at drop-down boxes and doing the same thing but with drop-down boxes so you've got a drop-down box and you select something from that box and then you want some action to take place based on what you clicked on the box without having to press a button in order to select it right so that's all we're gonna look at in this video and there's a couple of different drop-down box things in kinter we've looked at in past videos there's the the regular sort of option menu drop-down box and then there's the combo box which is a little fancier looking we'll look at both of those in this videos so I've got this code it's I saved to just drop bind dot pi and this is just our basic starter code that we've been using forever so let's just start out by creating a drop-down box and options menu widget so we start out by creating a Python list right and we just want to sort of define the things that we want to appear in the drop-down box so let's just go Monday and Tuesday and Wednesday and Thursday almost done Friday the best day and Saturday yeah hey let's put Sunday into why not we're crazy okay so we've got our options right so I'm gonna call this options because that makes more sense to me okay so now let's just create a variable is that we can get the state of our drop-down menu and this is just normal option menu widget coding we've looked at all this in the past so I'm just gonna call it clicked and let's set this equal to a string VAR capital V ah there we go and it's a function and now we can set which one we want to appear when this thing loads so we can go clicked dot set and we want to select options and then which one of these do we want let's start with Monday so Monday is the 0 with item in this list so we put 0 here if we wanted this to be Tuesday that's the first item because you know Python lists start at 0 so we have 0 1 go back so we have 0 1 2 3 4 5 & 6 so whichever one you want to show up in the little menu by default you put that there so we're setting that all right so let's let's define this and let's just call this I don't know drop it's a drop-down box or drop down menu and this is an option menu if you forgotten and we want to put this in root and we want to set it as this thing right here so clicked then we want our options to appear all right so now we can go drop pack and let's give this a pad Y of 20 just to smush it down a little bit okay so let's go ahead and say this so drop bind dot PI we want to run this and when we do we see here's our little box and here's Monday and if we click on it our box appears and we can select whichever one we want and nothing happens yet because we haven't done anything well in the past if we wanted to make you know make a selection and then do something based on that we would have a little button next to here that we would then click right so we can do that real quick if we want let's just go my button just to refresh our memory and it's a button and it's in route and the text we want to say select from list and then we want to give this a command of I don't know selected all right so then up here we can create a function called selected and then we just want say a label and that's a label and it's a route and the texts just say what let's just say clicked dot get and then let's just pack this on the screen here okay so we can come back down here and let's just my button dot pack this on the screen and good enough so let's save this and come back here and run it so we have let's pick Saturday and if we select it from list boom Saturday box pop pops up we remember how to do this we've looked at this before so how do we make this say for instance Saturday pop up without having to click this button that's the whole point of this video and it's surprisingly easy in fact let's just get rid of this my button I'll just comment it out completely now we just come to our option menu widget and right after options we can just set a command equals and it can be whatever we want we call this selected so let's just call selected and now all we have to do is pass this event into our function right here alright so there it is so this should whatever we click on it should pack it onto the screen as a label so let's give this a try real quick so we pick Friday boom Friday pops up we pick Saturday boom Saturday pops up we're not clicking any buttons we're just selecting it from the drop-down box and boom there it is now that's all there is to it now if you want to do something with that well you can you know you could just use clicked get so if you wanted to do some logic or something so we could go you know if click got get equals I don't know Friday then my label let's put the text as what yay it's Friday all right else we could just put the label as whatever it is so let's save this and give this a try so if we click here and we click Friday yeah it's Friday I'll did I forget to yeah we need to comment this out all right so let's save this try it again close this and run it again all right so Friday yeah it's Friday Monday just puts out Monday so whatever you want to do whatever actions you want to take based on what's clicked here Sunday try Friday again yeah it's Friday you could do that with simple logic just by calling on your clicked doc that get method which is just down here when we set this as a string variable and originally set it as 0 the first one on the list whenever you click it that zero changes to whatever you clicked on so those are drop-down boxes very very easy now we've also in the past looked at combo boxes and those look more like sort of webform drop-down boxes and if you remember to use those we have to import a little thing here so we can go from tkinter import ttk oops there we go so let's just come down here and make one real quick and let's call this I don't know my combo and set that equal to 80 TK dot combo box and we want this in route and we want the value to equal let's call this options we want to use the same options here and then we can go my combo dot current and we'll set that to zero Monday now this one is actually a little bit more like the stuff we did yesterday we have to actually bind this so let's just go ahead and do that let's go my combo dot bind and to bind this remember we have to do a thing and then the action right so our thing we want to do is doop doop like that and then it's combo box selected in the action we want to take and let's go ahead and run our selected function which is up here or let's go combo'd let's create a new one combo II come boy let's just go combo click so okay that's a new function let's come up here and create that function so let's go define combo click let's do some spacing here and again we can just grab this same thing here instead of this text let's put my combo dot get we can just get it okay and then we've already packed this under the screen so that should work let's come down here and we have to actually put this on the screen so my combo dot pack and okay so call combo click we come up to combo click we have to pass in the event of course okay so that should work let's go ahead and save this and run it and we have now this much nicer combo drop-down box and if we click Wednesday for instance boom Wednesday pops up and the same exact thing as the last one we can use logic if we want so you know we can do this whole thing here so let me just grab this and say well actually let's just copy this whole thing and bring it down here into our combo click thing and instead of clicked that get of course now this is my combo get otherwise down here it's also my combo get okay so if we save this and run it we should have the same sort of logical weird the same sort of logic thing so if we pick Friday yeah it's Friday if we pick Tuesday it just says Tuesday if we pick Sunday just says Sunday we could pick Friday again yeah it's Friday again so pretty simple and pretty easy and you absolutely do not need buttons in order to fire these combo boxes and so that's cool so that's all for this video if you like to be sure to smash the like button below subscribe to the channel give me a thumbs up to thumbs up for the YouTube algorithm and check-out go to me.com or you can use coupon code youtube one to get $30 off membership saint pages $49 to access all my courses over 40 courses hundreds of videos and the PDF versions of all my best-selling coding books shown over 85,000 students learning to code just like you my name is John elder from Cody we common we'll see in the next video
Info
Channel: Codemy.com
Views: 63,985
Rating: undefined out of 5
Keywords: tkinter drop down list, tkinter drop down list selection, tkinter drop down menus, tkinter optionmenu, tkinter optionmenu update list, tkinter optionmenu config, tkinter combobox get selected value, tkinter combobox example, tkinter combobox python 3, tkinter combobox binding, tkinter combobox, tkinter optionmenu binding, tkinter drop down box binding, tkinter drop down box event, python gui
Id: OPUSBBD2OJw
Channel Id: undefined
Length: 11min 58sec (718 seconds)
Published: Wed Mar 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.