Using menus in tkinter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's talk about menus in tkinter you are creating a menu with TK menu be aware here we are using TK not ttk but other than that the widget is fairly straightforward however there's one thing you have to understand to create more complex menus and that is that you are always using multiple TK menus and you Nest them sometimes you can Nest quite a few of them at the same time and this can get a bit confusing and a bit complex let me give you an example if you want to create a simple menu you will place a TK menu inside of another TK menu and then this sticker menu becomes an option now what does that mean let's say we have our normal application the entire window is the blue frame here inside of there we have TK menu this is the main menu this would be just a normal top bar like you've seen in basically any other application inside of that we are placing another item and this would be the TK menu this would now be one of the entries inside of this menu but other than that both of these are just TK menu widgets the red menu though can now have a couple of extra options meaning here we actually have items we can pick from I hope that makes sense you could make all of this even more complex because you could plays for a sub menu and menu inside of a menu inside of another menu for this example I kept the colors constant the main menu is the yellow one inside of there we have a red Sub menu or one item inside of the main menu inside of that menu we have another menu this would be one of the items in here inside of there we could have other entries like so and we could even have other menus in here this would still work the system overall can get quite complicated but well once you understand the basic logic this really isn't too difficult as long as you understand the nesting principle let me actually demonstrate what we are going to make it's going to look like this we have a menu with a couple of entries and inside of one we have a sub menu on top of that we also have a checkbox in here that we can check and uncheck finally there is a separate menu button it's basically a button with a menu although it works like a menu and that is all we need let's have a look at this in code once again I have a few lines of code ready if I execute all of this we have a plane ticket window it doesn't do very much in here I want to create a menu this we create with TK and menu like any other widget this is going to need a parent which in my case is going to be the window on top of that this widget I want to store inside of a variable that I'm going to call menu while this menu is just another widget we are not using the pack method on it instead to turn it into the menu of the window you would use window configure and then you would set the menu to the menu if I run this now we can't see anything the reason for that is that this menu by default is invisible we can only see the children inside of it since we don't have any children right now we can't see anything but that we can change quite easily let's start by creating one sub menu I want to create TK menu again although now the parent is going to be the menu the one we just created this one up here this I want to store as well in a variable I'm going to call this one the file menu the name is basically arbitrary with this we have an entry but if I run the code we still can't see anything the reason for that is that we need one more line of code we need to get our menu and then the method add Cascade for this one we need two arguments first of all we need what is called the label this is the text you are going to see I'm going to call this one file besides that we need a menu entry this is the file menu we just created and now we should be seeing something there we go in the top left we now have file although if I click on it we just see something empty that is doing some weird stuff there are two reasons why this is happening the more important one is that in this file menu doesn't have any entries right now to give it some we have a couple of different options the one you are going to use the most is the method add command this one like add Cascade has a label let me call this one new and then we have a command this one could be any function in my case I'm going to set a Lambda function that is going to print new file like so and now if I run this again I can click on file and we have a new entry if I click on that we have a new file print out that's pretty good although if I click again on file you can see we have this separator thing if I click on that we are creating another window that is kind of weird although the menu still works but this isn't the desired Behavior to fix that when we are creating the file menu we have to add one more argument and that is called tear off by default this one is true and that means you can tear away the menu and have it as a separate window I do not want this Behavior so I'm going to set this to false running this again now inside a file we only have the menu entry this is looking like a proper menu and let's go through the entire thing again just to make sure all of this makes sense we have the entire main window up here that is the entire window that one should be fairly obvious at this point inside of that thing we have a menu this is the entire menu bar up here for that you need to menu widget itself and on top of that you have to run window configure and set the menu as the menu inside of that we have right now one menu or one sub menu that is called file menu this is the file we have created here for that we need another TK menu and we have to get the original menu and add Cascade then inside of this file menu let me clean this up a bit inside of this file menu we can add different entries with for example add command there are a couple more entries but at command is the one you probably want to use if you got this far the worst part about understanding menus is basically over because now we could run this at command a second time besides new let's say I also want to have open which is going to print open file let's run all of this again inside of the file menu I now have new and open let's still both do a command oh this is working quite well and I guess besides at command there are a couple more methods you could use a really simple one is called add separator don't forget to call it if you run this one we now have a separator between the menus this one doesn't really do anything but it's a nice way to separate the menu entries to get all of the entries this website here is pretty good because if you scroll down you have a couple of basic options and below that you have all of the methods you could use for example we have used at command and we have used add separator there's also add radio button a check button those are quite obvious I think and you can also delete and do quite a few more things but I want to keep it simple if you want to go into more depth check this out in your own time I want to create another Sub menu this I want to store in a separate variable let me call it the help menu this again we create with TK menu once again menu is going to be the parent and tear off I want to set to false like the one we have created up here this Sub menu I now have to attach to the main menu which means I want to get my main menu then I want to add Cascade the label here is going to be help and then the menu is going to be the help menu I can run this now and there we have a second menu item this one if I click on it doesn't do anything because there are no entries but file still works just as before this one I want to have one item let's copy the add command from the file menu except this one should be for the help menu in here for example the label could be help entry and this one just prints help it doesn't really matter what it is and this we have already seen a couple of times so if I run this we now have a second menu besides file menu this one is pretty straightforward let's do something a bit more interesting for this help menu I now want to add a check button this is going to work basically like the normal check button that you have seen in the buttons a couple of sections ago although first of all we need a label and this label I just called check since we're working with a check button in here we can set an on value that I want to set to on and we have an off value that I want to set to off these two values we have to store somewhere for that I want to use a tkint variable I'm going to call this one help check string value the value here is going to be TK and a string VAR this variable I now want to set for this check button so variable is going to be the help check string that should be all we need let me run this and if I click on help now we have to check entry I can click on it and now we have this entry checked I can click on it again and it disappears cool once we have that we can always get the value from inside of this tkinder variable for example inside of this add command instead of printing help I want to get help check string and get the value let's try all of this inside of help I still have the check entry I can click on it and it still works and now if I click on help entry we get either on or if I unselect the check I get off so this is working pretty good with that we have created some basic menus and once again the main difficulty here is the nesting that we always Place one menu inside of another menu but they are the same widget besides that we can also create a menu button this is a separate widget that is called ttk and menu button this as always is going to need a master in my case this is going to be the window and we need a text like any other button I'm going to call this one the menu button on top of that this needs to be stored in a variable menu button is a good name here I think for this button we need the normal pack method because this one functions well like a button menu button and pack and now we have a menu button although if I click on it nothing happens right now because we don't have any entries that being said once you get this far you can just add TK menus to it let me create one TK menu [Music] in here the parent is going to be the menu button and tear off is also going to be false this I want to store let me call it button Sub menu once we have that we basically want to do what we have done here we want to set the sub menu to the parent menu via configure which means I want to get my menu button I want to run configure and here I want to set the menu to my button Sub menu let's try this one now and now if I click on the button we still can't see anything the reason for that now is that this button submenu doesn't have any entries although that we can change very easily all we need is the button Sub menu I want to add a command for this we need a label with entry one or whatever you want to add and we have a command that is going to be Lambda in my case that is going to print test one if I now fix the typo and run this this should be working now we have entry one I can click on it and we also get entry one besides that we could also add a check button for this one we can just leave it like so I guess I can call it check one I can run this now and now we have a check button in here that I can click on works like any other menu it's kind of similar compared to the menus you've seen before but not that much the main difference is that now you have a menu button and for this one you have to configure the menu to be the proper menu oh and what I forgot this configure you could simplify a tiny bit this is what we have seen earlier besides this line you could get the menu button in here you could get the menu and the value of this menu could be this button Sub menu the result would be the same if I run it we still have the same outcome choose whichever you like more it's really up to you both are equivalent we are nearly done although I want to do an exercise this exercise I want to place before we are setting the menu to the window I want you guys to add another menu item to the main menu like file menu and help menu you should create another menu the one difficulty for this one is that this new menu you're going to create should have one sub menu a menu inside of the menu you are going to create for that you want to read this website the one I just talked about I want to start by creating an exercise menu this is going to be TK and menu in here that parent is going to be the main menu and tear off I want to set to false just to make sure that we have one entry in here I want to get this exercise menu and add a command for the label I just want to have exercise test one this is actually all we need you don't have to specify a command if you don't want to or don't need to finally to see this menu we have to get the main menu and we have to add Cascade like we have done here and here we need a label this one I called exercise the menu is going to be the exercise menu with that I can run the entire thing and now we have the exercise menu with exercise test one and I can see I have a typo this should be exercise like so with this we have covered the first part of the exercise the next one is a bit more interesting I want to add a sub menu to this Sub menu meaning we have a sub Sub menu this Sub menu I want to store another variable I'm going to call it exercise Sub menu although this one once again is just going to be another TK menu with menu being the parent and tier of being false the way you have to think about it is that this exercise tab menu we want to add to the exercise menu like we've added the exercise menu to the main menu which we have done with ADD Cascade which means I want to get my exercise menu and add Cascade once again we need a label let's call it more stuff besides that we also need a menu this is going to be the exercise Sub menu now if I run this inside of exercise we have more stuff and this one doesn't do anything right now because it has no entries but that we can change very easily for this exercise submenu I want to add a command like I have done for the exercise menu which means I can copy the exercise menu at command and change this to exercise Sub menu with a new label some more stuff now I can run this and if I now click on exercise more stuff we have a sub Sub menu this could then be literally anything once again the nesting here is the complicated bit but all we have really done is we have the main menu all the way on the top this is where everything else goes in inside of this menu we are adding one exercise menu for that we are creating a TK menu widget and we are using add Cascade on the parent menu to create this menu item although this at Cascade we can do on any menu item for example on this exercise menu we have added another Sub menu and I'm fully aware that this can be kind of confusing definitely go for this slowly and make sure you understand every level of nesting although with that we have finished the menus
Info
Channel: Atlas
Views: 12,212
Rating: undefined out of 5
Keywords:
Id: aODNQnc4Ebc
Channel Id: undefined
Length: 18min 59sec (1139 seconds)
Published: Mon Dec 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.