来自线程的Python 3.0 tkinter对话框

时间:2011-03-29 01:13:07

标签: python multithreading dialog tkinter

我对Python比较陌生,尤其是tkinter新手。在下面的示例代码中,对话框在直接创建时工作正常(左按钮),但如果从线程(右按钮)创建对话框,应用程序将无响应。什么似乎是问题?线程可以启动对话框吗?如果是这样,怎么样?

from tkinter import *
from tkinter import ttk
import threading 

count = 0

def makeDialog():
    global count
    count = count + 1;
    messagebox.showinfo('Click Counter','Click #{0}'.format(count))

def makeThread():
    threading.Thread(target=makeDialog).start()

root = Tk()
root.title("Dialogs")
mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
button1 = ttk.Button(mainframe, text="Dialog", command=makeDialog)
button1.grid(row=1, column=1)
button2 = ttk.Button(mainframe, text="Thread Dialog", command=makeThread)
button2.grid(row=1, column=2)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

见Alex Martelli的answer to a similar question