【问题标题】:Update window layout with buttons in row with pysimplegui使用 pysimplegui 在行中使用按钮更新窗口布局
【发布时间】:2021-06-13 15:04:11
【问题描述】:

我正在尝试使用 PySimpleGUI 做一个带有 gui 的应用程序。

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

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

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

这是第一个窗口:

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

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

这是我的代码:

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)

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

【问题讨论】:

    标签: python user-interface tkinter pysimplegui


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 2012-01-09
      相关资源
      最近更新 更多