让 after() 方法按时运行 Tkinter

编程入门 行业动态 更新时间:2024-10-24 22:28:15
本文介绍了让 after() 方法按时运行 Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在编写一个工具,该工具必须使用 python 在 Tkinter 中显示精度为 1/100 秒的计时器.我尝试使用 after() 方法和 self.timerLabel.after(0, self.update_timer)

I'm programming a tool that has to display a timer with 1/100 of second as precision in Tkinter with python. I've tried using the after() method with self.timerLabel.after(0, self.update_timer)

def update_timer (self):
        self.timer += 0.01
        self.timerLabel.configure(text="%.2f" % self.timer)
        self.timerLabel.after(10, self.update_timer)

问题是它的运行速度比预期的要慢,我需要知道是否有解决方法或某种方法可以让计时器准确按时运行.或者也许是某种方式使用计算机的时间在屏幕上显示正确的经过时间.提前致谢

The problem is that it runs way slower than expected and I need to know if there's a workaround or some way to have the timer run exactly on time. Or maybe some way to use the computer's time to display the correct elapsed time on the screen. Thank you in advance

推荐答案

最准确的方法大概就是找一个开始时间,然后每次调用更新定时器函数就从当前时间减去开始时间.

The most accurate method is probably to find a start time, then every time you call the update timer function just subtract the start time from the current time.

>

我附上了一些示例代码,向您大致展示了它是如何工作的.调整它以将 after() 用于 Tkinter 应该相当简单.

I've attached some example code to show you roughly how it would work. It should be fairly simple to adapt that to use after() for Tkinter.

import time

# Start time
startTime = time.time();

def update_timer():

  # Find difference and print
  timeDifference = time.time() - startTime;
  print(timeDifference);

  # Sleep appropriate amount of time before printing
  # next statement.
  time.sleep(0.1);

  # Recursively call update.
  update_timer();

# Start running
update_timer();

示例小提琴:https://repl.it/Hoqh/0

这篇关于让 after() 方法按时运行 Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 05:59:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!