Window automatically closing Pysimplegui

Asked

Viewed 340 times

0

I’m having a problem with Pysimplegui. The program is returning the correct value in the output, but when I click the button for the program to do the calculations it closes the window very quickly 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")
   

1 answer

0

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

To solve this I added an infinite loop while which ends when the GUI is closed or the button Exit is clicked and specified events occurring within 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))]
]
  • Follow the modified part

Changed Sg. Window("Basal metabolic rate"). layout(layout) from the window to Sg. Window("Basal metabolic rate",layout) 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 the Sg. Window 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.

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 it does not influence much is not just a matter of reading

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

Below I added two conditions related to events that occur within the GUI, respectively the act of clicking the button "Exit" and in the "Compute 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 same but this way if the number is too broken as for example: 785.99999999999 the program will only make visible 785.99

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

and finally put this here to force the window shut when the whole code has run.

janela.close()

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

Complete 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.