python实现的简单窗口倒计时界面实例

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

python 计算积分图和haar特征的实例代码

下面的代码通过积分图计算一张图片的一种haar特征的所有可能的值。初步学习图像处理并尝试写代码,如有错误,欢迎指出。 import cv2 import numpy as np im...

浅析Python中的多条件排序实现

浅析Python中的多条件排序实现

多条件排序及itemgetter的应用 曾经客户端的同事用as写一大堆代码来排序,在得知Python排序往往只需要一行,惊讶无比,遂对python产生浓厚的兴趣。 之前在做足球的积分榜的...

python 获取sqlite3数据库的表名和表字段名的实例

Python中对sqlite3数据库进行操作时,经常需要用到字段名,然而对于sqlite使用select语句并不能象MySql等数据库一样返回带字段名的字典数据集。特别是对于一个不熟悉的...

Python+selenium点击网页上指定坐标的实例

Python+selenium点击网页上指定坐标的实例

例如有些页面元素很难获取,但是位置很固定,那么可以直接用坐标来进行操作 例如要对页面上的(x:200, y:100)进行操作,可以用如下代码: from selenium impor...

JupyterNotebook设置Python环境的方法步骤

JupyterNotebook设置Python环境的方法步骤

使用Python时,常遇到的一个问题就是Python和库的版本不同。Anaconda的env算是解决这个问题的一个好用的方法。但是,在使用Jupyter Notebook的时候,我却发现...