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使用MySQLdb for Python操作数据库教程
Oct 11 Python
Python用GET方法上传文件
Mar 10 Python
python+opencv轮廓检测代码解析
Jan 05 Python
python语音识别实践之百度语音API
Aug 30 Python
Python查找数组中数值和下标相等的元素示例【二分查找】
Feb 13 Python
应用OpenCV和Python进行SIFT算法的实现详解
Aug 21 Python
Python 元组拆包示例(Tuple Unpacking)
Dec 24 Python
Python异步编程之协程任务的调度操作实例分析
Feb 01 Python
python 使用raw socket进行TCP SYN扫描实例
May 05 Python
python 中的9个实用技巧,助你提高开发效率
Aug 30 Python
python matplotlib库的基本使用
Sep 23 Python
python爬虫基础之urllib的使用
Dec 31 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+SQLite存储方案
2010/09/04 PHP
PHP运行模式的深入理解
2013/06/03 PHP
PHP连接局域网MYSQL数据库的简单实例
2013/08/26 PHP
PHP字符串中特殊符号的过滤方法介绍
2014/02/18 PHP
thinkphp autoload 命名空间自定义 namespace
2015/07/17 PHP
php实现JWT(json web token)鉴权实例详解
2019/11/05 PHP
js中scrollHeight,scrollWidth,scrollLeft,scrolltop等差别介绍
2012/05/16 Javascript
深入探讨JavaScript String对象
2015/03/09 Javascript
基于JavaScript判断浏览器到底是关闭还是刷新(超准确)
2016/02/01 Javascript
AngularJS实现树形结构(ztree)菜单示例代码
2016/09/18 Javascript
Bootstrap CDN和本地化环境搭建
2016/10/26 Javascript
nodejs批量下载图片的实现方法
2017/05/19 NodeJs
js学习总结_选项卡封装(实例讲解)
2017/07/13 Javascript
vue中实现在外部调用methods的方法(推荐)
2018/02/08 Javascript
详解react native页面间传递数据的几种方式
2018/11/07 Javascript
利用React Router4实现的服务端直出渲染(SSR)
2019/01/07 Javascript
Vue中computed、methods与watch的区别总结
2019/04/10 Javascript
Vue父组件如何获取子组件中的变量
2019/07/24 Javascript
Vue+ElementUI项目使用webpack输出MPA的方法
2019/08/27 Javascript
如何解决django配置settings时遇到Could not import settings 'conf.local'
2014/11/18 Python
pymongo实现控制mongodb中数字字段做加法的方法
2015/03/26 Python
Python中的特殊语法:filter、map、reduce、lambda介绍
2015/04/14 Python
浅析Python中的join()方法的使用
2015/05/19 Python
Dlib+OpenCV深度学习人脸识别的方法示例
2019/05/14 Python
详解pytorch 0.4.0迁移指南
2019/06/16 Python
python networkx 根据图的权重画图实现
2019/07/10 Python
python统计函数库scipy.stats的用法解析
2020/02/25 Python
python实现猜数游戏
2020/03/27 Python
Django model.py表单设置默认值允许为空的操作
2020/05/19 Python
CSS3 @keyframes简单动画实现
2018/02/24 HTML / CSS
移动端解决悬浮层(悬浮header、footer)会遮挡住内容的3种方法
2015/03/27 HTML / CSS
详解HTML5 录音的踩坑之旅
2017/12/26 HTML / CSS
"序列点" 是什么
2016/07/29 面试题
企业安全生产承诺书
2014/05/22 职场文书
2019年圣诞节祝福语集锦
2019/12/25 职场文书
nginx实现多geoserver服务的负载均衡
2022/05/15 Servers