如何在PySimple GUI中基于按钮单击显示不同的布局? (永久窗口循环) [英] How to display different layouts based on button clicks in PySimple GUI? (Persistent window loop)

查看:1262
本文介绍了如何在PySimple GUI中基于按钮单击显示不同的布局? (永久窗口循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am wondering if there is a way that I can manage different layouts based on button clicks in PySimple GUI. I am just starting off using this framework and I want to find the best way of navigating menus. Doesn't have to be using different layouts but this just struck me of the most intuitive way of doing it.

我想象也许具有当选择某个子菜单按钮被推到顶部布局的列表.

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]

或者也许用以下命令启动程序:

Or maybe start the program with:

layout = layout1

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

And when a submenu is selected just change the state to:

layout = layout2

例如,具有主菜单"布局,然后单击按钮,将另一布局或子菜单"带到前部",以便整个程序在一个窗口中运行.也许看起来像这样:

So for example having a 'Main Menu' layout, and upon a button click, bringing a different layout, or 'submenu', to the 'front' so that the entire program runs in one single window. Maybe looking something like this:

主菜单

按钮1

按钮2

按钮3

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

When Button 1 is clicked, the window stays open, but the display changes to Submenu 1.

我从PySimpleGui文档中使用了建议用于构造某些程序的持久窗口循环:

From the PySimpleGui Docs, I am using the persistent window loop that it is recommended for structuring some programs:

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

我愿意完全改变结构,但是我想在开始构建功能之前先关闭菜单导航.

I am open to changing the structure entirely but I wanted to get the menu navigation down before I start building the functionality.

  • 使用PySimpleGUI == 4.14.1

推荐答案

您实际上非常亲近.

这就是我认为您要寻找的.您需要做的是将布局添加到Column元素.然后,使所有列均不可见,除了您想要的列.

Here is what I think you're looking for. What you need to do is add your layouts to Column elements. Then make all columns invisible except for the one you want visible.

这是个好主意.

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

新的演示程序已添加到PySimpleGUI GitHub,名为"Demo_Column_Elem_Swap_Entire_Window.py".您可以通过访问来查看代码并在浏览器中运行它.小饰品.

A new Demo Program was added to the PySimpleGUI GitHub named "Demo_Column_Elem_Swap_Entire_Window.py". You can see the code and run it in your browser by visiting Trinket.

这篇关于如何在PySimple GUI中基于按钮单击显示不同的布局? (永久窗口循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆