本文实例讲述了python实现的简单窗口倒计时界面。分享给大家供大家参考。具体分析如下:
下面的代码通过tkinter制作windows窗口界面,然后时间了一个简单的倒计时功能,代码可以直接运行
# countdown using tkinter
from tkinter import *
import time
import tkmessagebox
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=”start”,command=self.start)
self.hi_there.pack(side=left)
self.button = button(frame,text=”quit”,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):
lbl1[“text”] = k
root.update()
time.sleep(1)
lbl1.config(bg=’red’)
lbl1.config(fg=’white’)
lbl1[“text”] = “time up!”
tkmessagebox.showinfo(“time up!”,”time up!”)
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程序设计有所帮助。