Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading