Window closing automatically PySimpleGUI

I'm having a problem with PySimpleGUI. The program is returning the correct value in the output, but when I click the button so that the program does the calculations it closes the window very quickly soon after displaying the output.

Here is the Code:

import PySimpleGUI as sg

sg.theme('TanBlue')



#layout
layout = [
    [sg.Text("Sexo:", size=(10,0)),sg.Radio("M", "RADIO1",key="masculino"),sg.Radio("F","RADIO1",key="feminino")],

    [sg.Text("Biotipo:",size=(10,0)), sg.Radio("Ectomorfo","RADIO2",key="ecto"),sg.Radio("Mesomorfo","RADIO2",key="meso"), sg.Radio("Endomorfo","RADIO2",key="endo")],

    [sg.Text("Altura(cm):",size=(10,0)), sg.InputText(size=(6,0), key="altura")],

    [sg.Text("Peso(Kg):",size=(10,0)), sg.InputText(size=(6,0), key="peso")],

    [sg.Text("Idade:", size=(10,0)), sg.InputText(size=(6,0),key="idade")],

    [sg.Button("Calcular TMB", size=(10,0)), sg.Exit()],

    [sg.Text("Sua TMB:", size=(10,0)) ,sg.Output(size=(15,0))]
]
#janela
janela = sg.Window("Taxa metabólica basal").layout(layout)
#extrair os dados da tela
Button, values = janela.Read()

masculino = values['masculino']
feminino = values['feminino']  
ecto = values['ecto'] 
meso = values['meso']
endo = values['endo']   
altura = int(values['altura'])
peso = float(values['peso'])
idade = int(values['idade'])

if(masculino == True):    
    tmb = 66 + 13.7*peso + 5*altura - 6.8*idade   
elif(feminino == True): 
    tmb = 665 + 9.6*peso + 1.8*altura - 4.7*idade



print(str(tmb)+" Kcal")
   
 0
Author: Pedro Valerio, 2020-09-18

1 answers

The solution to this problem is simple, what happens is that since your code does not have a while loop to continue after performing an action within your GUI, such as collecting the passed data and performing the calculation for example it closes.

To solve this I I added an infinite loopwhile that ends when the GUI is closed or the button Exit is clicked and I specified the events that occur inside the program

I kept the layout Part

import PySimpleGUI as sg

sg.theme('TanBlue')

#layout
layout = [
    [sg.Text("Sexo:", size=(10,0)),sg.Radio("M", "RADIO1",key="masculino"),sg.Radio("F","RADIO1",key="feminino")],

    [sg.Text("Biotipo:",size=(10,0)), sg.Radio("Ectomorfo","RADIO2",key="ecto"),sg.Radio("Mesomorfo","RADIO2",key="meso"), sg.Radio("Endomorfo","RADIO2",key="endo")],

    [sg.Text("Altura(cm):",size=(10,0)), sg.InputText(size=(6,0), key="altura")],

    [sg.Text("Peso(Kg):",size=(10,0)), sg.InputText(size=(6,0), key="peso")],

    [sg.Text("Idade:", size=(10,0)), sg.InputText(size=(6,0),key="idade")],

    [sg.Button("Calcular TMB", size=(10,0)), sg.Exit()],

    [sg.Text("Sua TMB:", size=(10,0)) ,sg.Output(size=(100,0))]
]
  • follows the modified part

Changed sg.Window ("basal metabolic rate").layout (layout) from the window to sg.Window ("basal metabolic rate", layout) it has the same result in a more compact form.

#janela
janela = sg.Window("Taxa metabólica basal",layout)

According to the documentation of the PySimpleGui o sg.Window it has the parameters Title and layout within the function being: sg.window ("title", layout") the simplest way to use, but of course you can also use the .layout (layout) just like you used it.

First here I put the loop as said before to give segment on user actions while your GUI is not closed.

I changed the name of Button for event but this does not influence much is not just for the sake of reading

#extrair os dados da tela
while True:
    event, values = janela.Read() 

Below I have added two conditions related to the events that occur within the GUI, they being respectively the act of clicking the button "Exit" and no "calculate TMB".

    if event == sg.WIN_CLOSED or event == 'Exit': # if user closes window or clicks cancel
        break

    elif event == "Calcular TMB":
        masculino = values['masculino']
        feminino = values['feminino']  
        ecto = values['ecto'] 
        meso = values['meso']
        endo = values['endo']   
        altura = int(values['altura'])
        peso = float(values['peso'])
        idade = int(values['idade'])

        if(masculino == True):    
            tmb = 66 + 13.7*peso + 5*altura - 6.8*idade   

        elif(feminino == True): 
            tmb = 665 + 9.6*peso + 1.8*altura - 4.7*idade

In this part here I changed the print mode and put it to appear formatted

The result is still the even though this way if the number is very broken as for example: 785.99999999999999 the program will only leave vivid 785.99

        print(f"{tmb:.2f} Kcal")

And finally I put this here to force the window to close when the whole code is finished running.

janela.close()

Hope I helped! Follow the link for official documentation: https://pysimplegui.readthedocs.io/en/latest /

Full Code:

import PySimpleGUI as sg

sg.theme('TanBlue')

#layout
layout = [
    [sg.Text("Sexo:", size=(10,0)),sg.Radio("M", "RADIO1",key="masculino"),sg.Radio("F","RADIO1",key="feminino")],

    [sg.Text("Biotipo:",size=(10,0)), sg.Radio("Ectomorfo","RADIO2",key="ecto"),sg.Radio("Mesomorfo","RADIO2",key="meso"), sg.Radio("Endomorfo","RADIO2",key="endo")],

    [sg.Text("Altura(cm):",size=(10,0)), sg.InputText(size=(6,0), key="altura")],

    [sg.Text("Peso(Kg):",size=(10,0)), sg.InputText(size=(6,0), key="peso")],

    [sg.Text("Idade:", size=(10,0)), sg.InputText(size=(6,0),key="idade")],

    [sg.Button("Calcular TMB", size=(10,0)), sg.Exit()],

    [sg.Text("Sua TMB:", size=(10,0)) ,sg.Output(size=(100,0))]
]
#janela
janela = sg.Window("Taxa metabólica basal",layout)

#extrair os dados da tela
while True:
    event, values = janela.Read() 

    if event == sg.WIN_CLOSED or event == "Exit": # if user closes window or clicks cancel
        break

    elif event == "Calcular TMB":
        masculino = values['masculino']
        feminino = values['feminino']  
        ecto = values['ecto'] 
        meso = values['meso']
        endo = values['endo']   
        altura = int(values['altura'])
        peso = float(values['peso'])
        idade = int(values['idade'])

        if(masculino == True):    
            tmb = 66 + 13.7*peso + 5*altura - 6.8*idade   

        elif(feminino == True): 
            tmb = 665 + 9.6*peso + 1.8*altura - 4.7*idade

        print(f"{tmb:.2f} Kcal")

janela.close()
 0
Author: LucasV75, 2020-09-19 01:10:41