簡體   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