1

我正在尝试在运行绑定事件的同时更新画布背景。

(来自代码)会在do_popup弹出菜单中实现并conti不断改变画布背景颜色。我如何在画布不断更新时使用弹出选项。

示例代码:

from Tkinter import *
root = Tk()
def do_popup(event,w2):
    print w2 # inplace of print some popupmenu will be implemented
def conti():
    idt=1
    while idt==1:
        w.config(bg="red") # in place of red it will be a random color
        w.update_idletasks()
w= Canvas(root, width=600, height=600)
w.grid(row=0, column=0)
line1 = w.create_line(200,200,300,300, width=10, tags="line1", fill="black")
w.tag_bind(line1, "<Button-3>", lambda e, w2="test1" :do_popup(e,w2))
f = Frame(root)
f.grid(row=0, column=1, sticky=N)
f1=Button(f, text='visual', command=lambda :conti())
f1.grid(row=0, column=1,columnspan=1, sticky=W+N+S+E)
mainloop()

多处理会起作用吗?我正在使用带有 python 2.7.3 的 windows 7 32 位

提前致谢

4

1 回答 1

1

当您的脚本进入主循环时,将执行事件。

为了进行重复更新,我喜欢这样做:

def conti():
    try:
        w.config(bg="red") # in place of red it will be a random color
    finally:
        # start conti after 10 milliseconds, 
        root.after(10, conti) 
        # could also be 0ms to handle events
root.after(0, conti)

你可以root.mainloop看到

while not (root.quit was called):
    root.update()

这样你就可以做到:

root.quit()

conti自动停止。

没有与 中的线程一样的并发性mainloops。但是,当您创建自己的对话框并继续时,您可以将 mainloop() 放在某处conti。如果您使用模块tkMessageBox(Python2) 或tkinter.messagebox(Python3),那么您conti应该在对话框打开时运行。

这能回答你的问题吗?

PS:root.protocol("WM_DELETE_WINDOW", root.quit)关闭窗口时,请务必结束主循环。

于 2013-09-10T12:19:47.793 回答