我已经使用线程编写了一个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 …Run Code Online (Sandbox Code Playgroud)