简体   繁体   English

使用 pysimplegui 更新 window 布局,按钮排成一行

[英]Update window layout with buttons in row with pysimplegui

I'm trying to do an app with gui using PySimpleGUI.我正在尝试使用 PySimpleGUI 做一个带有 gui 的应用程序。

I need to show different buttons on the window once I click "Confirm" button.单击“确认”按钮后,我需要在 window 上显示不同的按钮。

I've done this using buttons with "Visibility false" in the first layout, when i click the Confirm button the script changes the visibilty of the buttons that were initially invisibile.我在第一个布局中使用带有“Visibility false”的按钮完成了此操作,当我单击“确认”按钮时,脚本会更改最初不可见的按钮的可见性。

The problem is that the button are visible but they are not in line but in the same column.问题是按钮是可见的,但它们不在同一列中而是在同一列中。

This is the first window:这是第一个 window:

这是第一个窗口:

This is the how the window updated should look:这是更新后的 window 的外观:

这是更新后的窗口的外观

This is how the updated window look instead:这就是更新后的 window 的外观:

这就是更新后的窗口的外观

Here's my code:这是我的代码:

import PySimpleGUI as sg

sg.theme('DarkAmber')
layout = [
                [sg.Text('\n\nText sample', key = '_text_', visible = True)],
                [sg.Text('Second sample: ', key = '_text2_'), sg.InputText(key='_IN_', size=(10, 1))],
                [sg.Text()],

                
                [sg.Button('Confirm', key = '_CONFIRM_', visible=True), 
                sg.Button('1', key ='_1_', visible=False), 
                sg.Button('2', key = '_2_', visible=False), 
                sg.Button('3', key = '_3_',  visible=False), 
                sg.Cancel('Exit', key = '_EXIT_')],

            ]

window = sg.Window('Window', layout)


while True:            
    event, values = window.read()
    if event in (sg.WIN_CLOSED, '_EXIT_'):
        break

    elif '_CONFIRM_' in event:
        window['_text_'].Update(visible = False)
        window['_text2_'].Update('Second text updated')
        

        window['_EXIT_'].Update(visible = False)
        window['_CONFIRM_'].Update(visible = False)

        window['_1_'].Update(visible = True)
        window['_2_'].Update(visible = True)
        window['_3_'].Update(visible = True)
        window['_EXIT_'].Update(visible = True)

Do you know how to show properly the buttons in the same row?您知道如何正确显示同一行中的按钮吗? Thanks!谢谢!

sg.pin is an element provided into a layout so that when it's made invisible and visible again, it will be in the correct place. sg.pin是提供到布局中的元素,因此当它再次变得不可见和可见时,它将位于正确的位置。 Otherwise it will be placed at the end of its containing window/column.否则它将被放置在其包含窗口/列的末尾。 Replace sg.Button(...) by sg.pin(sg.button(...)) when layout.布局时将sg.Button(...)替换为sg.pin(sg.button(...))

在此处输入图像描述 在此处输入图像描述

import PySimpleGUI as sg

sg.theme('DarkAmber')
layout = [
                [sg.pin(sg.Text('\n\nText sample', key = '_text_', visible = True))],
                [sg.Text('Second sample: ', key = '_text2_'), sg.InputText(key='_IN_', size=(10, 1))],
                [sg.Text()],

                [sg.Button('Confirm', key = '_CONFIRM_', visible=True),
                sg.pin(sg.Button('1', key = '_1_', visible=False)),
                sg.pin(sg.Button('2', key = '_2_', visible=False)),
                sg.pin(sg.Button('3', key = '_3_', visible=False)),
                sg.Cancel('Exit', key = '_EXIT_')],

            ]

window = sg.Window('Window', layout)


while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, '_EXIT_'):
        break

    elif '_CONFIRM_' in event:
        window['_text_'].Update(visible = False)
        window['_text2_'].Update('Second text updated')


        window['_EXIT_'].Update(visible = False)
        window['_CONFIRM_'].Update(visible = False)

        window['_1_'].Update(visible = True)
        window['_2_'].Update(visible = True)
        window['_3_'].Update(visible = True)
        window['_EXIT_'].Update(visible = True)

window.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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