Dr.
Dr.

Reputation: 79

how to kill a function call after a certain amount of time

def input():
  sg.theme('TanBlue')
  event, values = sg.Window('Login Window',
              [[sg.T('Please Enter your email'), sg.In(key='-ID-')],
              [sg.B('OK'), sg.B('Cancel') ]]).read(close=True)


  login_id = values['-ID-']
  return login_id

I have this simple function written in python, it a small GUI that pops up to take input from a user. but what I'm trying to do is, after a certain amount of time lets say 10 seconds. if no input is given I want to close the GUI and use a default value for the intended input. my entire script pauses using this GUI, if no input is given then the script won't continue. I do not want that, but I can't figure out how to implement this. I tried using the signal in python, but I'm on windows 10 and I couldn't integrate it correctly. I would like a universal solution for this Thanks

Upvotes: 1

Views: 99

Answers (1)

Mike from PSG
Mike from PSG

Reputation: 5764

input is a keyword in Python and thus shouldn't be used as a function name.

You can add a timeout on your read call.

This example will give the user 3 seconds to fill in a value and press OK:

import PySimpleGUI as sg

def timed_input():
    sg.theme('TanBlue')
    event, values = sg.Window('Login Window',
                              [[sg.T('Please Enter your email'), sg.In(key='-ID-')],
                               [sg.B('OK'), sg.B('Cancel')]]).read(timeout=3000, close=True)
    if event == sg.TIMEOUT_KEY:
        print('Timed out')
        login_id = None
    else:
        login_id = values['-ID-']
    return login_id

print(timed_input())

Nice use of the shortened element names and close parameter. I've not seen a timeout with a close parameter before. Interesting combination.

Upvotes: 2

Related Questions