Issue
Why does the program not check for the Event sg.WINDOW_CLOSED ?
First I check for the event "OK" and output the entered Name
But the following check for closing the window does not work here.
Why cant there be more than one event-checks in the event-loop?
import PySimpleGUI as sg
# Define the window's contents
layout = [[sg.Text("What's your name?")],
[sg.Input(key='-INPUT-')],
[sg.Text(size=(40,1), key='-OUTPUT-')],
[sg.Button('Ok'), sg.Button('Quit')]]
# Create the window
window = sg.Window('Window Title', layout)
# Display and interact with the Window using an Event Loop
while True:
event, values = window.read()
if event == 'Ok':
print(event)
window['-OUTPUT-'].update('Hello ' + values['-INPUT-'] + "!")
# Why cant I check for closing the program here?
if event == sg.WINDOW_CLOSED or event == None:
print(event)
window.close()
Solution
The window closed, so it should break from it's event loop. Add break
after you call window.close().
while True:
event, values = window.read()
print(event)
if event in (sg.WINDOW_CLOSED, 'Quit'): # break from event loop to call `window.close()
window.close()
break
elif event == 'Ok':
window['-OUTPUT-'].update('Hello ' + values['-INPUT-'] + "!")
or break from event loop and call window.close()
.
while True:
event, values = window.read()
print(event)
if event in (sg.WINDOW_CLOSED, 'Quit'): # break from event loop to call `window.close()
break
elif event == 'Ok':
window['-OUTPUT-'].update('Hello ' + values['-INPUT-'] + "!")
window.close()
Answered By - Jason Yang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.