如何在Python tkinter中为after函数赋予浮点值?

问题描述 投票:0回答:1

我想在 self.after() 中放入一个十进制数而不是 1,但是它给出了一个错误,或者如何让它更快?

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.counter = 10000
        self.running = False
        self.title('Einfacher down Zähler')
        tk.Label(self, text="Runter zählen:").grid(row=0,column=0)
        self.display = tk.Label(self, text=self.counter)
        self.display.grid(row=1,column=0)
        self.start_button = tk.Button(self, text="Start", command=self.start, state=tk.NORMAL, fg= "blue")
        self.stop_button = tk.Button(self, text="Stop", command=self.stop, state=tk.DISABLED)
        self.start_button.grid(row=0,column=1)
        self.stop_button.grid(row=1,column=1)

    def start(self):
        self.start_button.config(state=tk.DISABLED)
        self.stop_button.config(state=tk.NORMAL)
        self.running = True
        self.after(100,self.count_down)

    def stop(self):
        self.start_button.config(state=tk.NORMAL)
        self.stop_button.config(state=tk.DISABLED)
        self.running = False
 
    def count_down(self):
        self.counter -= 1
        self.display.config(text=self.counter)   
        if self.counter > 0 and self.running:
            self.after(1, self.count_down)

def main():
    app = App()
    app.mainloop()

if __name__ == '__main__':
    main()

我也使用了multiproc,但它不适合主代码

python tkinter multiprocessing
1个回答
0
投票

您可以通过将时间转换为毫秒来将浮点值合并到

after()
中。

self.after(int(1000 * 0.5), self.count_down)  # Adjust 0.5 to your desired floating-point value

确保根据所需的延迟(以秒为单位)修改浮点值。请记住,使用浮点值可能不会显着提高执行速度。

© www.soinside.com 2019 - 2024. All rights reserved.