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中实现远程调用(RPC、RMI)简单例子
Apr 28 Python
用Python生成器实现微线程编程的教程
Apr 13 Python
Python中用sleep()方法操作时间的教程
May 22 Python
Python实现短网址ShortUrl的Hash运算实例讲解
Aug 10 Python
十个Python程序员易犯的错误
Dec 15 Python
mysql 之通过配置文件链接数据库
Aug 12 Python
Python数据结构之栈、队列的实现代码分享
Dec 04 Python
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
Nov 10 Python
python二维码操作:对QRCode和MyQR入门详解
Jun 24 Python
详解Django将秒转换为xx天xx时xx分
Sep 27 Python
python nohup 实现远程运行不宕机操作
Apr 16 Python
Python提取视频中图片的示例(按帧、按秒)
Oct 22 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
咖啡磨器 如何选购一台适合家用的意式磨豆机
2021/03/05 新手入门
PHP新手上路(三)
2006/10/09 PHP
PHP 中检查或过滤IP地址的实现代码
2011/11/27 PHP
php在程序中将网页生成word文档并提供下载的代码
2012/10/09 PHP
浅析linux下apache服务器的配置和管理
2013/08/10 PHP
浅析Yii2中GridView常见操作
2016/04/22 PHP
PHP并发多进程处理利器Gearman使用介绍
2016/05/16 PHP
免费空间广告万能消除代码
2006/09/04 Javascript
javascript设计模式之工厂模式示例讲解
2014/03/04 Javascript
jQuery函数map()和each()介绍及异同点分析
2014/11/08 Javascript
使用canvas实现仿新浪微博头像截取上传功能
2015/09/02 Javascript
浅析jQuery 遍历函数,javascript中的each遍历
2016/05/25 Javascript
浅谈js构造函数的方法与原型prototype
2016/07/04 Javascript
微信小程序 WXDropDownMenu组件详解及实例代码
2016/10/24 Javascript
获取jqGrid中选择的行的数据
2016/11/30 Javascript
JS中IP地址与整数相互转换的实现代码
2017/04/10 Javascript
vue loadmore 组件滑动加载更多源码解析
2017/07/19 Javascript
Node实战之不同环境下配置文件使用教程
2018/01/02 Javascript
jQuery实现定时隐藏对话框的方法分析
2018/02/12 jQuery
JS实现的汉字与Unicode码相互转化功能分析
2018/05/25 Javascript
vue组件实现可搜索下拉框扩展
2020/10/23 Javascript
微信小程序 确认框的实现(附代码)
2019/07/23 Javascript
解决Vue使用bus总线时,第一次路由跳转时数据没成功传递问题
2020/07/28 Javascript
Webpack3+React16代码分割的实现
2021/03/03 Javascript
[51:17]Mski vs VGJ.S Supermajor小组赛C组 BO3 第三场 6.3
2018/06/04 DOTA
在Django框架中运行Python应用全攻略
2015/07/17 Python
Python的标准模块包json详解
2017/03/13 Python
Python实现使用卷积提取图片轮廓功能示例
2018/05/12 Python
Python实现iOS自动化打包详解步骤
2018/10/03 Python
VSCode基础使用与VSCode调试python程序入门的图文教程
2020/03/30 Python
Answear匈牙利:来自全球200多个知名时尚品牌
2017/04/21 全球购物
美国医疗用品、医疗设备和家庭保健用品商店:Medical Supply Depot
2018/07/08 全球购物
美国在线旅行社:Crystal Travel
2018/09/11 全球购物
自动化系在校本科生求职信
2013/10/23 职场文书
中学生自我鉴定
2014/02/04 职场文书
《闻一多先生的说和做》教学反思
2014/04/28 职场文书