一个倒计时的小工具

#!/usr/bin/env python
# coding=utf-8

import threading
import time
import Queue
from Tkinter import *
import tkMessageBox
import logging
logging.basicConfig(level=logging.INFO)

## Communication queue
commQueue = Queue.Queue()
g_time = 0

## Function run in thread
def timeThread():
    global g_time
    g_time = timeVar.get() * 60
    while 1:
        logging.info("线程放入队列:%d".decode("utf-8") % g_time)
        commQueue.put(g_time)
        try:
            root.event_generate('<<TimeChanged>>', when='tail')
        except TclError:
            break
        time.sleep(1)
        g_time -= 1
        if g_time==-1:
            begin_btn["fg"] = "black"
            clockVar.set("开始计时")
            break

def timeChanged(event):
    x = commQueue.get()
    logging.info("获取队列:%d".decode("utf-8") % x)
    minits = x//60
    seconds = x%60
    s = "剩余时间 {:02}:{:02}".format(minits, seconds)
    begin_btn["fg"] = "blue"
    clockVar.set(s)
    if x==0:
         tkMessageBox.showinfo("提醒","时间已到")


def clock_func(*args):
    global g_time
    if threading.activeCount()>1:
        g_time = timeVar.get() * 60
    else:
        th=threading.Thread(target=timeThread)
        th.start()

## Create main window
root = Tk()
root.title("计时工具")
root.geometry("180x95-0-45")
root.resizable(width=FALSE,height=FALSE)
root.wm_attributes("-topmost",1)
frame = Frame(root)
frame.pack()
Label(frame,text="设定时间间隔").grid(row=1,column=2)
timeVar = IntVar()
clockVar = StringVar()
time_entry = Entry(frame, textvariable=timeVar, width=8)
time_entry["justify"] = "center"
time_entry.grid(row=2,column=2,sticky="W,E")
begin_btn = Button(frame,textvariable=clockVar,command=clock_func)
begin_btn.grid(row=3,column=2)
timeVar.set(8)
begin_btn["fg"] = "black"
clockVar.set("开始计时")

for child in frame.winfo_children():
    child.grid_configure(pady=3)

time_entry.focus()
root.bind('<<TimeChanged>>', timeChanged)
root.bind("<Return>",clock_func)
root.mainloop()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值