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操作mongodb根据_id查询数据的实现方法
May 20 Python
python使用xmlrpclib模块实现对百度google的ping功能
Jun 02 Python
Python的Django框架中的表单处理示例
Jul 17 Python
详解python 拆包可迭代数据如tuple, list
Dec 29 Python
基于python-opencv3的图像显示和保存操作
Jun 27 Python
pycharm修改file type方式
Nov 19 Python
django框架基于queryset和双下划线的跨表查询操作详解
Dec 11 Python
TensorFLow 不同大小图片的TFrecords存取实例
Jan 20 Python
Python json读写方式和字典相互转化
Apr 18 Python
Kmeans均值聚类算法原理以及Python如何实现
Sep 26 Python
python使用selenium爬虫知乎的方法示例
Oct 28 Python
python元组打包和解包过程详解
Aug 02 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文件的实现方法
2007/03/19 PHP
php中file_get_content 和curl以及fopen 效率分析
2014/09/19 PHP
xml 与javascript结合的问题解决方法
2007/03/24 Javascript
关于window.pageYOffset和document.documentElement.scrollTop
2011/04/05 Javascript
JS简单实现文件上传实例代码(无需插件)
2013/11/15 Javascript
js替代copy(示例代码)
2013/11/27 Javascript
JavaScript中的标签语句用法分析
2015/02/10 Javascript
js实现点击图片将图片地址复制到粘贴板的方法
2015/02/16 Javascript
Node.js的Web模板引擎ejs的入门使用教程
2016/06/06 Javascript
jQuery Mobile漏洞会有跨站脚本攻击风险
2017/02/12 Javascript
Angular+Node生成随机数的方法
2017/06/16 Javascript
webpack-dev-server远程访问配置方法
2018/02/22 Javascript
使用express来代理服务的方法
2019/06/21 Javascript
java实现单链表增删改查的实例代码详解
2019/08/30 Javascript
微信小程序实现搜索功能
2020/03/10 Javascript
js实现复制粘贴的两种方法
2020/12/04 Javascript
Python生成随机数的方法
2014/01/14 Python
Python简单实现自动删除目录下空文件夹的方法
2017/08/29 Python
python select.select模块通信全过程解析
2017/09/20 Python
基于numpy中数组元素的切片复制方法
2018/11/15 Python
详解从Django Rest Framework响应中删除空字段
2019/01/11 Python
python 实现矩阵按对角线打印
2019/11/29 Python
python 回溯法模板详解
2020/02/26 Python
彻底搞懂 python 中文乱码问题(深入分析)
2020/02/28 Python
Selenium 滚动页面至元素可见的方法
2020/03/18 Python
对python pandas中 inplace 参数的理解
2020/06/27 Python
python工具快速为音视频自动生成字幕(使用说明)
2021/01/27 Python
Wiggle中国:英国骑行、跑步、游泳 & 铁三运动装备专卖网店
2016/08/02 全球购物
日本非常有名的内衣丝袜品牌:GUNZE
2017/01/06 全球购物
2014年师德师风学习材料
2014/05/16 职场文书
“向国旗敬礼”主题班会活动设计方案
2014/09/27 职场文书
2015安全保卫工作总结
2015/04/25 职场文书
《棉鞋里的阳光》教学反思
2016/02/20 职场文书
MySQL root密码的重置方法
2021/04/21 MySQL
python用tkinter开发的扫雷游戏
2021/06/01 Python
MySql数据库 查询时间序列间隔
2022/05/11 MySQL