欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python tkinter label 更新方法

程序员文章站 2022-05-16 10:55:51
网上看的两个例子关于tkinter界面更新的,简单易懂,分享一下。 例子_1: 代码_1: from tkinter import tk, checkbu...

网上看的两个例子关于tkinter界面更新的,简单易懂,分享一下。

例子_1:

Python tkinter label 更新方法

代码_1:

from tkinter import tk, checkbutton, label
from tkinter import stringvar, intvar

root = tk()

text = stringvar()
text.set('old')
status = intvar()

def change():
 if status.get() == 1: # if clicked
  text.set('new')
 else:
  text.set('old')

cb = checkbutton(root, variable=status, command=change)
lb = label(root, textvariable=text)
cb.pack()
lb.pack()

root.mainloop()

例子_2:

Python tkinter label 更新方法

代码_2:

from tkinter import *
import time


class stopwatch(frame):
 '''实现一个秒表部件'''
 msec = 50
 def __init__(self, parent=none, **kw):
  frame.__init__(self, parent, kw)
  self._start = 0.0
  self._elapsedtime = 0.0
  self._running = false
  self.timestr = stringvar()
  self.makewidgets()
  self.flag = true
 def makewidgets(self):
  '''制作时间标签'''
  l = label(self, textvariable = self.timestr)
  self._settime(self._elapsedtime)
  l.pack(fill = x, expand = no, pady = 2, padx = 2)
 def _update(self):
  self._elapsedtime = time.time() - self._start
  self._settime(self._elapsedtime)
  self._timer = self.after(self.msec, self._update)
 def _settime(self, elap):
  '''将时间格式改为 分:秒:百分秒'''
  minutes = int(elap/60)
  seconds = int(elap-minutes*60.0)
  hseconds = int((elap - minutes*60.0 - seconds) *100)
  self.timestr.set('%2d:%2d:%2d' %(minutes, seconds, hseconds))
 def start(self):
  if not self._running:
   self._start = time.time() - self._elapsedtime
   self._update()
   self._running = true
 def stop(self):
  '''停止秒表'''
  if self._running:
   self.after_cancel(self._timer)
   self._elapsedtime = time.time() - self._start
   self._settime(self._elapsedtime)
   self._running = false
 def reset(self):
  '''重设秒表'''
  self._start = time.time()
  self._elapsedtime = 0.0
  self._settime(self._elapsedtime)


 def stopwatch(self):
  if self.flag == true:
   self.pack(side = top)
   button(self, text = 'start', command = self.start).pack(side = left)
   button(self, text = 'stop', command = self.stop).pack(side = left)
   button(self, text = 'reset', command = self.reset).pack(side = left)
   button(self, text = 'quit', command = self.quit).pack(side = left)  
  self.flag = false


class watch(frame):
 msec = 1000
 def __init__(self, parent=none, **kw):
   frame.__init__(self, parent, kw)
   self._running = false
   self.timestr1 = stringvar()
   self.timestr2 = stringvar()
   self.makewidgets()
   self.flag = true
 def makewidgets(self):
  l1 = label(self, textvariable = self.timestr1)
  l2 = label(self, textvariable = self.timestr2)
  l1.pack()
  l2.pack()
 def _update(self):
  self._settime()
  self.timer = self.after(self.msec, self._update)
 def _settime(self):
  today1 = str(time.strftime('%y-%m-%d', time.localtime(time.time())))
  time1 = str(time.strftime('%h:%m:%s', time.localtime(time.time())))
  self.timestr1.set(today1)
  self.timestr2.set(time1)
 def start(self):
  self._update()
  self.pack(side = top)

if __name__ == '__main__':
 def main():
  root = tk()
  root.geometry('250x150')
  frame1 = frame(root)
  frame1.pack(side = bottom)
  sw = stopwatch(root)
  stpwtch = button(frame1, text = '秒表', command = sw.stopwatch)
  stpwtch.pack(side = right)
  mw = watch(root)
  mywatch = button(frame1, text = '时钟', command = mw.start)
  mywatch.pack(side = left)
  root.mainloop()
 main()

以上这篇python tkinter label 更新方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。