2.6 PySimpleGUI 的創(chuàng)建 Menu

sg.ButtonMenusg.Menu 均可以創(chuàng)建菜單。直接看代碼:

import PySimpleGUI as sg

# 預(yù)設(shè)
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() 的顯示結(jié)果
          [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) # 非阻塞
# 事件循環(huán)
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 按鈕欄

下面我們也可以為這些菜單創(chuàng)建快捷方式:即在需要創(chuàng)建的菜單字符之前添加 & 即可,比如:['&File'],則可以通過 Alt + F 訪問。有時候,我們還需要創(chuàng)建一個右鍵啟動,只需要在 sg.Window 中傳入?yún)?shù) right_click_menu=... 即可。下面看看具體的代碼實現(xiàn):

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():
    # 預(yù)設(shè)
    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() 的顯示結(jié)果
              [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)
                      ) 
    # 事件循環(huán)
    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()  # 重現(xiàn)窗口
        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 的方式。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容