Pysimplegui 简明教程

PySimpleGUI - Menubar

大多数桌面应用程序都具有菜单系统,以根据用户在菜单中选择的选项触发不同的操作。在典型的应用程序窗口中,菜单栏位于标题栏的正下方,并且处于窗口客户端区域的上方。

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

菜单栏是一个由可点击按钮组成的水平栏。当单击其中任何按钮时,它将生成下拉选项按钮列表。此类选项按钮触发可在事件循环中处理的单击事件。

A menubar is a horizontal bar consisting of clickable buttons. When any of these buttons is clicked it generates a pull down list of option buttons. Such an option button triggers a click 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 list of lists. Each list has one or more strings. The starting string of the list at the first level is the caption for the button appearing in the horizontal menu bar. It is followed by a list of caption strings for the option buttons in the drop down menu. These option captions are in a list inside the first level list.

在选项按钮下方可以有子菜单,在这种情况下,标题将被放入第三级列表中。同样,标题可以嵌套到任何级别。

You may have a sub-menu under an option button, in which case the captions are put in a third level list. 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'],],
]

要将菜单系统附加到 PysimpleGUI 窗口的主布局,请将 Menu 对象放置在布局的第一行。

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

Menu 构造函数以 menu_def 列表作为参数提供。菜单对象的所在行后面可能会出现主布局的其他行。

The Menu constructor is given the menu_def list 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.Multiline("", key='-IN-',
   expand_x=True, expand_y=True)],
   [psg.Multiline("", 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 Clicked")
   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 Multiline elements are placed. The last row has a Text element.

单击任何菜单选项按钮时,生成的事件是按钮的标题。此标题显示在最后一行中的 Text 标签上。参考下图 -

When any menu option button is clicked, 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

当 Copy 事件发生时,带有 -INkey 的上部多行方框中的文本存储在 txt 变量中。此后,当按下 Paste 按钮时,-OUT- 框会使用 txt 的值进行更新。

When the Copy event occurs, the text in the upper multiline 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

要在菜单按钮和键盘上的键之间建立映射,请在所需的字符前放置一个与符号 &。例如,在 File 前面加上 &,使字符串为“&File”。通过此操作,可以通过按 "Alf+F" 键来访问 File 菜单。此处“F”键被称为热键。

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 underlined.

hot key

Right-click Menu

此菜单从位于应用程序窗口顶部的菜单栏中分离出来。每当用户按下鼠标右键按钮时,此菜单都会在单击发生的位置弹出。

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

在上面定义的菜单栏中,每个列表都是单个菜单的定义。这样的单个菜单定义可以通过构造函数中的 right_click_menu 参数附加到任何元素。在构造主 Window 对象时,也可以传递此参数。

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

让我们使用 rightclick 作为与 Edit 菜单相对应的列表的变量。

Let us use rightclick as a variable for the list corresponding to the Edit menu.

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

在 Window 构造函数中将其用作 right_click_menu 参数的值。请参阅以下代码段 -

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

window=psg.Window("Menu", layout, size=(715, 300), right_click_menu=rightclick)

进行这些更改并运行代码。单击窗口中的任意位置。菜单会弹出,如下所示 -

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

right click menu

ButtonMenu

此菜单类似于右键菜单,只是它附加到一个按钮,并在单击该按钮时弹出。

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

在主布局的最后一行中,我们添加一个 ButtonMenu 元素,并使用 rightclick 列表作为其布局。

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

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

单击右下角的按钮时,菜单会弹出,如下所示 -

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

button menu