5

我已经使用线程编写了一个 python tkinter 代码,以便 tkinter 向导通过在主线程中运行的 tkinter mainloop 和在单独线程中运行的后台进程自动更新。但我注意到,在运行代码一段时间后,python 崩溃了。此外,它本质上是随机的,但 python 大部分时间都会崩溃。我写了一个小测试代码可以显示这个问题(我的原始代码与此类似,但有一些真实的过程和许多其他功能,所以我分享测试代码)。

######################################################################
 # Test Code for Tkinter with threads
import Tkinter
import threading
import Queue
import time

# Data Generator which will generate Data
def GenerateData(q):
    for i in range(1000000):
        #print "Generating Some Data, Iteration %s" %(i)
        time.sleep(0.01)
        q.put("Some Data from iteration %s. Putting this data in the queue for testing" %(i))

# Queue which will be used for storing Data
q = Queue.Queue()

def QueueHandler(widinst, q):
    linecount = 0
    while True:
        print "Running"
        if not q.empty():
            str = q.get()
            linecount = linecount + 1
            widinst.configure(state="normal")
            str = str + "\n"
            widinst.insert("end", str)
            if linecount > 100:
                widinst.delete('1.0', '2.0')
                linecount = linecount - 1
            widinst.see('end')
            widinst.configure(state="disabled")

# Create a thread and run GUI & QueueHadnler in it
tk = Tkinter.Tk()
scrollbar = Tkinter.Scrollbar(tk)
scrollbar.pack(side='right', fill='y' )
text_wid = Tkinter.Text(tk,yscrollcommand=scrollbar.set)
text_wid.pack()
t1 = threading.Thread(target=GenerateData, args=(q,))
t2 = threading.Thread(target=QueueHandler, args=(text_wid,q))
t2.start()
t1.start()

tk.mainloop()
######################################################################

重现:

如果您在 IDLE 中打开此代码并运行它,它有时会显示为处于挂起状态。所以要重现,将睡眠时间从 0.01 修改为 0.1 并运行它。在此之后停止应用程序,并将其修改回 0.01,保存并运行它。这次它将运行,一段时间后,python 将停止工作。我正在使用 Windows 7(64 位)。

问题

我已将其提交给 python 错误并被拒绝。但是我从一个stackoverflow问题中得到了这个想法,使用队列在tkinter中写入。有人可以建议应该做些什么来处理它。

编辑代码:

# Test Code for Tkinter with threads
import Tkinter
import threading
import Queue
import time

# Data Generator which will generate Data
def GenerateData(q):
    for i in range(1000000):
        #print "Generating Some Data, Iteration %s" %(i)
        time.sleep(0)
        q.put("Some Data from iteration %s. Putting this data in the queue for testing" %(i))

# Queue which will be used for storing Data
q = Queue.Queue()

def QueueHandler():
    global widinst, q
    linecount = 0
    if not q.empty():
        str = q.get()
        linecount = linecount + 1
        widinst.configure(state="normal")
        str = str + "\n"
        widinst.insert("end", str)
        if linecount > 100:
            widinst.delete('1.0', '2.0')
            linecount = linecount - 1
        widinst.see('end')
        widinst.configure(state="disabled")
        tk.after(1,QueueHandler)

# Create a thread and run GUI & QueueHadnler in it
tk = Tkinter.Tk()
scrollbar = Tkinter.Scrollbar(tk)
scrollbar.pack(side='right', fill='y' )
text_wid = Tkinter.Text(tk,yscrollcommand=scrollbar.set)
text_wid.pack()
t1 = threading.Thread(target=GenerateData, args=(q,))
#t2 = threading.Thread(target=QueueHandler, args=(text_wid,q))
#t2.start()
widinst = text_wid
t1.start()
tk.after(1,QueueHandler)
tk.mainloop()
4

3 回答 3

7

Tkinter 不是线程安全的;除了主线程之外,您无法从任何地方访问 Tkinter 小部件。您将需要重构代码以便QueueHandler在主线程中运行。

于 2013-01-05T14:13:02.463 回答
3

正如 Bryan 所说,Tkinter 不是线程安全的。这是一个试图做到这一点的修改:http: //tkinter.unpythonic.net/wiki/mtTkinter

于 2013-01-05T19:44:44.293 回答
1

Tkinter设计为线程安全的,但不是由于2.x3.x中的错误。

在发布修复程序之前(以及在旧版本中),您应该使用mtTkinter(设计为直接替代)作为解决方法。

于 2018-04-14T10:37:44.367 回答