时间:2021-05-22
网上看的两个例子关于tkinter界面更新的,简单易懂,分享一下。
例子_1:
代码_1:
from tkinter import Tk, Checkbutton, Labelfrom tkinter import StringVar, IntVarroot = 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:
代码_2:
from tkinter import *import timeclass 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 = Falseclass 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 更新方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Windows8系统更新有很多方法,这里主要给大家介绍两种界面更新方法。(很详细的图文介绍)1.首先介绍正常更新方法,在桌面计算机图标点右键属性或者鼠标移至右上
有些时候我们需要同时更新多个表中的数据那么就需要用到下面方法了:(1)sqlite多表更新方法复制代码代码如下://----------------------
本文实例讲述了Yii2.0实现的批量更新及批量插入功能。分享给大家供大家参考,具体如下:批量更新方法1/***批量更新循环周期*@paramarray$cond
最美天气app的用户可以选择天气更新方法,最方便的是自动更新,那么最美天气2016怎么更新呢?快和小编一起来看看最美天气2016版天气更新设置方法吧!软件名称:
5月7日消息,昨日,Shopee菲律宾平台表示,2021年6月起将推出新的商品分类法。新方法将提供更清晰的分类结构,优化搜索结果并提高商品曝光率。同时,新方法实