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原始字符串(raw strings)用法实例
Oct 13 Python
Python使用urllib模块的urlopen超时问题解决方法
Nov 08 Python
在Django的模型和公用函数中使用惰性翻译对象
Jul 27 Python
Python基于高斯消元法计算线性方程组示例
Jan 17 Python
利用python循环创建多个文件的方法
Oct 25 Python
对Python+opencv将图片生成视频的实例详解
Jan 08 Python
用Anaconda安装本地python包的方法及路径问题(图文)
Jul 16 Python
Python使用指定端口进行http请求的例子
Jul 25 Python
给大家整理了19个pythonic的编程习惯(小结)
Sep 25 Python
python argparse传入布尔参数false不生效的解决
Apr 20 Python
python实现126邮箱发送邮件
May 20 Python
在python中读取和写入CSV文件详情
Jun 28 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
使用VisualStudio开发php的图文设置方法
2010/08/21 PHP
CodeIgniter上传图片成功的全部过程分享
2013/08/12 PHP
PHP获取网页标题的3种实现方法代码实例
2014/04/11 PHP
Laravel第三方包报class not found的解决方法
2019/10/13 PHP
PHP超级全局变量【$GLOBALS,$_SERVER,$_REQUEST等】用法实例分析
2019/12/11 PHP
javascript 表格排序和表头浮动效果(扩展SortTable)
2009/04/07 Javascript
jquery 子窗口操作父窗口的代码
2009/09/21 Javascript
jQuery EasyUI中对表格进行编辑的实现代码
2010/06/10 Javascript
jquery改变disabled的boolean状态的三种方法
2013/12/13 Javascript
Javascript中call与apply的学习笔记
2014/09/22 Javascript
JS简单判断函数是否存在的方法
2017/02/13 Javascript
Node.js成为Web应用开发最佳选择的原因
2018/02/05 Javascript
H5实现手机拍照和选择上传功能
2019/12/18 Javascript
JavaScript 双向链表操作实例分析【创建、增加、查找、删除等】
2020/04/28 Javascript
javascript解析json格式的数据方法详解
2020/08/07 Javascript
vue+elementUI动态增加表单项并添加验证的代码详解
2020/12/17 Vue.js
JavaScript实现跟随鼠标移动的盒子
2021/01/28 Javascript
[52:09]2014 DOTA2华西杯精英邀请赛 5 25 NewBee VS DK第二场
2014/05/26 DOTA
[08:53]DOTA2每周TOP10 精彩击杀集锦vol.9
2014/06/26 DOTA
[01:16]DOTA2小知识课堂 Ep.03 芒果树无伤肉山
2019/12/05 DOTA
linux 下实现python多版本安装实践
2014/11/18 Python
理解Python中的类与实例
2015/04/27 Python
Python爬虫实例扒取2345天气预报
2018/03/04 Python
Python 数据处理库 pandas进阶教程
2018/04/21 Python
Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法
2018/05/04 Python
Python引用计数操作示例
2018/08/23 Python
python定时按日期备份MySQL数据并压缩
2019/04/19 Python
python中的函数递归和迭代原理解析
2019/11/14 Python
django中的数据库迁移的实现
2020/03/16 Python
幼儿园教师培训方案
2014/02/04 职场文书
商业活动邀请函
2014/02/04 职场文书
财务担保书范文
2014/04/02 职场文书
大学优秀班主任事迹材料
2014/05/02 职场文书
社区综治工作汇报
2014/10/27 职场文书
腾讯云服务器部署前后分离项目之前端部署
2022/06/28 Servers
MySQL中LAG()函数和LEAD()函数的使用
2022/08/14 MySQL