The full Raspberry Pi Workshop in step-by-step format can be found here. TkInter menu bars and new windows are useful tools for your GUI design. We'll explore some tips and tricks in this video.
If you're planning on making a more complicated GUI or you just want it to look really polished, something that we're all used to seeing are menu bars. Menu bars are these bars at the top that conveniently arrange various options into sub menus for us.
I have a demonstration running over here. This is a simple GUI and all it's doing is showing the menu bar. So you can see we've got the familiar file and that has open, save, save as. It's got this nice horizontal separator which is a nice visual feature and then quit. We've got something called command and in that we could we could launch various different kind of sub GUIs so that we can have this this menu launch other windows. One for irrigation manager, dog walking and calculating, that's fine. And of course you know we could have a help with an about and a how to to give us a bit of a manual on how to use the interface.
So at the moment all of these commands they're all doing the same thing. If I choose dog walker for instance it just opens up this placeholder window which is running a test pattern, a bit of example code. But you can see that we've been able to launch another GUI window from our main GUI and that's an important thing. If your project grows and becomes more complicated it might be practical to have a small command centre which you can launch various subtasks from.
Going back to the home automation example from before you could have in command you could have you know the irrigation manager for water in the garden and that could have a complicated GUI that comes up that runs some timers, some manual switching. The same is true if you then had an air conditioner one and a lighting menu as well.
So let's have a look at how this is created. We've got the standard imports and the standard GUI definition. We've got something called placeholder. That's just the window that pops up for us. I'll come back to that later. The only object really that we're making is menu bar and a couple of other menus.
So menu bar we're making as a menu object, if that makes sense. And when we do that it automatically puts the menu bar at the top of the window for us. Then we need to fill the bar with menu options. So what we do there is we create say the file menu. Here we have file and that contains open, save, save as a bar and quit. And we can see that we create a file menu object which is itself a menu object. But rather than load it into win like we did for menu bar, we're loading it into menu bar itself. Menu objects or menu widgets can contain other menu widgets.
We have an option here. We're setting tear off equal to zero. That's something that would allow us to pull that menu away if we so wish. And then you can see from file menu we're running the add command function and that's just allowing us to create the options within that menu like open, save, save as. So each of those commands we can give a label, that's the text, and of course we can attach to it some command. You can see that all of the commands have been set to placeholder. So every option of this menu is going to run the same function which is just you know this is just a neat template perhaps. You could duplicate this into many other functions and write some more useful code there.
This is where we insert that nice visual separator between save as and quit. And once we're done creating these menu items we have to actually add them to the menu bar. So that's created the file menu and stacked up all the options within it. But then we have to actually make that file menu appear on the menu bar and we do that with add cascade. So that add cascade command essentially just looks for the first vacant spot and puts the menu there. For instance if we grabbed that add cascade command, cut it, and pasted it elsewhere in the code, it would appear in a different spot on the menu bar. After the add cascade for the command menu, we've got file command help. But now that I've rearranged the order that those add cascade commands have been called, if I rerun the script we can see we have command file help. So the order that these add cascade commands run is important for deciding how that menu is going to look.
The rest of the script is really just duplicating the same thing as the file menu. We just have a command menu and a help menu and we're just adding commands to that and then cascading them.
This placeholder text, to make that window appear, sorry this placeholder function, every option we choose is going to call the same function which is placeholder. To actually make that window appear we create an object called top level. So a top level is just a window. You could think of the main GUI essentially as a top level object. So anytime you want a new window that's a new top level. And of course you can treat a top level just like we have in every other video so far where you can give it a title, you can create widgets within it and pack them within it.
Here I'm actually creating a canvas widget and that allows you to do some basic drawing. So I've placed the canvas in the pop-up which is this pop-up window and just say in the background setting the size and then just drawing a simple shape in that. So this is the example code for the canvas but you can see that if you had a very complicated command centre style GUI you can quickly jump to the various important tasks that are within each menu.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.