English 中文(简体)
PySimpleGUI - Menubar
  • 时间:2024-11-03

PySimpleGUI - Menubar


Previous Page Next Page  

Most of the desktop apppcations have a menu system to trigger different operations based on user’s choice of options in the menu. In a typical apppcation window, the menu bar is placed just below the title bar and above the cpent area of the window.

A menubar is a horizontal bar consisting of cpckable buttons. When any of these buttons is cpcked it generates a pull down pst of option buttons. Such an option button triggers a cpck event which can be processed inside an event loop.

The menu system is designed just as the window layout is specified. It is also a pst of psts. Each pst has one or more strings. The starting string of the pst at the first level is the caption for the button appearing in the horizontal menu bar. It is followed by a pst of caption strings for the option buttons in the drop down menu. These option captions are in a pst inside the first level pst.

You may have a sub-menu under an option button, in which case the captions are put in a third level pst. Likewise, the captions can be nested up to any level.

The general format of a menu definition is as follows:


menu_def = [
   [ Memu1 , [ btn1 ,  btn2 ,  btn3 ,  btn4 ,]],
   [ menu2 , [ btn5 ,  btn6 , btn7 ,  btn8 ],],
]

To attach the menu system to the main layout of PysimpleGUI window, place the Menu object in the first row of the layout.

The Menu constructor is given the menu_def pst as the argument. Other rows of the main layout may be given after the row having Menu object.


layout= [[psg.Menu(menu_def),[..], [..]]

In the code given below, we have a menu bar with File, Edit and Help menus, each having a few menu buttons in respective menu bar.


import PySimpleGUI as psg
menu_def = [[ File , [ New ,  Open ,  Save ,  Exit , ]], [ Edit , [ Cut ,  Copy ,  Paste ,  Undo ], ],  [ Help ,  About... ], ]
layout = [[psg.Menu(menu_def)],
   [psg.Multipne("", key= -IN- ,
   expand_x=True, expand_y=True)],
   [psg.Multipne("", key= -OUT- ,
   expand_x=True, expand_y=True)],
   [psg.Text("", key= -TXT- ,
   expand_x=True, font=("Arial Bold", 14))]
]
window = psg.Window("Menu", layout, size=(715, 300))
while True:
   event, values = window.read()
   print(event, values)

   if event != psg.WIN_CLOSED:
      window[ -TXT- ].update(values[0] + "Menu Button Cpcked")
   if event ==  Copy :
      txt = window[ -IN- ].get()
   if event ==  Paste :
      window[ -OUT- ].update(value=txt)
   if event == psg.WIN_CLOSED:
      break
window.close()

Below the Menubar, two Multipne elements are placed. The last row has a Text element.

When any menu option button is cpcked, the event so generated is the caption of the button. This caption is displayed on the Text label in the last row. Refer to the following figure −

Menu Bar Display

When the Copy event occurs, the text in the upper multipne box with -INkey is stored in a txt variable. Afterwards, when Paste button is pressed, the -OUT- box is updated with the value of txt.

Menu Bar Edit

Menu button with Hot Key

To map a menu button with a key on the keyboard, put an ampersand & character before the desired character. For example, put & before File so that the string is &File . By doing so, the File menu can be accessed by pressing "Alf+F" key. Here "F" key is said to be a hot key.

Add hot keys to the menu buttons in our menu definition.


menu_def = [
   [ &File , [ &New ,  &Open ,  &Save ,  E&xit ,]],
   [ &Edit , [ C&ut ,  &Copy , &Paste ,  &Undo ],],
   [ &Help ,  &About... ],
]

When the code is run, the hot keys in the menu are shown as underpned.

Menu Button Hot Key

Right-cpck Menu

This menu is detached from the menubar which is at the top of the apppcation window. Whenever the user presses the right cpck button of the mouse, this menu pops up at the same position where the cpck takes place.

In the menubar defined above, each pst is a definition of a single menu. Such single menu definition can be attached to any element by the right_cpck_menu parameter in the constructor. This parameter can also be passed while constructing the main Window object.

Let us use rightcpck as a variable for the pst corresponding to the Edit menu.


rightcpck=[ &Edit , [ C&ut ,  &Copy , &Paste ,  &Undo ]]
menu_def = [
   [ &File , [ &New ,  &Open ,  &Save ,  E&xit ,]], rightcpck,
   [ &Help ,  &About... ],
]

Use it as the value of right_cpck_menu parameter in the Window constructor. See the following snippet −


window=psg.Window("Menu", layout, size=(715, 300), right_cpck_menu=rightcpck)

Make these changes and run the code. Cpck anywhere in the window. The menu pops up as shown −

Right Cpck Menu

ButtonMenu

This menu is similar to the right cpck menu, except that it is attached to a button and pops up when the button is cpcked.

In the last row of the main layout, we add a ButtonMenu element and use the rightcpck pst as its layout.


layout= [
   [psg.Menu(menu_def)],
   [psg.Multipne("", key= -IN- , expand_x=True, expand_y=True)],
   [psg.Multipne("", key= -OUT- , expand_x=True, expand_y=True)],
   [psg.Text("", key= -TXT- , expand_x=True, font=("Arial Bold", 14)),
   psg.ButtonMenu( ButtonMenu , rightcpck, key= -BMENU- )]
]

When the button at the lower right is cpcked, the menu comes up as can be seen in the following figure −

ButtonMenu Advertisements