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基础教程项目二之画幅好画
Apr 02 Python
删除python pandas.DataFrame 的多重index实例
Jun 08 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
flask session组件的使用示例
Dec 25 Python
pandas ix &amp;iloc &amp;loc的区别
Jan 10 Python
python从zip中删除指定后缀文件(推荐)
Dec 05 Python
python 实现图片上传接口开发 并生成可以访问的图片url
Dec 18 Python
pandas参数设置的实用小技巧
Aug 23 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
Nov 24 Python
Python约瑟夫生者死者小游戏实例讲解
Jan 04 Python
python 命令行传参方法总结
May 25 Python
Python中time标准库的使用教程
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实现的功能是显示8条基色色带
2006/10/09 PHP
PHP4和PHP5共存于一系统
2006/11/17 PHP
php smarty 二级分类代码和模版循环例子
2011/06/16 PHP
利用curl 多线程 模拟 并发的详解
2013/06/14 PHP
jquery弹出层类代码分享
2013/12/27 Javascript
常规表格多表头查询示例
2014/02/21 Javascript
js实现精美的图片跟随鼠标效果实例
2015/05/16 Javascript
js实现的页面加载完毕之前loading提示效果完整示例【附demo源码下载】
2016/08/02 Javascript
node.js中Buffer缓冲器的原理与使用方法分析
2019/11/23 Javascript
在JavaScript中查找字符串中最长单词的三种方法(推荐)
2021/01/18 Javascript
[01:10:30]DOTA2-DPC中国联赛正赛 Dragon vs Dynasty BO3 第一场 3月4日
2021/03/11 DOTA
Python查询阿里巴巴关键字排名的方法
2015/07/08 Python
python 实现红包随机生成算法的简单实例
2017/01/04 Python
Python列表list内建函数用法实例分析【insert、remove、index、pop等】
2017/07/24 Python
Django基础知识与基本应用入门教程
2018/07/20 Python
Python实现繁体中文与简体中文相互转换的方法示例
2018/12/18 Python
Python功能点实现:函数级/代码块级计时器
2019/01/02 Python
python交互模式下输入换行/输入多行命令的方法
2019/07/02 Python
Python3+Requests+Excel完整接口自动化测试框架的实现
2019/10/11 Python
Python调用graphviz绘制结构化图形网络示例
2019/11/22 Python
使用Python给头像加上圣诞帽或圣诞老人小图标附源码
2019/12/25 Python
pyecharts调整图例与各板块的位置间距实例
2020/05/16 Python
python speech模块的使用方法
2020/09/09 Python
python使用bs4爬取boss直聘静态页面
2020/10/10 Python
Puma印度官网:德国运动品牌
2019/10/06 全球购物
北大研究生linux应用求职信
2013/10/29 职场文书
质检部部长职责
2013/12/16 职场文书
小组合作学习反思
2014/02/18 职场文书
老员工辞职信范文
2015/05/12 职场文书
通用员工手册范本
2015/05/14 职场文书
幼儿园教师培训心得体会
2016/01/21 职场文书
施工安全协议书
2016/03/22 职场文书
2016年小学“感恩教师”主题队日活动总结
2016/04/01 职场文书
青年人初次创业的“五不要”
2019/08/23 职场文书
Python基于百度API识别并提取图片中文字
2021/06/27 Python
Python中异常处理用法
2021/11/27 Python