<>Python实现秒表,显示时分秒,并有开始、退出、重置按钮
from tkinter import * import time # 继承自 Frame 的类 class Clock(Frame): def
__init__(self): Frame.__init__(self) self._start = 0.0 # 私有 开始时间设为 0 self.
_passtime= 0.0 # 已经过去了的时间设为 0 self._isRunning = False # 秒表是否在运行 默认为否 self.
timestr= StringVar() # 时间字符串 self.layout() # 图形界面布局 # 布局 def layout(self): lab =
Label(self, textvariable=self.timestr, font=100) self._setTime(self._passtime)
lab.pack(fill="x", expand=1, pady=20) # 设定时间 def _setTime(self, passTime):
minutes= int(passTime/60) seconds = int(passTime - minutes * 60.0) mseconds =
int((passTime - minutes * 60.0 - seconds) * 10) self.timestr.set(
'%02d:%02d.%01d' % (minutes, seconds, mseconds)) # 更新时间 def _update(self): self.
_passtime= time.time() - self._start self._setTime(self._passtime) self.timer =
self.after(100, self._update) # 每 100ms 更新一次 # 开始 def begin(self): if not self.
_isRunning: self._start = time.time() - self._passtime self._update() self.
_isRunning= True # 停止 def stop(self): if self._isRunning: self.after_cancel(self
.timer) self._passtime = time.time() - self._start self._setTime(self._passtime)
self._isRunning = False # 重设 def reset(self): if not self._isRunning: #
设置只有在秒表停止后 reset 才起作用 self._start = time.time() self._passtime = 0.0 self.
_setTime(self._passtime) if __name__ == '__main__': def main(): import tkinter
myclock= Tk() clk = Clock() width = 320 height = 96 screenwidth = myclock.
winfo_screenwidth() screenheight = myclock.winfo_screenheight() alignstr =
'%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
myclock.geometry(alignstr) myclock.title("My Second Chronograph") clk.pack(side=
TOP) b1 = Button(myclock, text='Start', command=clk.begin, bg="#82A6F5") b1.pack
(fill="x", expand=1, side=LEFT) b2 = Button(myclock, text='Stop', command=clk.
stop, bg="#82A6F5") b2.pack(fill="x", expand=1, side=LEFT) b3 = Button(myclock,
text='Reset', command=clk.reset, bg="#82A6F5") b3.pack(fill="x", expand=1, side=
LEFT) b4 = Button(myclock, text='Quit', command=clk.quit, bg="#82A6F5") b4.pack(
fill="x", expand=1, side=LEFT) myclock.mainloop() main()

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信