我正在尝试使用tkinter创建一个基本的控制台窗口。我当前的代码有两个类,一个用于gui,另一个用于向控制台添加文本。文本添加类与gui(在主线程中运行)在不同的线程中运行。我设置了一个队列来在两个线程之间进行通信。除了排队,一切正常。当我向队列中添加文本时,它可以从工作线程读取,但不能从主线程读取。这是我的密码:
import threading, sys, os, time, Queue
from Tkinter import *
class consolegui:
"""Main GUI for the program"""
def __init__(self, root):
self.root=root
self.cout=Text(self.root, width=80)
self.root.after(100, self.process_queue)
self.cout.config(state=DISABLED)
self.cout.pack(side=TOP)
self.bottomf=Frame(self.root, height=1, width=80)
self.bottomf.pack(side=BOTTOM)
self.cin=Entry(self.bottomf, width=100)
self.cin.pack(side=LEFT)
self.enter=Button(self.bottomf, text="send", command=self.on_click)
self.cin.bind('<Return>', self.on_click)
self.enter.pack(side=BOTTOM)
self.queue = Queue.Queue()
worker(self.queue).start()
def on_click(self, *args):
self.cout.config(state=NORMAL)
self.cout.insert(END, self.cin.get()+'\n')
self.cout.config(state=DISABLED)
self.cin.delete(0, END)
def add_text(self, text):
self.cout.insert(END, text+'\n')
def process_queue(self):
print "reading queue"
try:
msg = self.queue.get(0)
print "found items in queue!"
self.add_text(msg)
with self.queue.mutex:
self.queue.queue.clear()
except Queue.Empty:
print "found nothing"
self.root.after(100, self.process_queue)
class worker(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
time.sleep(5)
print "adding to queue"
self.queue.put("Task finished")
print self.queue.get(0)
if __name__ == "__main__":
root = Tk()
console=consolegui(root)
root.mainloop()
谢谢你的帮助!你知道吗
你确定它不能从主线程读取吗?您正在使用一个非阻塞的
get
,并在检查之间休眠。worker只向队列中添加一个条目,并立即将其读回(这将清空队列)。您创建的竞争条件要求PythonGIL在检查之间的100秒处交换到put
和get
之间的主线程上(如前所述,看起来您是在5秒之后添加的,所以可能还有~95秒的时间,而且这种竞争永远不会发生)。你知道吗简而言之:您只能
get
一次来自Queue
的条目。如果工作线程立即读取它,则主线程不能(反之亦然)。你知道吗相关问题 更多 >
编程相关推荐