简体   繁体   中英

Why can't I seem to enter these if statements inside a while loop when using PySimpleGUI?

I am writing a python program to calculate throughput and model costs for me. I am fairly green in Python and completely new to PySimpleGUI with this project. In the code below, I have no issue entering the Exit and Throughput if statements, but for some reason pressing Correct or Append does not cause the program to enter those corresponding if statements. Can anyone see why this is happening?

Note that I didn't include the code for the user-defined functions as I didn't think it was relevant since I'm not even able to enter the loops that would execute any of the functions.

# Setup the overall GUI font and theme, column structure, and window layout
sg.theme('Dark2')
fnt = ('Gadugi', 20)
left_column = [[sg.Text("What would you like to calculate? \n", font=('Gadugi', 30))],
               [sg.Button("Throughput"),
               sg.Button("Cost of Ownership"),
               sg.Button("Exit")]]
right_column = [[sg.Text("Do these times look correct, or would you like to append? ", font=('Gadugi', 30), key='HEADER', visible=False)],
                  [sg.Text("All times are in seconds. ", font=fnt, key='WARNING', visible=False)],
                  [sg.Listbox(values=[], size=(70, 10), key='LISTBOX', visible=False)],
                  [sg.Input(size=(25, 1), enable_events=True, key='USER-INPUT', visible=False)],
                  [sg.Button("Correct", key='CORRECT', visible=False),
                   sg.Button("Append", key='APPEND', visible=False)]]
layout = [[sg.Column(left_column),
          sg.VSeparator(),
          sg.Column(right_column)]]
window = sg.Window("6EZ Operations Estimations", layout, font=fnt)

# Create an event loop
while True:
    event, values = window.read()
    if event == "Exit" or event == sg.WIN_CLOSED:
        break
    if event == "Throughput":
        window['HEADER'].update(visible=True)
        window['WARNING'].update(visible=True)
        window['LISTBOX'].update(visible=True)
        window['USER-INPUT'].update(visible=True)
        window['CORRECT'].update(visible=True)
        window['APPEND'].update(visible=True)
        with open(r'throughput_variables.txt', 'r') as file:
            content = display_file()
        window['LISTBOX'].update(content)
    if event == "Correct":
        window['LISTBOX'].update(["TEST"])
        print('This is working')
        # answer = ["6EZ Throughput = ", str(calc_thruput(import_values())) + " microns/hour"]
        # window['LISTBOX'].update(answer)
    if event == "Append":
        window['USER-INPUT'].bind('<Return>', '_Enter')
        answer = ["6EZ Throughput = ", str(calc_thruput(update_values())) + " microns/hour"]
        window['LISTBOX'].update(answer)

window.close()

I realized that if a key is not specified for a Button element, then the associated event is the string of the button. But if a key is specified, then the associated event is the key, not the button string. So, in the case of the Correct button being pressed, event then equaled CORRECT not Correct . Thanks everyone!

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