python实现倒计时小工具

 更新时间:2019年07月29日 15:50:34   作者:侠之大者为国为民  
这篇文章主要为大家详细介绍了python实现倒计时小工具,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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验证身份证信息实例代码

    这篇文章主要介绍了python验证身份证信息的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 为什么选择python编程语言入门黑客攻防 给你几个理由!

    为什么选择python编程语言入门黑客攻防 给你几个理由!

    为什么选择python编程语言入门黑客攻防,小编今天给你几个理由!Python语言的优点、Python黑客攻击优点,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • PyTorch 模型 onnx 文件导出及调用详情

    PyTorch 模型 onnx 文件导出及调用详情

    这篇文章主要介绍了PyTorch模型onnx文件导出及调用详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-07-07
  • python文件和文件夹复制函数

    python文件和文件夹复制函数

    这篇文章主要为大家详细介绍了python文件和文件夹复制函数的实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • matplotlib图例legend语法及设置的方法

    matplotlib图例legend语法及设置的方法

    这篇文章主要介绍了matplotlib图例legend语法及设置的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Python简单基础小程序的实例代码

    Python简单基础小程序的实例代码

    这篇文章主要介绍了Python简单基础小程序的实例代码,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-04-04
  • python opencv 图像处理之图像算数运算及修改颜色空间

    python opencv 图像处理之图像算数运算及修改颜色空间

    这篇文章主要介绍了python opencv 图像处理之图像算数运算及修改颜色空间,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-08-08
  • Python BeautifulSoup库的高级特性详解

    Python BeautifulSoup库的高级特性详解

    在Python的网络爬虫中,BeautifulSoup库是一个强大的工具,用于解析HTML和XML文档并提取其中的数据,在这篇文章中,我们将深入研究BeautifulSoup的一些高级特性,让您的爬虫工作更高效,更强大,需要的朋友可以参考下
    2023-08-08
  • 在Python中使用next()方法操作文件的教程

    在Python中使用next()方法操作文件的教程

    这篇文章主要介绍了在Python中使用next()方法操作文件的教程,是Python入门中的基础知识,需要的朋友可以参考下
    2015-05-05
  • Pytorch训练模型得到输出后计算F1-Score 和AUC的操作

    Pytorch训练模型得到输出后计算F1-Score 和AUC的操作

    这篇文章主要介绍了Pytorch训练模型得到输出后计算F1-Score 和AUC的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05

最新评论