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的Django框架中的中间件
Jul 24 Python
Python中Django框架利用url来控制登录的方法
Jul 25 Python
python异常和文件处理机制详解
Jul 19 Python
Python实现简单过滤文本段的方法
May 24 Python
Python+树莓派+YOLO打造一款人工智能照相机
Jan 02 Python
在python中使用nohup命令说明
Apr 16 Python
解决Keras TensorFlow 混编中 trainable=False设置无效问题
Jun 28 Python
vscode调试django项目的方法
Aug 06 Python
Python获取excel内容及相关操作代码实例
Aug 10 Python
Python使用requests模块爬取百度翻译
Aug 25 Python
Django-Scrapy生成后端json接口的方法示例
Oct 06 Python
一文带你了解Python 四种常见基础爬虫方法介绍
Dec 04 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中动态显示签名和ip原理
2007/03/28 PHP
php下目前为目最全的CURL中文说明
2010/08/01 PHP
php调用方法mssql_fetch_row、mssql_fetch_array、mssql_fetch_assoc和mssql_fetch_objcect读取数据的区别
2012/08/08 PHP
php实现的微信红包算法分析(非官方)
2015/09/25 PHP
PHP常量define和const的区别详解
2019/05/18 PHP
js parentElement和offsetParent之间的区别
2010/03/23 Javascript
Javascript笔记一 js以及json基础使用说明
2010/05/22 Javascript
javascript预加载图片、css、js的方法示例介绍
2013/10/14 Javascript
Javascript实现的常用算法(如冒泡、快速、鸽巢、奇偶等)
2014/04/29 Javascript
纯javascript实现自动发送邮件
2015/10/21 Javascript
深入理解ECMAScript的几个关键语句
2016/06/01 Javascript
javascript ES6中箭头函数注意细节小结
2017/02/17 Javascript
ztree实现左边动态生成树右边为内容详情功能
2017/11/03 Javascript
JS通过ajax + 多列布局 + 自动加载实现瀑布流效果
2019/05/30 Javascript
javascript实现图片轮播代码
2019/07/09 Javascript
JS实现网页时钟特效
2020/03/25 Javascript
深入了解Vue.js 混入(mixins)
2020/07/23 Javascript
一些Python中的二维数组的操作方法
2015/05/02 Python
Linux下为不同版本python安装第三方库
2016/08/31 Python
Python自动化运维_文件内容差异对比分析
2017/12/13 Python
Django 接收Post请求数据,并保存到数据库的实现方法
2019/07/12 Python
python 消除 futureWarning问题的解决
2019/12/25 Python
Tensorflow获取张量Tensor的具体维数实例
2020/01/19 Python
From CSV to SQLite3 by python 导入csv到sqlite实例
2020/02/14 Python
查看keras各种网络结构各层的名字方式
2020/06/11 Python
Matplotlib自定义坐标轴刻度的实现示例
2020/06/18 Python
viagogo法国票务平台:演唱会、体育比赛、戏剧门票
2017/03/27 全球购物
美国最大的网络男装服装品牌:Bonobos
2017/05/25 全球购物
英国在线购买轮胎、预订汽车、汽车维修和装配网站:Protyre
2020/04/12 全球购物
《巨人的花园》教学反思
2014/02/12 职场文书
战友聚会主持词
2014/04/02 职场文书
长城导游词400字
2015/01/30 职场文书
神农溪导游词
2015/02/11 职场文书
高中物理教学反思
2016/02/19 职场文书
Nginx进程管理和重载原理详解
2021/04/22 Servers
十个Python自动化常用操作,即拿即用
2021/05/10 Python