[英]PySimpleGui change whole layout based menu clicked
我為我的 python 腳本制作了簡單的 GUI,我不想使用 Tabs 或 TabGroups 選項卡 github 示例
我添加了自己的菜單和項目,但沒有找到任何根據菜單項點擊更改布局的解決方案。 那么是否有任何快速而好的解決方案來更改基於菜單項單擊的布局?
menu_def = ['Layout', ['Layout1::LAYOUT1_KEY', 'Layout2::LAYOUT2_KEY', 'Layout3::LAYOUT3_KEY']],['About',['About::ABOUT_KEY']]
這是示例布局和 window 調用。
layout1_layout = [
[sg.Text("Layout 3 text")]
]
layout2_layout = [
[sg.Text("Layout 2 text.")]
]
window = sg.Window("Example form", layout, resizable=True, finalize=True, margins=(250,100)).read()
嘗試在同一行中為您的布局 1、布局 2 使用 Column 元素,但只有一個布局帶有選項visible=True
,所有其他布局帶有選項visible=False
,同時記錄現在可見的布局。
當點擊菜單中的哪個布局時,將可見的布局設置為不可見,並將點擊的布局設置為可見,並記錄此布局可見。
import PySimpleGUI as sg
menu_def = [['Layout', ['Layout1', 'Layout2']]]
font = ('Courier New', 11)
sg.set_options(font=font)
layout1 = [[sg.Text("Layout 1 text")]]
layout2 = [[sg.Text("Layout 2 text")]]
layout = [
[sg.Menu(menu_def, key='MENU')],
[sg.Column(layout1, key='COL1'),
sg.Column(layout2, key='COL2', visible=False)],
]
window = sg.Window("Title", layout, finalize=True)
col, col1, col2 = 1, window['COL1'], window['COL2']
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
print(event, values)
if event == 'Layout1' and col != 1:
col = 1
col1.update(visible=True)
col2.update(visible=False)
elif event == 'Layout2' and col != 2:
col = 2
col2.update(visible=True)
col1.update(visible=False)
window.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.