Python - Pysimplegui - Program error when finished

Asked

Viewed 865 times

-1

Hey there, guys! I’m new to programming and I’ve been studying Python for a few weeks. I am working on a program that basically, picks a price whatever you type (in Real) and applies a percentage to more according to two options defined in a menu and returns it updated in an Output. Your GUI was created using the Pysimplegui library.

The program runs perfectly. However, when closed, Pycharm accuses: Process finished with Exit code 1

When converting it to exe, the program runs correctly, however, when it is closed, an error appears in a box written "Failed to execute script taxa.py" and when passing it to another computer, it does not even open, just appears this error.

I know the code is far from perfect, but I would like to know what I should do to make it work properly.

The version of Python I use is 3.8

Here is the program code:

import PySimpleGUI as sg
from decimal import Decimal
import locale
locale.setlocale(locale.LC_ALL, '')


sg.theme('DefaultNoMoreNagging')

class Tela:

    def __init__(self):

        # Layout
        layout = [
            [sg.OptionMenu(values=[
                "Taxa 1",
                "Taxa 2"],
                key='opcoes', size=(27, 0), pad=(15, 10))],
            [sg.Text('Digite o valor a ser calculado:', pad=(30, 0))],
            [sg.Input(key='valordigitado', size=(40, 0), pad=(0, 10))],
            [sg.Button('Calcular', pad=(80, 0), bind_return_key=True, size=(30,0))],
            [sg.Output(size=(30,20), key='output')]

        ]
        # Janela
        self.janela = sg.Window('Aplicadora de taxas',
                                size=(250, 460)).layout(layout)

    def iniciar(self):
        
        taxa01 = 1.15
        taxa02 = 1.30
        
        while True:
            # Extrair os dados da tela
            self.button, self.values = self.janela.Read()

            opcoes = self.values['opcoes']
            valordigitado = self.values['valordigitado']

            self.janela['output'].Update('')
            
            if ',' in valordigitado:
                if '.' in valordigitado:
                    valordigitado = valordigitado.replace('.', '')
                valordigitado = valordigitado.replace(',', '.')

            try:
                valordigitado = Decimal(valordigitado)

            except:
                print('Digite um número válido.')
                continue
            
            #Cálculo
            try:
                if opcoes == 'Taxa 1':
                    valordigitado = valordigitado * Decimal(taxa01)
                
                elif opcoes == 'Taxa 2':
                    valordigitado = valordigitado * Decimal(taxa02)

            except:
                print('Digite um número válido.')
                continue
            

            print(f'R$ {locale.format_string("%.2f", valordigitado, grouping=True)}')


Tela().iniciar()
  • You wrote the script with lack of configuration,I will send you the corrected code soon.... And with explanation! Also no need to use class in Pysimplegui, no POO use required .

2 answers

0

The most critical error was the lack of command:

   if event == sg.WIN_CLOSED:
        break

This command closes the window in (X) without error!

Also does not go with this other guy there,kivy is much more complex and he does the same thing as Pysimplegui,but with kivy generates an APK on linux with buildozer. The guy already knows how to program by the way, so I recommend you use Pysimplegui,for what I saw in your code you just met the lib or did not read the documentation or is new 4 months at most in python is not?

0


I recommend migrating from pysimplegui to kivy. It’s equally simple and fast to create interfaces without stress, but better in many ways.I also found unsolved bugs in pysimplegui and found it simpler to use kivy. https://python.libhunt.com/compare-pysimplegui-vs-kivy

Browser other questions tagged

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