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采用requests库模拟登录和抓取数据的简单示例
Jul 05 Python
python实现定时播放mp3
Mar 29 Python
Python设计模式编程中Adapter适配器模式的使用实例
Mar 02 Python
python单例模式实例解析
Aug 28 Python
小白入门篇使用Python搭建点击率预估模型
Oct 12 Python
python 图像平移和旋转的实例
Jan 10 Python
Python 循环终止语句的三种方法小结
Jun 24 Python
python实现上传文件到linux指定目录的方法
Jan 03 Python
pytorch中使用cuda扩展的实现示例
Feb 12 Python
sklearn的predict_proba使用说明
Jun 28 Python
Python实现机器学习算法的分类
Jun 03 Python
Python面向对象编程之类的概念
Nov 01 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 遍历XP文件夹下所有文件
2008/11/27 PHP
Laravel中使用阿里云OSS Composer包分享
2015/02/10 PHP
PHP实现登陆表单提交CSRF及验证码
2017/01/24 PHP
基于jquery的无限级联下拉框js插件
2011/10/29 Javascript
js实现的仿新浪微博完美的时间组件升级版
2011/12/20 Javascript
PHP abstract与interface之间的区别
2013/11/11 Javascript
JS+CSS实现大气的黑色首页导航菜单效果代码
2015/09/10 Javascript
jQuery Chart图表制作组件Highcharts用法详解
2016/06/01 Javascript
玩转NODE.JS(四)-搭建简单的聊天室的代码
2016/11/11 Javascript
leaflet的开发入门教程
2016/11/17 Javascript
JavaScript使用delete删除数组元素用法示例【数组长度不变】
2017/01/17 Javascript
javascript实现二叉树的代码
2017/06/08 Javascript
详细介绍RxJS在Angular中的应用
2017/09/23 Javascript
es6系列教程_ Map详解以及常用api介绍
2017/09/25 Javascript
EasyUI Tree树组件无限循环的解决方法
2017/09/27 Javascript
redux中间件之redux-thunk的具体使用
2018/04/17 Javascript
使用vue2.0创建的项目的步骤方法
2018/09/25 Javascript
Python实现给文件添加内容及得到文件信息的方法
2015/05/28 Python
python使用threading获取线程函数返回值的实现方法
2017/11/15 Python
用python写测试数据文件过程解析
2019/09/25 Python
解决Pyinstaller打包软件失败的一个坑
2021/03/04 Python
IWOOT美国:新奇的小玩意
2018/04/27 全球购物
奢华时尚的独特视角:La Garçonne
2018/06/07 全球购物
介绍一下结构化程序设计方法和面向对象程序设计方法的区别
2012/06/27 面试题
员工培训心得体会
2013/12/30 职场文书
新学期开学寄语
2014/01/18 职场文书
《天安门广场》教学反思
2014/04/23 职场文书
新书发布会策划方案
2014/06/09 职场文书
教室布置标语
2014/06/26 职场文书
行政文员实习自我鉴定范文
2014/09/14 职场文书
2014预备党员批评与自我批评思想汇报
2014/09/20 职场文书
2015新员工试用期工作总结
2014/12/12 职场文书
2015大学生求职信范文
2015/03/20 职场文书
如何写好闭幕词
2019/04/02 职场文书
优秀大学生申请书
2019/06/24 职场文书
Vue3中toRef与toRefs的区别
2022/03/24 Vue.js