简体   繁体   中英

PySimpleGui change whole layout based menu clicked

I've made simple GUI for my python script and I don't want to use Tabs or TabGroups tabs github example

I added own menu and items, but not found any solutions to change layout based on menu item clicks. So is there any quick and nice solution to change layout based menu item click?

menu_def = ['Layout', ['Layout1::LAYOUT1_KEY', 'Layout2::LAYOUT2_KEY', 'Layout3::LAYOUT3_KEY']],['About',['About::ABOUT_KEY']]

Here is the example layouts and window call.

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()

Try to use Column element for your layout1, layout2 in same row, but only one layout with option visible=True , all others with option visible=False , also record which layout is visible now.

When click which layout in menu, Set visible layout to be invisible and set which layout you click to be visible and record this layout visible now.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM