A menu bar

The top menu bar of an application usually just contains other menus, such as file and edit. These are known as "cascades" in Tkinter, and are essentially a menu inside a menu. This may be confusing at first, so let's begin with a very simple example to demonstrate the difference between a menu and a cascade.

Create a new Python file called menu.py and add the following code:

import tkinter as tk

win = tk.Tk()
win.geometry('400x300')

lab = tk.Label(win, text="Demo application")

menu = tk.Menu(win)

After importing Tkinter and creating a main window and Label, we make our first Menu widget. As with a lot of widgets, the first argument needed is the master, or parent, in which the widget will be drawn. We draw this menu in our main window as expected.

The menu won't actually do anything until we add some commands to it:

menu.add_command(label='Change Label Text', command=lambda: lab.configure(text='Menu Item Clicked'))
menu.add_command(label='Change Window Size', command=lambda: win.geometry('600x600'))

We create two commands to sit in this menu using the add_command method. This method takes two keyword arguments (in this case) called label and command. The label argument is the text to display (which the user will click on). The command argument is  a function to be called when the menu item is clicked on, much like with a Button widget.

For our demo application we have two menu commands. The first of which says Change Label Text and when clicked will update the existing Label widget to say Menu Item Clicked. The second command says Change Window Size and will call the geometry method of our main window to increase its size.

Now that we have a Menu widget with some commands we need to draw it inside its parent. We use the configure method to achieve this:

win.configure(menu=menu)

A Tk widget, among others, can take a menu keyword argument, which tells it to draw a menu bar along the top. We use this argument to place our Menu widget into our main window. This is instead of using a geometry manager, such as pack, to handle inserting the widget.

Speaking of which, let's add our Label and finish off the demo script:

lab.pack(padx=50, pady=50)

win.mainloop()

Run this file and you should see a small window, which now contains a menu bar at the top:

Give each option a click and see it execute the command assigned to it.

This is how commands within a menu work, in a nutshell. Now let's check out cascades and how they differ.

In the same menu.py file we are going to add a cascade to the beginning of our menu. A cascade can be thought of as a submenu, since it will also contain a Menu widget. Add this code between creating your menu variable and your calls to add_command:

cascade = tk.Menu(win)

cascade.add_command(label='Change Label color', command=lambda: lab.configure(fg="blue4"))
cascade.add_command(label='Change Label Highlight', command=lambda: lab.configure(bg="yellow"))

menu.add_cascade(label="Label colors", menu=cascade)

Another Menu widget is created, this time called cascade. We will be placing this menu inside our other Menu widget as a submenu.

As before, we add two commands to our cascade that will alter the styling of our Label using a lambda.

In order to place this menu into another the add_cascade method is used. Much like with add_command, this takes only keyword arguments. The label argument is the text that the user will see on the clickable area of the menu. The menu argument specifies which Menu widget we want to add as our cascade. We give this our cascade object so that this will become a submenu of our menu object.

Leave the rest of the code as it was and run this version of the file. You should see a new option as the left-most item in the window's menu bar. When you click this you will see the two commands we added to our cascade appear. Give each a try and see that it changes our Label just like the commands in the outer menu:

This is all there is to creating a menu bar along the top of an application. We simply need an instance of a Menu widget for each submenu, then one more to act as the top menu bar itself.

With the menu bar covered, let's have a look at adding a context menu that appears when the user right-clicks within the window.

..................Content has been hidden....................

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