0

im doing a calculator and im trying to give a message when the typed on the input is not an int rather than the program just closing(on the exe file),it gives an error when running in vscode.can someone help me? this is the code:

 if window == janela1 and events == 'Calcular Resultados':
    op = ipt['f']
    te1 = int(ipt['t1'])
    te2 = int(ipt['t2'])
    tc1 = ipt['t1']
    check1 = isinstance(tc1, int)
    if check1 == False:
        def erro404():
            er4 = [
                [sg.Text('Estes campo devem ser preenchidos com numeros')],
                [sg.Button('Fechar programa'), sg.Button('Voltar')]
            ]
            return sg.Window('MathMatrix', er4, finalize=True)
        janela1.hide()
        janela8 = erro404()
        window7, eventos6, ipt7 = sg.read_all_windows()
        if window7 == janela8 and eventos6 == 'Voltar':
            window['t1']('')
            window['t2']('')
            window['f']('')
            janela8.hide()
            janela1.un_hide()
        if window7 == janela8 and eventos6 == 'Fechar programa':
            break

the ipt's are inputs, im using pysimplegui for the interface, im a beginner so dumb code may exist. the error i get is this one Traceback (most recent call last): File "c:\Meus_Projetos\MathMatrix\main.py", line 69, in te1 = int(ipt['t1']) ^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: 'rr'

Renzo-M
  • 21
  • 3
  • i tried this code but it gives an error when i go back to the initial interface as apparently it gives an error with the already typed text i tried resetting it but it didnt work – Renzo-M Aug 16 '23 at 00:26
  • 1
    _it gives an error when running in vscode_ Show us the full error message. Edit the question and add it there. – John Gordon Aug 16 '23 at 01:06
  • @JohnGordon added – Renzo-M Aug 16 '23 at 01:17
  • You can CHECK for this by doing `if not ipt['t1'].isdigit():`. – Tim Roberts Aug 16 '23 at 01:21
  • yes but the problem is when it goes back to "janela1" as it reads the values again but without the if and then i get the error, i tried resetting the inputs but it didnt work – Renzo-M Aug 16 '23 at 01:25
  • You need to add code that forces the user to re-enter the inputs if they aren't integers. – John Gordon Aug 16 '23 at 01:32
  • Maybe do input validation as the user inputs the characters rather than waiting until needing to do a calculation? There's a simple [Input Validation Demo Program](https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Input_Validation.py) that shows one way of doing this – Mike from PSG Aug 16 '23 at 12:02

0 Answers0