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

yipeiwu_com5年前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使用xlrd和xlwt读写Excel文件的实例代码

安装模块 如果使用的是Linux系统,并且安装了pip,可以直接使用pip安装xlrd, xlwt: pip install xlwt pip install xlrd 也可以从官...

对python函数签名的方法详解

函数签名对象,表示调用函数的方式,即定义了函数的输入和输出。 在Python中,可以使用标准库inspect的一些方法或类,来操作或创建函数签名。 获取函数签名及参数 使用标准库的sig...

利用python3随机生成中文字符的实现方法

前言 运行环境在Python3.6下,Python2的解决方案网上有很多.,想学习python2实现的朋友们可以参考这篇文章:/post/34884.htm,下面来一起看看详细的介绍吧。...

Python3遍历目录树实现方法

本文实例讲述了Python3遍历目录树的方法。分享给大家供大家参考。具体实现方法如下: import os, fnmatch # 检查一个目录,后者某个包含子目录的目录树,并根据某种...

使用Python来开发微信功能

使用Python来开发微信功能

在移动社交时代,微信已经成为我们生活不可或缺的一部分。2017年的《微信数据报告》中显示:每天有380亿条消息从微信上发出,其中6亿条是语音消息,有350万个活跃的公众账号,并存在着8亿...