Python Tkinter多线程队列永远等待

Python Tkinter多线程队列永远等待,python,multithreading,python-2.7,tkinter,queue,Python,Multithreading,Python 2.7,Tkinter,Queue,我的tkinter应用程序有两个线程(我需要它们),我在stackoverflow上发现了一个很棒的函数tkloop(),它只为tkinter制作了一个主线程;它使用队列。执行此操作时,它会显示tkMessagebox: self.q.put((tkMessageBox.askyesno,("Cannot download it", "Download \"" + tag +"\" via internet site"),{}, self.q1 )) 但是,当我创建自己的函数时,它不知何故没有

我的tkinter应用程序有两个线程(我需要它们),我在stackoverflow上发现了一个很棒的函数tkloop(),它只为tkinter制作了一个主线程;它使用队列。执行此操作时,它会显示tkMessagebox:

self.q.put((tkMessageBox.askyesno,("Cannot download it", "Download \"" + tag +"\" via internet site"),{}, self.q1 ))
但是,当我创建自己的函数时,它不知何故没有执行该函数

self.q.put((self.topleveldo,(resultlist),{},None))
只有一个类应用程序:

self.q=Queue()
def tkloop(self):
    try:
        while True:
            f, a, k, qr = self.q.get_nowait()
            print f
            r = f(*a,**k)

            if qr: qr.put(r)
    except:
        pass
    self.okno.after(100, self.tkloop)
def topleveldo(resultlist):
    print ("executed - actually this never prints")
    self.choice=Toplevel()
    self.choices=Listbox(self.choice)
    for result in resultlist:
        self.choices.insert(END,str(result))
    choosebutton=Button(text="Vybrat",command=self.readchoice)
def readchoice(self):
    choice=int(self.choices.curselection())
    self.choice.destroy()
    self.q1.put(choice)
类App中方法中的另一个代码,由第二个线程运行:

def method(self):
    self.q1=Queue()
    self.q.put((self.topleveldo,(resultlist),{},None))
    print ("it still prints this, but then it waits forever for q1.get, because self.topleveldo is never executed")
    choice=self.q1.get()

在tkloop异常处理程序中记录错误-现在您不知道调用topleveldo是否失败(可能失败了)。问题在于(1)
(resultlist)
只是resultlist,而不是像topleveldo所期望的那样具有1个参数的元组。(2)tkloop仅在消息中的第四个参数是队列时才放置响应。您可以通过以下方式进行修复:

self.q.put((self.topleveldo,(resultlist,),{},self.q1))
增加:

tkloop应该始终返回一条消息,即使它捕获了一个异常,这样调用者就可以可靠地调用q.get()来获得响应。一种方法是返回被调用程序引发的异常:

def tkloop(self):
    while True:
        try:
            f, a, k, qr = self.q.get_nowait()
            print f
            r = f(*a,**k)
            if qr: 
                qr.put(r)
            del f,a,k,qr
    except Exception, e:
        if qr:
            try:
                qr.put(e)
            except:
                # log it
                pass
    self.okno.after(100, self.tkloop)

好的,我将尝试tuple(resultlist),但是topleveldo不应该返回任何内容。readchoice做自我。q1。放(选择)哦,谢谢!只需要捕获并打印异常。topleveldo()只接受1个参数(给定11个),就像您所说的,似乎没有元组。那么,当tuple(resultlist)不起作用时,我该如何处理tuple呢?我更新了响应以显示
(resultlist,)
(注意额外的逗号)以及其他内容。我没有注意到您在choosebutton中使用self.q1的方式。我尝试过(resultlist,),{},没有,但我得到:topleveldo()正好接受1个参数(给定2个)抱歉,topleveldo:D没有self