Web Analytics Made Easy - Statcounter
Skip to content

Menu

Menu call reference

Menus are discussed in detail in the Menus section.

If it's important that your menubar match your window's color theme, then take a look at the MenubarCustom element.

The Menu element adds a menubar to your window. This particular feature of the GUI is usually provided by the operating system rather than the GUI framework. How menubars work on a Mac is very different than how they work on Windows or Linux. Notice that your PySimpleGUI window has a menubar that looks identical to other programs such as Notepad.

menu_def = [['&File', ['!&Open', '&Save::savekey', '---', '&Properties', 'E&xit']], ['&Help', ['&About...']]]

layout = [[sg.Menu(menu_def)],
          [sg.Text('Your window!', size=(30, 5))]]

Events

The Menu element causes an event to be returned when an item is chosen. Rather than the Menu element's key being returned as the event, the menu item that the user chose will be the event. The event will contain the menu item including the "key" portion of the menu item.

Values Dictionary

When a Menu element generates an event, the Values Dictionary will contain the same information as the event.

As discussed in the section on Menus, the Menu Data Structure defines your menu. The Menu element uses the entire Menu Data Structure whereas Button Menus and Right-click menus only use a portion.

There is one simple pattern/rule with these data structures:

  • If a string is followed by a list, then the string is the start of a sub-menu or cascading menu.

Notice for the Menu element definitions, each list is a string followed by a list. The idea was to use the same rule for both items on a menubar and for cascading menus. Using the example from above, reformatting it so that you can more easily see that there are 2 lists inside the main list:

menu_def = [['&File', ['!&Open', '&Save::savekey', '---', '&Properties', 'E&xit']],
            ['&Help', ['&About...']]]

In this example we have 2 lists in the definition. Each of those lists begins with a string:

  • 'File'
  • 'Help'

Which makes 2 items on the menubar:

image

After the string is a list which represents a list of the items in the menu. If any of those items is a string followed by a list, then a cascading menu is created at that point in the menu.