我正在尝试大多数after
小部件拥有的Tkinter
方法。
这是我的演示:
import Tkinter as tk
import ttk
import random
# set up main window
root = tk.Tk()
root.minsize(300, 100)
# set up function that gets a random widget from the UI
def all_widgets(root=root):
for w in root.winfo_children():
yield w
if isinstance(w, ttk.Frame):
for ww in walk_widgets(w):
yield ww
def random_widget():
return random.choice(list(all_widgets()))
# create two scales ...
kw = dict(master=root, from_=0, to=10, length=500)
scale1 = ttk.Scale(**kw)
scale1.grid(row=0, column=0, pady=20)
scale2 = ttk.Scale(**kw)
scale2.grid(row=1, column=0)
# ... that jump randomly once a second
delay = 1000
def jump(scale):
scale.set(random.randint(0, 10))
random_widget().after(delay, lambda: jump(scale))
jump(scale1)
jump(scale2)
root.mainloop()
这将创建两个刻度,它们每秒随机跳跃一次。
它使用随机小部件的after
方法。我是否缺少某些东西,或者在我叫after
的小部件上完全是任意的?