Home > database >  How to make a time limit for answer in python game with GUI?
How to make a time limit for answer in python game with GUI?

Time:04-26

I am making a game that requires from a user to type a proper phylum, based on a given photo of a species. I provide a GUI for my game. Currently, I am struggling to limit the time that user have to give his answer. I tried with Timer (threading), but idk how to cancel a thread when user doesn't exceed a maximum time. Here is my code for the button that is used to confirm answer:

  time_answer = 10
    def confirm_action():
        global img_label, answer, n
        from_answer = answer.get().lower()
        if n == len(files) - 1:
            answer_check(from_answer, round_up(end - start, 2))
            confirm.configure(text="Done")
            answer.configure(state=DISABLED)
            n  = 1
        elif n == len(files):
            root.quit()
        else:
            answer_check(from_answer, "(Press shift)")
            answer.bind("<Shift_L>", insert_text)
            n  = 1
            img_f = ImageTk.PhotoImage(Image.open(f"program_ib_zdjecia/faces/{directories[n]}/{files[n]}"))
            img_label.configure(image=img_f)
            img_label.image = img_f
            t = Timer(time_answer, confirm_action)
            t.start()
    
    confirm = Button(root, text="Confirm", command=confirm_action)

CodePudding user response:

As @Bryan Oakley said, you can use tkinter's after() method to set a timer that disables user input, but only if the user doesn't submit input within the certain amount of time.

I'll have the explanations here, and a simple example will be at the bottom.


Setting a timer using after()

First, how to set a timer using after(). It takes two arguments:

  1. The number of milliseconds to wait before calling the function, and
  2. The function to call when it's time.

For example, if you wanted to change a label's text after 1 second, you could do something like this:

root.after(1000, lambda: label.config(text="Done!")

Canceling the timer using after_cancel()

Now, if the user does submit the input within the given amount of time, you'll want some way of cancelling the timer. That's what after_cancel() is for. It takes one argument: the string id of the timer to cancel.

To get the id of a timer, you need to assign the return value of after() to a variable. Like this:

timer = root.after(1000, some_function)
root.after_cancel(timer)

Example

Here's a simple example of a cancel-able timer using a button. The user has 3 seconds to press the button before it becomes disabled, and if they press the button before time is up, the timer gets canceled so that the button never gets disabled.

import tkinter

# Create the window and the button
root = tkinter.Tk()
button = tkinter.Button(root, text="Press me before time runs out!")
button.pack()

# The function that disables the button
def disable_button():
    button.config(state="disabled", text="Too late!")

# The timer that disables the button after 3 seconds
timer = root.after(3000, disable_button)

# The function that cancels the timer
def cancel_timer():
    root.after_cancel(timer)

# Set the button's command so that it cancels the timer when it's clicked
button.config(command=cancel_timer)

root.mainloop()

If you have any more questions, let me know!

  • Related