''' **************************************** |
* 日 期: 2021-01-23 |
* 版 本: V1.1 |
* 文件名: 计时器\\计时器_1_0.py |
* 描 述: 点击开始按钮 开始计时 同时按钮上的标签变为暂停 |
点击暂停按钮 停止计时 同时按钮上的标签变为开始 |
每一次暂停所对应的计时时间都记录到 record.txt |
点击清零按钮 停止计时 时间变为 00:00:000 |
* 作 者: 帅哥哥 |
**************************************** ''' |
import tkinter |
import threading |
import time |
# 创建窗口 |
from1 = tkinter.Tk() |
# 窗口标题 |
from1.title( '帅哥哥计时器' ) |
# 窗口大小 |
from1.minsize( 400 , 400 ) |
isloop = False # 初始化按钮False为停止 True为开始 |
var = tkinter.StringVar() |
stopid = None # 定义一个空值 |
'''********* 计时函数 *********''' |
def gettime(): |
global isloop |
global stopid |
global star |
global fo |
elap = time.time() - star # 获取时间差 |
if isinstance (stopid, float ): |
a = stopid |
elap = elap + a |
minutes = int (elap / 60 ) # 分钟 |
seconds = int (elap - minutes * 60.0 ) # 秒 |
hseconds = int ((elap - minutes * 60.0 - seconds) * 1000 ) # 毫秒 |
var. set ( '%02d:%02d:%03d' % (minutes, seconds, hseconds)) |
if isloop = = False : |
but1[ 'text' ] = '继续' |
stopid = elap # 把暂停时的时间差赋给 stopid (有记忆) |
fo.write( '%02d:%02d:%03d' % (minutes, seconds, hseconds) + "\n" ) # 记录时间 |
fo.close() # 关闭文件 |
return |
from1.after( 1 , gettime) # 每隔1ms调用函数自身获取时间 |
'''********* 开始\暂停按钮函数 **********''' |
def newtask(): |
global isloop |
global star |
global fo |
if but1[ 'text' ] = = '开始' or but1[ 'text' ] = = '继续' : # 根据按钮的文本来判断是否开启循环 |
if but1[ 'text' ] = = '开始' : |
fo = open ( "record.txt" , "w" ) # 开始时清楚上一次记录的内容 |
else : |
fo = open ( "record.txt" , "a" ) # 追加暂停时的时间 |
but1[ 'text' ] = '暂停' |
isloop = True |
star = time.time() # 获取当前时间 |
# 建立线程 |
t = threading.Thread(target = gettime) |
# 开启线程 |
t.start() |
else : |
isloop = False |
'''******* 清零按钮函数 ********''' |
def clearing(): |
global isloop |
global stopid |
isloop = False # 初始化按钮为停止 |
stopid = None # 定义一个空值 |
var. set ( '00:00:000' ) |
but1[ 'text' ] = '开始' |
# 开始\暂停 按钮 |
but1 = tkinter.Button(from1, text = '开始' , command = newtask) |
but1.place(x = 95 , y = 280 , width = 80 , height = 50 ) # 按钮位置和大小 |
# 重置按钮 |
but2 = tkinter.Button(from1, text = '清零' , command = clearing) |
but2.place(x = 225 , y = 280 , width = 80 , height = 50 ) |
# # 显示时间 |
var. set ( '00:00:000' ) # 初始化时间 |
lab1 = tkinter.Label(from1, textvariable = var, font = ( "Arial Bold" , 30 ), foreground = "red" ) |
lab1.place(x = 110 , y = 150 ) |
# from1.overrideredirect(1) # 隐藏标题栏 最大化最小化按钮 |
from1.attributes( "-toolwindow" , 1 ) # 去掉窗口最大化最小化按钮,只保留关闭 |
# 显示窗体 |
from1.mainloop() |
中级程序员
by: 小少爷 发表于:2021-04-03 17:56:38 顶(0) | 踩(0) 回复
下次抄代码,把注释删掉,作者都显示出来了[斗图表情],点击[ http://img04.sogoucdn.com/app/a/200678/14d226559ccfd8aba502efe9a1c3e12e.jpg ]查看表情
回复评论