如何使用 GUI 在 python 游戏中设置回答时间限制?
How to make a time limit for answer in python game with GUI?
我正在制作一款游戏,要求用户根据给定的物种照片输入正确的门。我为我的游戏提供了一个 GUI。目前,我正在努力限制用户必须给出答案的时间。我尝试使用 Timer(线程),但不知道当用户未超过最长时间时如何取消线程。
这是我用于确认答案的按钮的代码:
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)
正如@Bryan Oakley所说,您可以使用tkinter
的after()
方法来设置一个计时器来禁用用户输入,但前提是用户没有在一定的时间。
我会在这里进行解释,并在底部提供一个简单的示例。
使用 after()
设置定时器
首先,如何使用after()
设置定时器。它有两个参数:
- 调用函数前等待的毫秒数,以及
- 到时候调用的函数。
例如,如果您想在 1 秒后更改标签的文本,您可以这样做:
root.after(1000, lambda: label.config(text="Done!")
正在使用 after_cancel()
取消计时器
现在,如果用户确实在给定的时间内提交了输入,您将需要一些取消计时器的方法。这就是 after_cancel()
的用途。它需要一个参数:要取消的计时器的字符串 ID。
要获取定时器的id,您需要将after()
的return值赋给一个变量。像这样:
timer = root.after(1000, some_function)
root.after_cancel(timer)
例子
这是一个使用按钮的 cancel-able 计时器的简单示例。在按钮被禁用之前,用户有 3 秒的时间按下按钮,如果他们在时间到之前按下按钮,计时器将被取消,这样按钮就永远不会被禁用。
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()
如果您还有其他问题,请告诉我!
我正在制作一款游戏,要求用户根据给定的物种照片输入正确的门。我为我的游戏提供了一个 GUI。目前,我正在努力限制用户必须给出答案的时间。我尝试使用 Timer(线程),但不知道当用户未超过最长时间时如何取消线程。 这是我用于确认答案的按钮的代码:
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)
正如@Bryan Oakley所说,您可以使用tkinter
的after()
方法来设置一个计时器来禁用用户输入,但前提是用户没有在一定的时间。
我会在这里进行解释,并在底部提供一个简单的示例。
使用 after()
设置定时器
首先,如何使用after()
设置定时器。它有两个参数:
- 调用函数前等待的毫秒数,以及
- 到时候调用的函数。
例如,如果您想在 1 秒后更改标签的文本,您可以这样做:
root.after(1000, lambda: label.config(text="Done!")
正在使用 after_cancel()
取消计时器
现在,如果用户确实在给定的时间内提交了输入,您将需要一些取消计时器的方法。这就是 after_cancel()
的用途。它需要一个参数:要取消的计时器的字符串 ID。
要获取定时器的id,您需要将after()
的return值赋给一个变量。像这样:
timer = root.after(1000, some_function)
root.after_cancel(timer)
例子
这是一个使用按钮的 cancel-able 计时器的简单示例。在按钮被禁用之前,用户有 3 秒的时间按下按钮,如果他们在时间到之前按下按钮,计时器将被取消,这样按钮就永远不会被禁用。
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()
如果您还有其他问题,请告诉我!