简体   繁体   中英

Issue when re-using layout in PySimpleGUI

I have a pop-up with a user input field using 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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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