pythonpysimplegui

PySimpleGUI need to add a check for event == WIN_CLOSED error


import PySimpleGUI as sg
import math

layout = [
    [sg.Text('Calculator', font=('Helvetica', 20))],
    [sg.Text('', size=(10,1), font=('Helvetica', 20), key='result')],
    [sg.Button('7', size=(5,2)), sg.Button('8', size=(5,2)), sg.Button('9', size=(5,2)), sg.Button('+', size=(5,2))],
    [sg.Button('4', size=(5,2)), sg.Button('5', size=(5,2)), sg.Button('6', size=(5,2)), sg.Button('-', size=(5,2))],
    [sg.Button('1', size=(5,2)), sg.Button('2', size=(5,2)), sg.Button('3', size=(5,2)), sg.Button('*', size=(5,2))],
    [sg.Button('0', size=(5,2)), sg.Button('.', size=(5,2)), sg.Button('=', size=(5,2)), sg.Button('/', size=(5,2))],
    [sg.Button('sin', size=(5,2)), sg.Button('cos', size=(5,2)), sg.Button('tan', size=(5,2)), sg.Button('sqrt', size=(5,2))],
    [sg.Button('log', size=(5,2)), sg.Button('Clear', size=(5,2))]
]

window = sg.Window('Calculator', layout)

result = ''

while True:
    event, values = window.read()
    if event in (None, 'Clear'): 
        result = ''
    elif event in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-', '*', '/', 'log'):
        result += event
    elif event == '=':
        try:
            result = eval(result)
            result = str(result)
        except Exception as e:
            result = f'Error: {e}'
    elif event == 'sin': 
        try:
            result = math.sin(float(result))
            result = str(result)
        except Exception as e:
            result = f'Error: {e}'
    elif event == 'cos':
        try:
            result = math.cos(float(result))
            result = str(result)
        except Exception as e:
            result = f'Error: {e}'
    elif event == 'tan':
        try:
            result = math.tan(float(result))
            result = str(result)
        except Exception as e:
            result = f'Error: {e}'
    elif event == 'sqrt':
        try:
            result = math.sqrt(float(result))
            result = str(result)
        except Exception as e:
            result = f'Error: {e}'
    elif event == sg.WIN_CLOSED :
        break

window.close()

i have here a simple calculator app, which is able to run but the buttons don't work and when i try to close the app a following error popup appears : ERROR Trying to read a closed window, line 20, in event, values = window.read(), You have tried 100 times to read a closed window., You need to add a check for event == WIN_CLOSED

this made me confused because i've already added

elif event == sg.WIN_CLOSED :
        break

any ideas to help point me in the right direction? i'm a beginner.

tried re-writing the sg.WIN_CLOSED command. still doesn't work.


Solution

  • You will get an event sg.WIN_CLOSED which is None when you click X button of the window, window already destroyed before you got the event and you cannot call window.read() again, should break from while loop. That's why you got exception Trying to read a closed window

    Should not combine the check for None and 'Clear' together.

        if event in (None, 'Clear'): 
            result = ''
    

    It should be

        if event == sg.WIN_CLOSED:
            break
        elif event == 'Clear': 
            result = ''