-1

I have asked the user to enter a value pertaining to how many driver bits they need which is an integer from 1 to 71. But the

layouts = [
    [   # Layout 0
        [sg.Text('How many driver bits do you need? (Enter a number between 1 and 71)')],
        [sg.Input(key='-IN0-', expand_x=True, enable_events=True)],
        [sg.Text('', size=(20, 1), key='-OUTPUT0-')],
        [sg.Push(), sg.Button('Next >', key='Next 0')]],

I have shown the statement that I used to take the user input and store it in the variable -IN0-

    innie = int(values["-IN0-"]) ######
    numfiles = int(innie % 8)
    open_output_files(numfiles) 

I am trying to parse the value that the user entered in -IN0- and do a mod 8 and then execute a function with the following value that results from mod. What would be the correct way to have the variable stored to access it later.

Thank you to Jason who has helped me resolve many of the issues I have faced throughout creating this program

WoohSteezy
  • 11
  • 2

1 Answers1

1

It will generate an event if any key pressed for an Input element with option enable_events=True. For example, if you want to input 50, but it will generate event when you press 5, then it will get wrong result to call your function, and then another '0' pressed, it will call your function again. If your click BackSpace clear your input, then your will get exception for ValueError: invalid literal for int() with base 10: ''.

When does the input end ?

  • A button to confirm the input end, or
  • A Enter key pressed by binding '<Return>' event to Input element, or
  • else.

There're many situations maybe happened to get your code wrong, for example, user may input again and again, then it will open the output files again and again.

The best ways is to wait until all required data input from user, click 'Submit' button to valid all inputs and to call your function if all data fine.

For example, a login window will wait user click login button to get username and password, then call login function.

import PySimpleGUI as sg

def login(username, password):
    if username in users and users[username] == password:
        sg.popup(f'You already logged in, {username} !', title='LOGIN')
    else:
        sg.popup(f'Invalid username / password entered !')

users = {
    "Michael": "M12345",
    "Jason": "J54321",
    "Louis": "L67890",
    "Jack": "J09876",
}

sg.theme('DarkBlue3')
layout = [
    [sg.Text("Username"), sg.Push(), sg.Input('', key='Username')],
    [sg.Text("Password"), sg.Push(), sg.Input('', key='Password', password_char='*')],
    [sg.Push(), sg.Button('Login'), sg.Push()],
]
window = sg.Window('title', layout)
sg.theme('DarkBlue4')

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Login':
        username = values['Username']
        password = values['Password']
        login(username, password)

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23