python秒表游戏_数学游戏Tkinter中的Python计时器

匿名用户

我想你需要的是这个。from Tkinter import *

import time

class StopWatch(Frame):

""" Implements a stop watch frame widget. """

def __init__(self, parent=None, **kw):

Frame.__init__(self, parent, kw)

self._start = 0.0

self._elapsedtime = 0.0

self._running = 0

self.timestr = StringVar()

self.makeWidgets()

def makeWidgets(self):

""" Make the time label. """

l = Label(self, textvariable=self.timestr)

self._setTime(self._elapsedtime)

l.pack(fill=X, expand=NO, pady=2, padx=2)

def _update(self):

""" Update the label with elapsed time. """

self._elapsedtime = time.time() - self._start

self._setTime(self._elapsedtime)

self._timer = self.after(50, self._update)

def _setTime(self, elap):

""" Set the time string to Minutes:Seconds:Hundreths """

minutes = int(elap/60)

seconds = int(elap - minutes*60.0)

hseconds = int((elap - minutes*60.0 - seconds)*100)

self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))

def Start(self):

""" Start the stopwatch, ignore if running. """

if not self._running:

self._start = time.time() - self._elapsedtime

self._update()

self._running = 1

def Stop(self):

""" Stop the stopwatch, ignore if stopped. """

if self._running:

self.after_cancel(self._timer)

self._elapsedtime = time.time() - self._start

self._setTime(self._elapsedtime)

self._running = 0

def Reset(self):

""" Reset the stopwatch. """

self._start = time.time()

self._elapsedtime = 0.0

self._setTime(self._elapsedtime)

def main():

root = Tk()

sw = StopWatch(root)

sw.pack(side=TOP)

Button(root, text='Start', command=sw.Start).pack(side=LEFT)

Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)

Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)

Button(root, text='Quit', command=root.quit).pack(side=LEFT)

root.mainloop()

if __name__ == '__main__':

main()

注:我刚刚在Tkinter中实现了基本的计时器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值