sunhearth
sunhearth

Reputation: 93

No Error Message in Console during Call function in PysimpleGUI

I want to use PySimpleGUI to create a basic GUI, that takes a value as input, and if a button is pressed, a function should start with the input text as parameter. The problem is that the GUI crashes and no errors are printed, so I cant understand whats the issue. Even if I use print("something"), nothing appears in the console. I tried by replacing the function with a simple add() function, and the GUI still crashes. What am I doing wrong? I put the code below, with the simple function instead of the original one.

import PySimpleGUI as sg
def add(x):
    return x+3
sg.theme('DarkAmber')
layout = [  
            [sg.Text('Enter the sentence'), sg.Input(key='-INPUT-')],
            [sg.Button('Translate'), sg.Button('Cancel')],
            [sg.Text(""), sg.Output(key='OUTPUT') ]
            ]
window = sg.Window('AAC', layout)
while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, 'Cancel'):
        break
    elif event == 'Translate':
        v = values['-INPUT-']
        window['OUTPUT'].update(add(v))
window.close()

If I could see the errors maybe I would understand, but nothing is output, the gui just crashes. Thank you.

EDIT: I just understood that here the problem is fixed by adding int() to the function parameter. But still, I would like to get the error in the console, so that I could understand whats the problem of more complex function.

Upvotes: 1

Views: 263

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

I got the exception as

d:\>python test4.py
Traceback (most recent call last):
  File "d:\test4.py", line 21, in <module>
    window['OUTPUT'].update(add(v))
  File "d:\test4.py", line 4, in add
    return x+3
TypeError: can only concatenate str (not "int") to str

The type of value for an Input element is str, you cannot add it with another integer. Convert it into int or float before you add it with a number.

def add(x):
    return int(x) + 3

Replace Output element by Multiline element, or the information of the exception won't be shown on console.

Output element is a sub-class of Multiline element, with options reroute_stdout=True, reroute_stderr=True, then all outputs on stdout and stderr will be transferred to and shown on the Output element. Now GUI closed for the exception, so you will get nothing about it.

Upvotes: 2

Related Questions