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中内置的NotImplemented类型的用法
Mar 31 Python
简单介绍Python的Django框架加载模版的方式
Jul 20 Python
Python 两个列表的差集、并集和交集实现代码
Sep 21 Python
Python实现网站注册验证码生成类
Jun 08 Python
python中MethodType方法介绍与使用示例
Aug 03 Python
python将txt文档每行内容循环插入数据库的方法
Dec 28 Python
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
May 06 Python
python求最大值,不使用内置函数的实现方法
Jul 09 Python
对Python函数设计规范详解
Jul 19 Python
利用python实现逐步回归
Feb 24 Python
Python学习之os模块及用法
Jun 03 Python
python判断是空的实例分享
Jul 06 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
phpBB BBcode处理的漏洞
2006/10/09 PHP
PHP输出控制功能在简繁体转换中的应用
2006/10/09 PHP
php adodb连接带密码access数据库实例,测试成功
2008/05/14 PHP
下拉列表多级联动dropDownList示例代码
2013/06/27 PHP
理解PHP中的stdClass类
2014/04/18 PHP
PHP抓取及分析网页的方法详解
2016/04/26 PHP
用Javascript 获取页面元素的位置的代码
2009/09/25 Javascript
javascript 模拟点击广告
2010/01/02 Javascript
jQuery $.each的用法说明
2010/03/22 Javascript
更优雅的事件触发兼容
2011/10/24 Javascript
js获取对象为null的解决方法
2013/11/21 Javascript
javascript实现简单的页面右下角提示信息框
2015/07/31 Javascript
js+flash实现的5图变换效果广告代码(附演示与demo源码下载)
2016/04/01 Javascript
详解vue-router 2.0 常用基础知识点之导航钩子
2017/05/10 Javascript
nodejs 日志模块winston的使用方法
2018/05/02 NodeJs
Angular Material Icon使用详解
2018/11/07 Javascript
Makefile/cmake/node-gyp中区分判断不同平台的方法
2018/12/18 Javascript
python类继承与子类实例初始化用法分析
2015/04/17 Python
Python itertools模块详解
2015/05/09 Python
Python函数式编程指南(三):迭代器详解
2015/06/24 Python
python中实现k-means聚类算法详解
2017/11/11 Python
python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
2019/04/03 Python
Pytorch 卷积中的 Input Shape用法
2020/06/29 Python
python 递归相关知识总结
2021/03/03 Python
Html5元素及基本语法详解
2016/08/02 HTML / CSS
澳大利亚当地社区首选的光学商店:1001 Optical
2019/08/24 全球购物
为什么要优先使用同步代码块而不是同步方法?
2013/01/30 面试题
SQL Server提供的3种恢复模型都是什么? 有什么区别?
2012/05/13 面试题
大学生毕业求职找工作的自我评价
2013/09/29 职场文书
教师旷工检讨书
2014/01/18 职场文书
购房公证委托书(2014版)
2014/09/12 职场文书
学校师德师风自我剖析材料
2014/09/29 职场文书
商业门面租房协议书
2014/11/25 职场文书
物业公司管理制度
2015/08/05 职场文书
vue 自定义的组件绑定点击事件
2022/04/21 Vue.js
python 镜像环境搭建总结
2022/09/23 Python