本文實例為大家分享了python實現(xiàn)倒計時小工具的具體代碼,供大家參考,具體內(nèi)容如下
#!/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('<
>', 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="設(shè)定時間間隔").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)
root.bind("
",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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

