# 引入模块 import tkinter as tk import time # 设计一个时钟类 class clock(tk.Frame): wait_time=1000 def __init__(self, parent=None, **kw): tk.Frame.__init__(self, parent, kw) self.time_str=tk.StringVar() tk.Label(self, textvariable = self.time_str).pack() def _update(self): self.time_str.set(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) self.timer = self.after(self.wait_time, self._update) def start(self): self._update() self.pack(side = tk.TOP) def main(): root = tk.Tk() mw = clock(root) mywatch = tk.Button(root, text = '时钟', command = mw.start) mywatch.pack(side = tk.LEFT) root.mainloop() main()