gpt4 book ai didi

带有线程的python tkinter导致崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:59 27 4
gpt4 key购买 nike

我已经使用线程编写了一个 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 中打开此代码并运行它,它有时会出现挂起状态。所以要重现,将 sleep 时间从 0.01 修改为 0.1,然后运行。在此之后停止应用程序,并将其修改回 0.01,保存并运行它。这次它将运行,一段时间后,python 将停止工作。我正在使用 Windows 7(64 位)。

问题

我已经将它提交给 python bugs,但被拒绝了。但我从一个 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()

最佳答案

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

关于带有线程的python tkinter导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14168346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com