python计时器(打包exe即可)

import time
import tkinter
from tkinter import *
from tkinter.messagebox import *

class StopWatch(Frame):
    msec = 100
    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)
        self._start = 0.0
        self._elapsedtime = 0.0
        self._running = False
        self.timestr = StringVar()
        self.makeWidgets()
        self.flag  = True

    def makeWidgets(self):
        '''制作时间标签'''
        # l = Label(self, textvariable = self.timestr,font=('Arial',150),width= 7,relief=GROOVE,borderwidth=12)
        l = Label(self, textvariable = self.timestr,font=('Arial',150),width= 7)
        self._setTime(self._elapsedtime)
        # l.pack(fill = X, expand = NO, pady = 150, padx = 150)
        l.pack(fill = X, expand = NO, pady = 100, padx = 100)
    def _update(self):
        self._elapsedtime = time.time() - self._start
        self._setTime(self._elapsedtime)
        self._timer = self.after(self.msec, self._update)
    def _setTime(self, elap):
        '''将时间格式改为 分:秒:百分秒'''
        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):
        if not self._running:
            self._start = time.time() - self._elapsedtime
            self._update()
            self._running = True
    def Stop(self):
        '''停止秒表'''
        global count
        if self._running:
            count += 1
            self.after_cancel(self._timer)
            self._elapsedtime = time.time() - self._start
            self._setTime(self._elapsedtime)
            t.insert('end','第{}名:'.format(count) + self.timestr.get())
            t.insert(INSERT,'\n')
            self._running = False
    def Reset(self):
        '''重设秒表'''
        self._start = time.time()
        self._elapsedtime = 0.0
        self._setTime(self._elapsedtime)
    def End(self):
        ''' 记录 '''
        global count
        if self._running:
            count += 1
            self.after_cancel(self._timer)
            self._elapsedtime = time.time() - self._start
            self._setTime(self._elapsedtime)
            t.insert('end','第{}名:'.format(count) + self.timestr.get())
            t.insert(INSERT,'\n')
            self._update()
            self._running = True
    def dump(self):
        t.delete(1.0,'end')
        global count
        count = 0
    def stopwatch(self):
        if self.flag == True:
            self.pack(side = TOP)
            Button(self, text = '开始',font=("Consolas", 15),bg='Silver', command = self.Start).pack(side = LEFT)
            Button(self, text = '停止',font=("Consolas", 15),bg='Silver', command = self.Stop).pack(side = LEFT)
            Button(self, text = '记录',font=("Consolas", 15),bg='Silver', command = self.End).pack(side = LEFT)
            Button(self, text = '重置',font=("Consolas", 15),bg='Silver', command = self.Reset).pack(side = RIGHT)
            Button(self, text = '退出',font=("Consolas", 15),bg='Silver', command = self.quit).pack(side = RIGHT)
            Button(self, text = '清除',font=("Consolas", 15),bg='Silver', command = self.dump).pack(side = RIGHT)
        self.flag = False

# class LoWatch(Frame):
#     def lowatch(self):
#         # if self.flag == True:
#         self.pack(side = TOP)
#         Button(self, text = '开始',font=("Consolas", 15),bg='DodgerBlue').pack(side = LEFT)
#         Button(self, text = '停止',font=("Consolas", 15),bg='DodgerBlue').pack(side = LEFT)
#         Button(self, text = '记录',font=("Consolas", 15),bg='DodgerBlue').pack(side = LEFT)
#         Button(self, text = '重置',font=("Consolas", 15),bg='DodgerBlue').pack(side = RIGHT)
#         Button(self, text = '退出',font=("Consolas", 15),bg='DodgerBlue').pack(side = RIGHT)
#         Button(self, text = '清除',font=("Consolas", 15),bg='DodgerBlue').pack(side = RIGHT)
    # self.flag = False
if __name__ == '__main__':
    count = 0
    root = Tk()
    root.title("比赛计时器")
    root.geometry("600x400")
    # root.configure(background='RoyalBlue')
    frame1 = Frame(root)
    frame1.pack(side = BOTTOM)
    sw = StopWatch(root)
    # nw = LoWatch(root)
    stpwtch = Button(frame1, text = '记时', command = sw.stopwatch,font=('Arial', 10))
    # lopwtch = Button(root, text = '倒计时', command = nw.lowatch,font=('Arial', 10))
    stpwtch.pack(side = LEFT)
    # lopwtch.pack(side = BOTTOM)
    # stpwtch.place(x=300,y=650)
    # lopwtch.place(x=350,y=650)
    t = tkinter.Text(frame1, height=10, width=60,font=("Consolas", 12),autoseparators=False, undo=True, maxundo=10)
    t.pack(pady = 50, padx = 50)
    # t.pack()
    root.mainloop()
上一篇:Conmi的正确答案——安装KEIL后进入“Pack Installer”出现“SSL caching disabled in Windows Internet settings...”解决方案


下一篇:精通Python网络爬虫:核心技术、框架与项目实战.3.2 爬行策略