下面幾個例子關於tkinter介面更新的,簡單易懂,分享一下。
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 = strin**ar() 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 = strin**ar() self.timestr2 = strin**ar() 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基礎之控制項
控制項名稱功能說明 button 按鈕,滑鼠單擊時執行相應事件 label 標籤,顯示文字或圖示,起提示作用 entry 單行文字輸入框 text 多行文字輸入框 checkbutton 核取方塊按鈕 radiobutton 單選按鈕 frame 框架,在螢幕上顯示乙個矩形區域,多用作其他元件容器 ...
Python Tkinter 布局方式
優點問題,如何在介面上顯示 定義 所有的tkinter 元件都包含專用的幾何管理方法,這些方法是用來組織和管理整個父配件區中子配件的布局的。tkinter 提供了截然不同的三種幾何管理類 pack grid 和place。pack 幾何管理採用塊的方式組織配件,在快速生成介面設計中廣泛採用,若干元件...
python tkinter 單選 多選
單選按鈕 tkinter.radiobutton root,text a pack tkinter.radiobutton root,text b pack tkinter.radiobutton root,text c pack 預設被選中,且單個一組 variable把radiobutton分成...