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定时执行之Timer用法示例
May 27 Python
Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
Jan 11 Python
浅析Python装饰器以及装饰器模式
May 28 Python
python实现多张图片拼接成大图
Jan 15 Python
python网络应用开发知识点浅析
May 28 Python
Tensorflow模型实现预测或识别单张图片
Jul 19 Python
详解centos7+django+python3+mysql+阿里云部署项目全流程
Nov 15 Python
python 实现按对象传值
Dec 26 Python
python随机模块random使用方法详解
Feb 14 Python
总结Python连接CS2000的详细步骤
Jun 23 Python
Django Paginator分页器的使用示例
Jun 23 Python
python前后端自定义分页器
Apr 13 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+oracle 分页类
2006/10/09 PHP
实测在class的function中include的文件中非php的global全局环境
2013/07/15 PHP
php设置允许大文件上传示例代码
2014/03/10 PHP
Linux中用PHP判断程序运行状态的2个方法
2014/05/04 PHP
利用PHP将部分内容用星号替换
2020/04/21 PHP
PHP序列化/对象注入漏洞分析
2016/04/18 PHP
js jquery做的图片连续滚动代码
2008/01/06 Javascript
javascript 用原型继承来实现对象系统
2010/03/22 Javascript
jquery表格内容筛选实现思路及代码
2013/04/16 Javascript
jQuery实现的一个tab切换效果内部还嵌有切换
2014/08/10 Javascript
js日期插件dateHelp获取本月、三个月、今年的日期
2016/03/07 Javascript
基于JS实现textarea中获取动态剩余字数的方法
2016/05/25 Javascript
javascript动画系列之模拟滚动条
2016/12/13 Javascript
前端构建工具之gulp的配置与搭建详解
2017/06/12 Javascript
vue.js分页中单击页码更换页面内容的方法(配合spring springmvc)
2018/02/10 Javascript
小程序指纹验证的实现代码
2018/12/04 Javascript
JavaScript中this的全面解析及常见实例
2019/05/14 Javascript
Angular8基础应用之表单及其验证
2019/08/11 Javascript
Vue-router 报错NavigationDuplicated的解决方法
2020/03/31 Javascript
Python聚类算法之DBSACN实例分析
2015/11/20 Python
深入分析python中整型不会溢出问题
2018/06/18 Python
解决python中的幂函数、指数函数问题
2019/11/25 Python
Python实现RGB与HSI颜色空间的互换方式
2019/11/27 Python
CSS3教程(2):网页边框半径和网页圆角
2009/04/02 HTML / CSS
Html5与App的通讯方式详解
2019/10/24 HTML / CSS
完美实现CSS垂直居中的11种方法
2021/03/27 HTML / CSS
护士自我鉴定
2013/10/23 职场文书
环境工程求职简历的自我评价范文
2013/10/24 职场文书
捐书寄语赠言
2014/01/18 职场文书
大学生毕业自我鉴定范文
2014/02/03 职场文书
《狮子和兔子》教学反思
2014/03/02 职场文书
《宿建德江》教学反思
2014/04/23 职场文书
教师作风整改措施思想汇报
2014/10/12 职场文书
2015年酒店工作总结
2015/04/28 职场文书
2015小学音乐教师个人工作总结
2015/07/21 职场文书
高考满分作文赏析(2篇)
2019/08/12 职场文书