我在多线程方面遇到了一些麻烦

时间:2017-01-13 10:24:48

标签: python multithreading user-interface queue

我的目标是在按下按钮(来自覆盆子pi上的GPIO)时更新GUI中的变量。 所以我想我会使用多线程。

当我启动应用程序时,只执行其中一个线程。我的主应用程序(GUI)无法启动。

我已经坚持这个问题好几天了,这让我发疯了!感谢您抽出时间来研究这个问题。(可能存在多个问题,对不起)

这是我的代码:

snelheidsvar = 0
q = queue.Queue()

class worker():

    def run(self):
        global snelheidsvar #this is the variable that is passed around
        global q

        try: 
           .....
           #this checks what button is pressed   

           elif j == 0 and i == 3: # one of the buttons                                   
           snelheidsvar +=1
           print("snelheidsvar is:", snelheidsvar)
           q.put(snelheidsvar) #putting the variable in a queue
           time.sleep(0.5)

        except KeyboardInterrupt:
            GPIO.cleanup()
class SampleApp(tk.Tk, threading.Thread):
    .....
    #this arranges what frame is shown and builds them

class some_class(tk.Frame,threading.Thread): #this is also a frame
    def __init__(self, parent, controller):

         .....
         self.some_label= tk.Label(some_frame, textvariable=self.local_variable) 
         #this is the variable that needs to be updated
         .....
         #putting in some more buttons and stuff

    def run(self):
         global snelheidsvar
         global q   

         q.get(snelheidsvar)
         self.local_variable = tk.IntVar()
         self.local_variable.set(snelheidsvar) 
         #taking it out of the queue and setting it to the local variable
if __name__ == "__main__":

    a = some_class(None, None) #not sure this should be None, None
    b = worker()
    c = SampleApp.mainloop() #error: parameter 'self' unfilled. ?
    bt = threading.Thread(target=b.run, args=())
    ct = threading.Thread(target=c.run, args=())
    bt.start()
    ct.start()

    #app = SampleApp()
    #before I used threading I called the sample app with:app.mainloop()

0 个答案:

没有答案