如何根据 PySimple GUI 中的按钮点击显示不同的布局? (持续 window 循环)

How to display different layouts based on button clicks in PySimple GUI? (Persistent window loop)

我想知道是否有一种方法可以根据 PySimple GUI 中的按钮点击来管理不同的布局。我刚刚开始使用这个框架,我想找到导航菜单的最佳方式。不必使用不同的布局,但这只是让我印象深刻的最直观的方式。

I was thinking like maybe having a list of layouts that get pushed to the top when a certain submenu button is selected.

layouts = [layout1, layout2, layout3, layout4]

或者启动程序:

layout = layout1

选择子菜单后,只需将状态更改为:

layout = layout2

因此,例如具有 'Main Menu' 布局,并在单击按钮时将不同的布局或 'submenu' 带到 'front',以便整个程序在一个中运行单身window。也许看起来像这样:

主菜单

按钮 1

按钮 2

按钮 3

单击按钮 1 时,window 保持打开状态,但显示更改为子菜单 1。

在 PySimpleGui 文档中,我正在使用推荐用于构建某些程序的持久性 window 循环:

import PySimpleGUI as sg

sg.theme('BluePurple')

layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')],
      [sg.Input(key='-IN-')],
      [sg.Button('Show'), sg.Button('Exit')]]

window = sg.Window('Pattern 2B', layout)

while True:  # Event Loop
    event, values = window.read()
    print(event, values)
    if event in  (None, 'Exit'):
        break
    if event == 'Show':
        # Update the "output" text element to be the value of "input" element
        window['-OUTPUT-'].update(values['-IN-'])

window.close()

我愿意完全改变结构,但我想在开始构建功能之前先降低菜单导航。

使用 Google 我发现没有办法将一种布局替换为另一种布局。

PySimpleGUI Issues: Updating form layouts #117,

您只能在一个布局中创建所有元素,其中一些元素会隐藏并再次显示:

Reddit r/PySimpleGUIIs it possible to update the layout of a column / frame?


tkinter 中,有一种流行的方法是使用 Frame 对小部件进行分组,然后使用 hide/show 框架来更改 window 中的内容。似乎 PySimpleGUI 也有 Frame 但我没有像我预期的那样工作的例子。

其实你们很亲近。

这就是我认为您要查找的内容。您需要做的是将您的布局添加到 Column 元素。然后使所有列不可见,除了您希望可见的列。

这是个好主意。

import PySimpleGUI as sg

# ----------- Create the 3 layouts this Window will display -----------
layout1 = [[sg.Text('This is layout 1 - It is all Checkboxes')],
           *[[sg.CB(f'Checkbox {i}')] for i in range(5)]]

layout2 = [[sg.Text('This is layout 2')],
           [sg.Input(key='-IN-')],
           [sg.Input(key='-IN2-')]]

layout3 = [[sg.Text('This is layout 3 - It is all Radio Buttons')],
           *[[sg.R(f'Radio {i}', 1)] for i in range(8)]]

# ----------- Create actual layout using Columns and a row of Buttons
layout = [[sg.Column(layout1, key='-COL1-'), sg.Column(layout2, visible=False, key='-COL2-'), sg.Column(layout3, visible=False, key='-COL3-')],
          [sg.Button('Cycle Layout'), sg.Button('1'), sg.Button('2'), sg.Button('3'), sg.Button('Exit')]]

window = sg.Window('Swapping the contents of a window', layout)

layout = 1  # The currently visible layout
while True:
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        break
    if event == 'Cycle Layout':
        window[f'-COL{layout}-'].update(visible=False)
        layout = layout + 1 if layout < 3 else 1
        window[f'-COL{layout}-'].update(visible=True)
    elif event in '123':
        window[f'-COL{layout}-'].update(visible=False)
        layout = int(event)
        window[f'-COL{layout}-'].update(visible=True)
window.close()

[编辑] 一个名为 "Demo_Column_Elem_Swap_Entire_Window.py" 的新演示程序已添加到 PySimpleGUI GitHub。您可以通过访问 Trinket.

在浏览器中查看代码和 运行