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 相关文章推荐
仅利用30行Python代码来展示X算法
Apr 01 Python
解析Python中的异常处理
Apr 28 Python
python高级特性和高阶函数及使用详解
Oct 17 Python
Python使用combinations实现排列组合的方法
Nov 13 Python
python 提取tuple类型值中json格式的key值方法
Dec 31 Python
python根据时间获取周数代码实例
Sep 30 Python
Python如何实现动态数组
Nov 02 Python
关于Tensorflow分布式并行策略
Feb 03 Python
Keras:Unet网络实现多类语义分割方式
Jun 11 Python
python3实现将json对象存入Redis以及数据的导入导出
Jul 16 Python
python3爬虫GIL修改多线程实例讲解
Nov 24 Python
python实现socket简单通信的示例代码
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防注入漏洞过滤函数代码
2012/04/11 PHP
使用 TypeScript 重新编写的 JavaScript 坦克大战游戏代码
2015/04/07 Javascript
js实现获取div坐标的方法
2015/11/16 Javascript
canvas绘制万花筒效果(代码分享)
2017/01/20 Javascript
解决ztree搜索中多级菜单展示不全问题
2017/07/05 Javascript
如何在Vue.js中实现标签页组件详解
2019/01/02 Javascript
js实现指定时间倒计时效果
2019/08/26 Javascript
layui中select,radio设置不生效的解决方法
2019/09/05 Javascript
微信小程序(订阅消息)功能
2019/10/25 Javascript
webgl实现物体描边效果的方法介绍
2019/11/27 Javascript
ant-design-vue中的select选择器,对输入值的进行筛选操作
2020/10/24 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
详解Swift中属性的声明与作用
2016/06/30 Python
Python基础之文件读取的讲解
2019/02/16 Python
python re库的正则表达式入门学习教程
2019/03/08 Python
对python中不同模块(函数、类、变量)的调用详解
2019/07/16 Python
Python logging设置和logger解析
2019/08/28 Python
Python如何访问字符串中的值
2020/02/09 Python
python thrift 实现 单端口多服务的过程
2020/06/08 Python
Python join()函数原理及使用方法
2020/11/14 Python
意大利咖啡、浓缩咖啡和浓缩咖啡机:illy caffe
2019/03/20 全球购物
"引用"与多态的关系
2013/02/01 面试题
警察思想汇报
2014/01/04 职场文书
酒店总经理欢迎词
2014/01/08 职场文书
岗位职责的构建方法
2014/02/01 职场文书
网络管理员岗位职责
2014/03/17 职场文书
《红军不怕远征难》教学反思
2014/04/14 职场文书
个人典型事迹材料
2014/12/30 职场文书
邀请函范文
2015/02/02 职场文书
校长师德表现自我评价
2015/03/05 职场文书
2015员工年度考核评语
2015/03/25 职场文书
检讨书模板大全
2015/05/07 职场文书
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
2021/06/05 Python
MySQL表类型 存储引擎 的选择
2021/11/11 MySQL
深入讲解Vue中父子组件通信与事件触发
2022/03/22 Vue.js
利用Apache Common将java对象池化的问题
2022/06/16 Servers