使用 pysimplegui 在行中更新 window 布局

Update window layout with buttons in row with pysimplegui

我正在尝试使用 PySimpleGUI 开发带有图形用户界面的应用程序。

单击“确认”按钮后,我需要在 window 上显示不同的按钮。

我在第一个布局中使用“Visibility false”按钮完成了此操作,当我单击“确认”按钮时,脚本会更改最初不可见的按钮的可见性。

问题是按钮是可见的,但它们不在一行而是在同一列。

这是第一个window:

这是 window 更新后的样子:

这是更新后的 window 的样子:

这是我的代码:

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)

你知道如何正确显示同一行的按钮吗?谢谢!

sg.pin 是提供给布局的元素,因此当它变得不可见和再次可见时,它将位于正确的位置。否则它将被放置在其包含 window/column 的末尾。布局时将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()