How it works...

First, we call the tkinter constructor of the Menu class. Then, we assign the newly created menu to our main GUI window. This, in fact, becomes the menu bar.

We save a reference to it in the instance variable named menu_bar.

Next, we create a menu and add a menu item to it. We then add a second menu item to the menu. The add_cascade() method aligns the menu items one below the other, in a vertical layout.

Then, we add a separator line between the two menu items. This is generally used to group related menu items and separate them from less related items (hence, the name).

Finally, we disable the tearoff dashed line to make our menu look much better.

Without disabling this default feature, the user can tear off the menu from the main window. I find this capability of little value. Feel free to play around with it by double-clicking the dashed line (before disabling this feature). If you are using a Mac, this feature might not be enabled, so you do not have to worry about it at all.

Check the following GUI:

We then add a second menu to the menu bar. We can keep on adding menus through this technique.

Next, we create a function to quit our GUI application cleanly. This is the recommended Pythonic way to end the main event loop.

We bind the function we created to the menu item, using the tkinter's command property. Whenever we want our menu items to actually do something, we have to bind each of them to a function.

We are using a recommended Python naming convention by preceding our quit function with one single underscore, to indicate that this is a private function not to be called by the clients of our code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.206.25