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 调用VC++的动态链接库(DLL)
Sep 06 Python
使用Python对Access读写操作
Mar 30 Python
Python with语句上下文管理器两种实现方法分析
Feb 09 Python
解决已经安装requests,却依然提示No module named requests问题
May 18 Python
浅谈python3发送post请求参数为空的情况
Dec 28 Python
Python使用正则表达式分割字符串的实现方法
Jul 16 Python
django mysql数据库及图片上传接口详解
Jul 18 Python
python中文分词库jieba使用方法详解
Feb 11 Python
MxNet预训练模型到Pytorch模型的转换方式
May 25 Python
python实现三壶谜题的示例详解
Nov 02 Python
python包的导入方式总结
Mar 02 Python
python实现三阶魔方还原的示例代码
Apr 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
php 正则表达式小结
2009/08/31 PHP
PHP 数组教程 定义数组
2009/10/23 PHP
PHP和.net中des加解密的实现方法
2013/02/27 PHP
微信网页授权(OAuth2.0) PHP 源码简单实现
2016/08/29 PHP
根据key删除数组中指定的元素实现方法
2017/03/02 PHP
jquery改变disabled的boolean状态的三种方法
2013/12/13 Javascript
javascript删除数组元素并且数组长度减小的简单实例
2014/02/14 Javascript
Jquery方式获取iframe页面中的 Dom元素
2014/05/07 Javascript
Shell脚本实现Linux系统和进程资源监控
2015/03/05 Javascript
轻松学习jQuery插件EasyUI EasyUI创建CRUD应用
2015/11/30 Javascript
javascript跑马灯抽奖实例讲解
2020/04/17 Javascript
高效Web开发的10个jQuery代码片段
2016/07/22 Javascript
JS实现问卷星自动填问卷脚本并在两秒自动提交功能
2020/06/17 Javascript
JS获取月的第几周和年的第几周实例代码
2018/12/05 Javascript
jQuery AJAX与jQuery事件的分析讲解
2019/02/18 jQuery
解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题
2019/10/25 Javascript
JQuery插件tablesorter表格排序实现过程解析
2020/05/28 jQuery
[08:02]DOTA2牵红线 zhou神抱得美人归
2014/03/22 DOTA
pyside写ui界面入门示例
2014/01/22 Python
python中List的sort方法指南
2014/09/01 Python
Python中使用装饰器来优化尾递归的示例
2016/06/18 Python
Python实现树莓派WiFi断线自动重连的实例代码
2017/03/16 Python
Python 多核并行计算的示例代码
2017/11/07 Python
python中使用iterrows()对dataframe进行遍历的实例
2018/06/09 Python
对Python3 解析html的几种操作方式小结
2019/02/16 Python
pytorch神经网络之卷积层与全连接层参数的设置方法
2019/08/18 Python
python中Lambda表达式详解
2019/11/20 Python
python GUI库图形界面开发之PyQt5美化窗体与控件(异形窗体)实例
2020/02/25 Python
浅析python 动态库m.so.1.0错误问题
2020/05/09 Python
印尼穆斯林时尚购物网站:Hijabenka
2016/12/10 全球购物
世界领先的高品质定制产品平台:Zazzle
2017/07/23 全球购物
广播电视新闻学专业应届生求职信
2013/10/08 职场文书
高一新生军训方案
2014/05/12 职场文书
保洁公司服务承诺书
2014/05/28 职场文书
财务会计专业求职信
2014/06/09 职场文书
SQL Server实现分页方法介绍
2022/03/16 SQL Server