Reputation: 1
I have a simple python script , witch when closing popup-window gives error-message. I have tried the different ways in PySimpleGui to write the correct code but it doesen´t work. So I have a popup window with four choices; 1 monitor, 2 monitor, extend monitors and quit. They all do what I want them to do, but if I close the window ( X ) without making a choice , I get an error. I am a beginner. here is the code:
**import os
import subprocess
os.chdir("C:/windows/system32/")
current_dir = r"C:/windows/system32/"
import PySimpleGUI as sg
sg.theme('Bluepurple') # Add a touch of color
layout = [ [sg.Text('Chose monitor')],
[sg.Button('1 monitor'), sg.Button('2 monitor'), sg.Button('3 extend'), sg.Button('4 quit') ]]
window = sg.Window('Chose monitor', layout,)
while True:
event, values = window.Read()
if event == '1 monitor':
subprocess.Popen(os.path.join(current_dir, "Displayswitch.exe /internal"))
elif event == '2 monitor':
subprocess.Popen(os.path.join(current_dir, "Displayswitch.exe /external"))
elif event == '3 extend':
subprocess.Popen(os.path.join(current_dir, "Displayswitch.exe /extend"))
if event in (sg.WIN_CLOSED or event == '4 quit'):
break
window.close()****
here is the error message:
[error][1]
Any ideas to point me in the right direction?
Regards: Goesta
Upvotes: 0
Views: 4404
Reputation: 13051
In your GUI, there're five elements, one sg.Text
and 4 sg.Button
, and also five kind of events may happen, 4 buttons and one window close button, so you need to handle all the events in your event loop.
In general, value of event
will be the key of event or element.
Here, key not defined in your sg.Button
, so default key is the first argument button_text
of sg.Button
.
Another event is the close button of window, it will be sg.WIN_CLOSED
.
while True:
event, values = window.read()
if event == key1: # '1 monitor'
do_job1()
elif event == key2: # '2 monitor'
do_job2()
elif event == key3: # '3 extend'
do_job3()
elif event == key4: # '4 quit'
do_job4()
elif event == sg.WIN_CLOSED: # Window close button event
break
window.close()
Here, it will do the same thing for event '4 quit'
and window close event sg.WIN_CLOSED
, so we handle them in same case and break event loop to close window.
while True:
event, values = window.read()
if event == '1 monitor':
do_job1()
elif event == '2 monitor':
do_job2()
elif event == '3 extend':
do_job3()
elif event in ('4 quit', sg.WIN_CLOSED):
break
window.close()
Trying to read a closed window
It means your window already closed by window.closed()
or destroied when close button of window clicked. That's why statement here to break while loop to call window.close()
and no more window.read()
.
while True:
event, values = window.read()
...
elif event in ('4 quit', sg.WIN_CLOSED): # Window close button event
break
window.close()
You have tried 100 times to read a closed window
, it clearly show that you didn't break while loop and call window.read()
again and again. Check your event while loop to confirm everything is fine.Upvotes: 1