我的目标是在按下按钮(来自raspberry 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()
目前没有回答
相关问题 更多 >
编程相关推荐