当前位置: 首页 > 编程笔记 >

python实现倒计时小工具

禄豪
2023-03-14
本文向大家介绍python实现倒计时小工具,包括了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍jQuery实现的倒计时效果实例小结,包括了jQuery实现的倒计时效果实例小结的使用技巧和注意事项,需要的朋友参考一下 本文实例总结了jQuery实现的倒计时效果。分享给大家供大家参考,具体如下: 当我们在webservice里读取到数据库中的时间字段后,把时间分割判断。Javascript代码如下: 多个倒计时>>> 代码: 更多关于jQuery相关内容感兴趣的读者可查看本站专题

  • 本文向大家介绍python实现windows倒计时锁屏功能,包括了python实现windows倒计时锁屏功能的使用技巧和注意事项,需要的朋友参考一下 python实现windows倒计时锁屏功能 知识点扩展: Python在windows锁屏的代码 总结 以上所述是小编给大家介绍的python实现windows倒计时锁屏功能 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家

  • 本文向大家介绍小程序实现列表倒计时功能,包括了小程序实现列表倒计时功能的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了小程序实现列表倒计时的具体代码,供大家参考,具体内容如下 效果 HTML代码 CSS代码 JS代码(得到后台数据查询用FIND方法插入字段,直接遍历会有问题) 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍Unity实现倒计时功能,包括了Unity实现倒计时功能的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Unity实现倒计时功能的具体代码,供大家参考,具体内容如下 有两种思路可以实现倒计时,一个是Update,另一个是协程。这里只展示核心的算法思路,有收获的还请点个赞哦 Update 首先定义三个变量,访问权限按需求设置: 思路: 首先用GameTime初始化Time

  • 本文向大家介绍Flutter倒计时/计时器的实现代码,包括了Flutter倒计时/计时器的实现代码的使用技巧和注意事项,需要的朋友参考一下 在我们实现某些功能时,可能会有倒计时的需求。 比如发送短信验证码,发送成功后可能要求用户一段时间内不能再次发送,这时候我们就需要进行倒计时,时间到了才允许再次操作。 如下图: 为了实现这样场景的需求,我们需要使用 Timer.periodic 。 一、引入Ti

  • 本文向大家介绍Javascript实现倒计时时差效果,包括了Javascript实现倒计时时差效果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了js倒计时时差效果的实现代码,供大家参考,具体内容如下 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。