- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 python 中使用 Tkinter 制作了一个倒数计时器,但我唯一的问题是计时器中的一秒比实际秒长一点。
我使用 after() 函数每毫秒从时钟中删除一毫秒(0.001 秒)。
我不知道它是否这样做是因为时钟的代码需要一些额外的时间来执行,如果是这种情况,我如何制作一个具有完全相同的 UI 且执行时间更短的时钟?
这是一个 Video showing the problem
程序:
from tkinter import *
class root(Tk):
def __init__(self):
super(root, self).__init__()
self.title("Timer")
self.buttonplay = Button(self, text = "Play", fg= 'green', command = self.play)
self.buttonplay.pack()
self.buttonpause = Button(self, text = "Pause", fg = "red", command=self.pause)
self.buttonpause.pack()
self.createTimers()
def play(self):
self.timeit=True
self.timer1.configure(bg='#1C953D')
self.doTimer()
def pause(self):
self.timeit=False
self.timer1.configure(bg='#454545')
def reset(self):
self.timer1.destroy()
self.createTimers()
def createTimers(self):
self.minute = 0
self.second = 5
self.ms = 0
self.total = self.second + self.minute *60 + self.ms*0.001
self.time1 = StringVar()
self.time1.set(str(self.minute).rjust(2, '0') + ':' + str(self.second).rjust(2, '0') +'.'+ str(self.ms).rjust(3, '0'))
self.timer1 = Label(self, textvariable=self.time1, bg='#454545', fg='white', font ="Gadugi 40 bold")
self.timer1.pack()
self.timer1.configure(bg='#454545')
def doTimer(self):
self.time = self.second + self.minute *60 + self.ms*0.001
if self.time !=0: #Checks if the timer ended
if self.timeit:
self.timer1.configure(bg='#1C953D')
self.ms = self.ms -1
if self.ms <0:
self.second = self.second -1
self.ms = 999
if self.second == -1:
self.minute = self.minute -1
self.second = 59
self.time1.set(str(self.minute).rjust(2, '0') + ':' + str(self.second).rjust(2, '0') +'.'+ str(self.ms).rjust(3, '0'))
if self.timeit:
self.after(1, self.doTimer)
else:
self.ended = 1
self.timer1.configure(bg='#FF0000')
self.after(3000, self.reset)
root = root()
root.mainloop()
最佳答案
I don't know if it's doing that because the code of the clock takes some extra time to execute
在这种情况下,您是对的,计时器的节奏取决于代码的运行时间。因此,让这个脚本需要更多的计算机资源也会减慢计时器的速度,反之亦然。
"Don't reinvent the wheel." - Programming proverb.
使用 time
模块在您的计时器中获得更准确的时间。更具体地说,time.time()
并对其进行格式化以使其可读。
这里有一个例子来说明你的问题。
import time
time_zone = 0 # UTC
while True:
current_time = time.time()
ms = (current_time * 1000) % 1000
s = (current_time % 60)
m = (current_time % (60*60))//60
h = (current_time % (60*60*24))//(60*60)
print(f"h:{int(h+time_zone)} - m:{int(m)} - s:{int(s)} - ms:{int(ms)}")
time.sleep(1)
理论上,这个脚本应该精确地每秒打印一次时间。然而,由于代码运行时间,增加了几毫秒。删除 time.sleep(1)
应该让您非常接近实时时钟。
所以如果你想要一个接近计时器或秒表的东西,你会得到一个当前纪元时间的时间戳(time.time()
)更新你的计时器每秒每 x 个滴答声为了获得计算 ms 的效果,当再次按下按钮时,您将获得一个新的时间戳,然后将其与第一个时间戳进行比较并减去以获得每次按钮按下之间的准确时间。
我制作了一个模板供您修改:
import tkinter as tk
import time
time_zone = 0 # UTC
# Convert epoch time to readable format
def readable_epoch(time_epoch):
ms = (time_epoch * 1000) % 1000
s = (time_epoch % 60)
m = (time_epoch % (60*60))//60
h = (time_epoch % (60*60*24))//(60*60)
return (h, m, s, ms)
class App(tk.Tk):
def __init__(self):
super().__init__()
self.time1 = 0
self.time2 = 0
self.geometry("300x200")
self.button1 = tk.Button(
self, text="Click me first", command=self.button1, bg="teal")
self.button1.pack(ipadx=10, ipady=10, expand=True, side=tk.LEFT)
self.button2 = tk.Button(
self, text="Click me second", command=self.button2, bg="pink")
self.button2.pack(ipadx=10, ipady=10, expand=True, side=tk.LEFT)
# Get current time
def button1(self):
self.current_time = time.time()
h, m, s, ms = readable_epoch(self.current_time)
print(f"h:{int(h+time_zone)}, m:{int(m)}, s:{int(s)}, ms:{int(ms)}")
# Elapsed time since button 1 was pressed:
def button2(self):
new_time = time.time()
new_time = new_time - self.current_time
h, m, s, ms = readable_epoch(new_time)
print(f"h:{int(h+time_zone)}, m:{int(m)}, s:{int(s)}, ms:{int(ms)}")
if __name__ == "__main__":
app = App()
app.mainloop()
关于Python tkinter : Countdown Timer not accurate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72336137/
我有一个具有多种随机形式的场景(如三 Angular 形、梯形,还有更多自定义设计),我正在尝试编写碰撞检测代码。这些形状都是二维的,并且位于 Y=0 由于形状比圆形和矩形更复杂,我决定使用光线转换来
所以我对编程还很陌生,但我只是在研究一个简单的计算器。当我启动程序并尝试除法部分(尝试将 5 除以 2)时,输出为 3.0 。这 2 个数字是 float ,所以我真的不明白为什么这不起作用。其次,乘
我正在寻找一种准确的AS3语法(格式不是问题,但我想ANTLR将发挥最大作用)可用于我正在制作的实践语法。 可用于AS3的最准确的语法是什么? 最佳答案 如果您正在寻找一种ANTLR语法,我认为这是非
我正在尝试将 GPS 功能添加到我的 iPhone 应用程序中。这是一款在步行或运行时使用的锻炼应用程序。所以我想使用 GPS 来显示人的移动速度(以英里/小时和分钟/英里为单位)。 我应该如何配置
我在 PlaneGeometry 上使用 EdgesGeometry,它似乎在鼠标事件中创建了一个更大的碰撞框。然而,当使用 CircleGeometry 时,这一点并不明显。我有以下内容:
我希望从 URL 中提取以下字段,如协议(protocol)、域名、端口和路径。 我知道这个split 函数对我很有帮助。这是我的代码 "https://www.test.com:8081/a/b/c
我发现无论何时我们想要多态性,我们都需要指针或引用,因为将 Derived 实例存储在 Base 变量中会“切掉”任何不是的东西在“基础”中定义。 (我想那是因为 Derived 和 Base 实例不
我在 python 中使用 Tkinter 制作了一个倒数计时器,但我唯一的问题是计时器中的一秒比实际秒长一点。 我使用 after() 函数每毫秒从时钟中删除一毫秒(0.001 秒)。 我不知道它是
我最近询问了我当前工作地点的前首席开发人员,为什么他选择使用Razor Generator将我们的 View 预编译为单独的文件组装。 他在下面提出了一些主张,但我似乎无法在网络上找到任何 Razor
我正在使用 Windows 批处理文件与服务器执行双向同步。批处理文件将由多人使用,以同步具有单个网络位置的多台计算机。由于我不会进入的限制,它必须是一个批处理文件。没有 vbscript,没有 ja
我想为代码中的每个函数计时。所以我有 func1(); func2(); func3(); 目前我正在使用 #include #include double start1=omp_get_wtim
sizeWithFont:constrainedToSize:lineBreakMode: 似乎没有返回正确的宽度。执行这些代码后,我看到标签中的部分字符串被截断了,这意味着我必须手动添加几个像素的大
我正在尝试在 Julia 中实现以下公式来计算 Gini coefficient工资分配: 哪里 这是我为此使用的代码的简化版本: # Takes a array where first column
在 Xcode 9.1 上,Pixel Accurate 选项在Window 菜单下被禁用并呈灰色显示。 如果没有此选项,如何截取 App Store 屏幕截图? 最佳答案 原来的答案是错误的! Ap
我正在使用谷歌地图 API 3 和自定义叠加层 (OverlayView)我有这个代码: http://jsfiddle.net/8X6cY/1/ 请将鼠标悬停在制作者上以便查看工具提示叠加层。 如何
好吧,我没有使用“alsoResize”,但我已经测试过,它的行为是相同的。 当您调整主元素的大小时,底部元素“选取框”的黑色边框通常会与顶部元素的白色虚线边框偏离。 $(".layer").resi
我不明白这条 itunesconnect 信息? 我使用这个库: https://github.com/ISBX/apprtc-ios/issues We noticed that your app
我有一个包含调用详细记录的 mysql 数据库。在这个特定的查询中,我想搜索所有调用以了解调用断开的原因,并返回断开连接原因的列表以及每个原因发生的次数。 示例结果集如下所示: causeCode
我正在寻找解决粘条问题的更好方法。 第二个和第三个框之间的“-----”是粘性条应该显示的阈值。当它显示时,它与第三个框完全重叠。 在真正的解决方案中,我使用 jquery 添加了 css (marg
我正在关注这个 Vulkan Youtube video tutorial约书亚舒克。我目前正在观看他的第 14 个视频,他正在为顶点缓冲区创建辅助队列系列。这侧重于顶点缓冲区的暂存过程。我的代码与他
我是一名优秀的程序员,十分优秀!