简体   繁体   English

在 PySimpleGUI 中重用布局时出现的问题

[英]Issue when re-using layout in PySimpleGUI

I have a pop-up with a user input field using PySimpleGUI.我有一个使用 PySimpleGUI 的带有用户输入字段的弹出窗口。 If the user types the wrong input I want to show some text beneath the input field saying that they need to type it in again.如果用户输入错误的输入,我想在输入字段下方显示一些文本,说明他们需要再次输入。 My idea this far is the code below:我的想法到目前为止是下面的代码:

def start_pop_up(self, wrong_answer=False):
    sg.theme('DarkAmber')
    if wrong_answer:
        layout = wrong_answer
    else:
        layout = initial

    window = sg.Window('Please chose start settings', layout)
    event, values = window.read()

    if values['user_input']:
        if test_user_input:
            # Set some parameters accordingly .....
            pass
        else:
            window.close()
            self.start_pop_up(True)

    window.close()

Where my layouts looks like this:我的布局如下所示:

initial = [[sg.Text('User input: ', size=(15, 1)), sg.InputText(size=(60, 1), key='user_input')],
           [sg.Submit()]]

wrong_answer = [[sg.Text('User input: ', size=(15, 1)), sg.InputText(size=(60, 1), key='user_input')],
                [sg.Text('Wrong input, please input the correct answer.', text_color='red')],
                [sg.Submit()]]

If I input the wrong format then the correct window shows up.如果我输入了错误的格式,则会显示正确的窗口。 But if I try to put in the wrong answer again then I get this error:但是,如果我再次尝试输入错误的答案,则会出现此错误:

How can I solve this?我该如何解决这个问题?

Problem solved, but it is a strange behaviour which might be a bug.问题解决了,但这是一个奇怪的行为,可能是一个错误。

Instead of first creating the layouts as variables I put them immediately in the layout if/else statements like this:我没有首先将布局创建为变量,而是将它们立即放入布局 if/else 语句中,如下所示:

if wrong_answer:
    layout = [[sg.Text('User input: ', size=(15, 1)), sg.InputText(size=(60, 1), key='user_input')],
              [sg.Text('Wrong input, please input the correct answer.', text_color='red')],
              [sg.Submit()]]
else:
    layout = [[sg.Text('User input: ', size=(15, 1)), sg.InputText(size=(60, 1), key='user_input')],
              [sg.Submit()]]

And now it worked correctly.现在它工作正常。 Seems like PySimpleGUI needs to "re-initialize" the layouts by printing them out like this.似乎 PySimpleGUI 需要通过像这样打印出来来“重新初始化”布局。

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

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