python实现倒计时小工具


Posted in Python onJuly 29, 2019

本文实例为大家分享了python实现倒计时小工具的具体代码,供大家参考,具体内容如下

#!/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()

小编再为大家分享一段代码:Python窗口倒计时

# Countdown using Tkinter 
from tkinter import *
import time
import tkinter.messagebox
 
class App:
 def __init__(self,master):
  frame = Frame(master)
  frame.pack()
  self.entryWidget = Entry(frame)
  self.entryWidget["width"] = 15
  self.entryWidget.pack(side=LEFT)
  self.hi_there = Button(frame, text="开始", command=self.start)
  self.hi_there.pack(side=LEFT)
  self.button = Button(frame, text="退出", fg="red", command=frame.quit)
  self.button.pack(side=LEFT)
  
 def start(self):
  text = self.entryWidget.get().strip()
  if text != "":
   num = int(text)
   self.countDown(num)
  
 def countDown(self,seconds):
  lbl1.config(bg='yellow')
  lbl1.config(height=3, font=('times', 20, 'bold'))
  for k in range(seconds, 0, -1):
   if k == 30:
    print("\a")
   if k== 29:
    print("\a")
   if k== 28:
    print("\a")
   lbl1["text"] = k
   root.update()
   time.sleep(1)
  lbl1.config(bg='red')
  lbl1.config(fg='white')
  lbl1["text"] = "时间到!"
  tkMessageBox.showinfo("时间到!","时间到!")
 
 def GetSource():
  get_window = Tkinter.Toplevel(root)
  get_window.title('Source File?')
  Tkinter.Entry(get_window, width=30,
      textvariable=source).pack()
  Tkinter.Button(get_window, text="Change",
      command=lambda: update_specs()).pack()
 
root = Tk()
root.title("Countdown")
lbl1 = Label()
lbl1.pack(fill=BOTH, expand=1)
app = App(root)
root.mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
用实例分析Python中method的参数传递过程
Apr 02 Python
python运行时间的几种方法
Jun 17 Python
python中文件变化监控示例(watchdog)
Oct 16 Python
Python使用matplotlib绘制多个图形单独显示的方法示例
Mar 14 Python
PyQt实现界面翻转切换效果
Apr 20 Python
解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available
Jul 31 Python
python机器学习库xgboost的使用
Jan 20 Python
sklearn线性逻辑回归和非线性逻辑回归的实现
Jun 09 Python
django 装饰器 检测登录状态操作
Jul 02 Python
BeautifulSoup获取指定class样式的div的实现
Dec 07 Python
python基础学习之递归函数知识总结
May 26 Python
python模板入门教程之flask Jinja
Apr 11 Python
django rest framework 实现用户登录认证详解
Jul 29 #Python
pycharm重命名文件的方法步骤
Jul 29 #Python
PyQt5实现暗黑风格的计时器
Jul 29 #Python
Python Django 实现简单注册功能过程详解
Jul 29 #Python
Django models.py应用实现过程详解
Jul 29 #Python
pycharm中显示CSS提示的知识点总结
Jul 29 #Python
pandas 如何分割字符的实现方法
Jul 29 #Python
You might like
php 生成饼图 三维饼图
2009/09/28 PHP
php字符串函数学习之substr()
2015/03/27 PHP
CodeIgniter钩子用法实例详解
2016/01/20 PHP
Jquery中getJSON在asp.net中的使用说明
2011/03/10 Javascript
javascript 全选与全取消功能的实现代码
2012/12/23 Javascript
Raphael带文本标签可拖动的图形实现代码
2013/02/20 Javascript
JQuery获取样式中的background-color颜色值的问题
2013/08/20 Javascript
js如何获取兄弟、父类等节点
2014/01/06 Javascript
js全选按钮的实现方法
2015/11/17 Javascript
JavaScript的React框架中的JSX语法学习入门教程
2016/03/05 Javascript
原生js实现autocomplete插件
2016/04/14 Javascript
浅谈JavaScript的全局变量与局部变量
2016/06/10 Javascript
js方法数据验证的简单实例
2016/09/17 Javascript
js实现黑白div块画空心的图形
2018/12/13 Javascript
详解Vue-cli3 项目在安卓低版本系统和IE上白屏问题解决
2019/04/14 Javascript
nuxt.js 在middleware(中间件)中实现路由鉴权操作
2020/11/06 Javascript
Python入门篇之对象类型
2014/10/17 Python
Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)
2016/09/21 Python
python绘制直线的方法
2018/06/30 Python
在python里从协程返回一个值的示例
2019/02/19 Python
Python操作qml对象过程详解
2019/09/26 Python
python如何操作mysql
2020/08/17 Python
StubHub希腊:购买体育赛事、音乐会和剧院门票
2019/08/03 全球购物
英文简历中的自荐信范文
2013/12/14 职场文书
公司面试感谢信
2014/02/01 职场文书
体育系毕业生求职自荐信
2014/04/16 职场文书
《鹬蚌相争》教学反思
2014/04/22 职场文书
学生期末评语大全
2014/04/30 职场文书
奥林匹克运动会口号
2014/06/19 职场文书
2014县政府领导班子三严三实对照检查材料思想汇报
2014/09/26 职场文书
环卫工作个人总结
2015/03/04 职场文书
医院病假条怎么写
2015/08/17 职场文书
宣传委员竞选稿
2015/11/19 职场文书
2016年小学教师师德承诺书
2016/03/25 职场文书
导游词之云南省玉龙雪山
2019/12/19 职场文书
MySql 缓存查询原理与缓存监控和索引监控介绍
2021/07/02 MySQL