繁体   English   中英

有没有办法在 Python 的指定时间后运行 function 而无需 after 方法?

[英]Is there a way to make a function run after a specified amount of time in Python without the after method?

我正在尝试创建一个简单的程序来跟踪 Tkinter 中用户每秒的点击次数,但我不知道如何使用 after 方法让程序等待而不冻结程序。 问题是我需要在时间结束后记录高分,但是使用这种方法,分数会在点击计数器上升之前记录下来。 这是我的代码:

from tkinter import *
import time
root = Tk()
root.geometry('600x410')
screen = Canvas(root)
h = 6 #button height
w = 12 #button width
c = 0 #counts amount of times clicked
start_btn = 0 #logs clicks of the start button
high_score = 0 #logs the highest score
time = 0

def count_hs():
    high_score = c
def remove_time():
    global time
    time -= 1
def countdown(n):
    for i in range(n):
        time = n
        root.after(1000, remove_time())
        #alternatively i tried this:
        #time.sleep(1)
        #remove_time()
        if time <= 0:
            b["text"] = "Test done."
            break

def start_test():
    global start_btn
    b["text"] = "Click to begin."
    start_btn += 1
    print("start button: " + str(start_btn))

def button_click():
    global start_btn
    global c
    c+=1
    print("click counter: " + str(c))
    #resets the amount of clicks on the large button when the start button is pressed
    if c >= 1 and start_btn >= 1:
        print("test1")
        c = 1
        start_btn = 0
        if b["text"] == "Click to begin.":
            print("test2")
            b["text"] = "Click!"
            countdown(6)
            count_hs()
            print("hs: " +str(high_score))
#primary button
b = Button(root, text=" ", font=("Arial", 40), height = h, width = w, command = lambda: button_click())
b.grid(row=0, column=0)
#start button
start = Button(root, text="Start.", command = lambda: start_test())
start.grid(row=0, column=1)

root.mainloop()

试一试

from tkinter import *

root = Tk()
root.geometry('600x410')
screen = Canvas(root)
h = 6  # button height
w = 12  # button width
c = 0  # counts amount of times clicked
start_btn = 0  # logs clicks of the start button
high_score = 0  # logs the highest score
time = 0


def count_hs():
    global high_score
    if c > high_score:
        high_score = c

    return high_score


def remove_time():
    global time
    time -= 1
    if time > 0:
        root.after(1000, remove_time)
    else:
        show_score()


def start_test():
    global start_btn
    global c
    global time
    b["text"] = "Click to begin."
    start_btn += 1
    print("start button: " + str(start_btn))

    # Reset your timer and counter
    time = 6
    c = 0


def button_click(*args):
    global start_btn
    global c
    # resets the amount of clicks on the large button when the start button is pressed
    if c == 0 and start_btn >= 1:
        start_btn = 0
        b["text"] = "Click!"
        root.after(1000, remove_time)
        print("hs: " + str(high_score))
    else:
        c += 1
    print("click counter: " + str(c))


def show_score():
    global c
    score_label.configure(text=str(c))
    high_score_label.configure(text=str(count_hs()))

    c = 0
    b['text'] = ""


# primary button
b = Button(root, text="", font=("Arial", 40), height=h, width=w, command=button_click)
b.grid(row=0, column=0, rowspan=5)

# start button
start = Button(root, text="Start.", command=lambda: start_test())
start.grid(row=0, column=1)
Label(root, text="Your score").grid(row=1, column=1)
score_label = Label(root, text="")
score_label.grid(row=2, column=1)
Label(root, text="High score").grid(row=3, column=1)
high_score_label = Label(root, text="")
high_score_label.grid(row=4, column=1)

root.mainloop()

一些变化:

  • count_hs ,我假设您只有在当前分数超过它时才会更新高分。
  • 您可以将remove_time用作计时器,方法是让它自己调用直到time <= 0 ,在这种情况下您应该结束游戏。
  • 我将start按钮用作重置器,因此单击它时它将重置ctime
  • button_click上,您现在只需费心更新c (并更改开头的文本)。
  • 最后,我添加了一些标签来显示最终结果,包括当前分数和高分。

一些继续前进的建议:

  • 您可以为应用程序创建一个 class 而不是global变量,它应该使您更容易交换信息并避免细微的错误。
  • 您可以改进布局,尤其是对于新添加的标签。
  • 你可以让你的原始计时器成为一个参数(目前,它是在start_test中设置的)。
  • 而不是from tkinter import * ,我建议你做一些像import tkinter as tkfrom tkinter import...这样的事情,因为它增加了可读性并减少了错误来源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM