2.6 PySimpleGUI 的创建 Menu

sg.ButtonMenusg.Menu 均可以创建菜单。直接看代码:

import PySimpleGUI as sg

# 预设
sg.change_look_and_feel('LightGreen')
sg.set_options(element_padding=(0, 0))
# 定义菜单选项
menu_def = [['File', ['Open', 'Save', 'Exit' ]],
            ['Edit', ['Paste', ['Special', 'Normal',], 'Undo'],]]

# 定义布局
layout = [[sg.Menu(menu_def, tearoff=False, pad=(20,1))],
          [sg.Text('您的输出:')],
          [sg.Output(size=(60,20))],  # print() 的显示结果
          [sg.ButtonMenu('ButtonMenu',key='-BMENU-', menu_def=menu_def[0])],]
# 定义 Window 
window = sg.Window("Windows-like program",
                       layout,
                       default_element_size=(12, 1),
                       grab_anywhere=True) # 非阻塞
# 事件循环
while True:
    event, values = window.read()
    print('Event = ', event)
    if event in (None, 'Exit'):
        break
    # ------ Process menu choices ------ #
    elif event == 'About...':
            window.disappear()
            sg.popup('About this program','Version 1.0',
                     'PySimpleGUI rocks...', grab_anywhere=True)
            window.reappear()
    elif event == 'Open':
            filename = sg.popup_get_file('file to open', no_window=True)
            print(filename)
    elif event == '-BMENU-':
        print('You selected from the button menu:', values['-BMENU-'])
window.close()

效果图见图1和图2:

图1 菜单栏
图2 按钮栏

下面我们也可以为这些菜单创建快捷方式:即在需要创建的菜单字符之前添加 & 即可,比如:['&File'],则可以通过 Alt + F 访问。有时候,我们还需要创建一个右键启动,只需要在 sg.Window 中传入参数 right_click_menu=... 即可。下面看看具体的代码实现:

import PySimpleGUI as sg

def second_window():

    layout = [[sg.Text('The second form is small \nHere to show that opening a window using a window works')],
              [sg.OK()]]
    window = sg.Window('Second Form', layout)
    event, values = window.read()
    window.close()
    
def test_menus():
    # 预设
    sg.change_look_and_feel('LightGreen')
    sg.set_options(element_padding=(0, 0))
    # 定义菜单选项
    menu_def = [['&File', ['&Open', '&Save', '&Properties', 'E&xit' ]],
                ['&Edit', ['&Paste', ['Special', 'Normal',], 'Undo'],],
                # '---' 分割菜单栏
                ['&Toolbar', ['---', 'Command &1', 'Command &2', '---', 'Command &3', 'Command &4']],
                ['&Help', '&About...'],]
    # 定义右键菜单
    right_click_menu = ['Unused', ['Right::_Right_', '!&Click', '&Menu', 'E&xit', 'Properties']]
    # 定义布局
    layout = [[sg.Menu(menu_def, tearoff=False, pad=(20,1))],
              [sg.Text('Click right on me to see right click menu')],
              [sg.Output(size=(60,20))],  # print() 的显示结果
              [sg.ButtonMenu('ButtonMenu',key='-BMENU-', menu_def=menu_def[0])],]
    # 定义 Window 
    window = sg.Window("Windows-like program",layout,
                       default_element_size=(12, 1),
                       grab_anywhere=True, # 非阻塞
                       right_click_menu=right_click_menu, # 添加右键菜单
                       default_button_element_size=(12, 1)
                      ) 
    # 事件循环
    while True:
        event, values = window.read()
        print('Event = ', event)
        if event in (None, 'Exit'):
            break
        # ------ Process menu choices ------ #
        elif event == 'About...':
                window.disappear() # 隐藏窗口
                sg.popup('About this program','Version 1.0',
                         'PySimpleGUI rocks...', grab_anywhere=True)
                window.reappear()  # 重现窗口
        elif event == 'Open':
                filename = sg.popup_get_file('file to open', no_window=True)
                print(filename)
        elif event == '-BMENU-':
            print('You selected from the button menu:', values['-BMENU-'])
        elif event == 'Properties':
                second_window()
    window.close()
 
test_menus()

显示效果图见图3与图4:

图3 右键菜单
图4 Alt+快捷方式

在代码中的 'Right::_Right_' 是指定该菜单的 Key 的方式。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容