I m试图起草一个方案,从港口序列连接获取数据,并根据这些数据实时自动更新Tkinter窗口。
我试图为窗户建立一个单独的透镜,定期从主线上获取现有数据并更新窗户。
serialdata = []
data = True
class SensorThread(threading.Thread):
def run(self):
serial = serial.Serial( dev/tty.usbmodem1d11 , 9600)
try:
while True:
serialdata.append(serial.readline())
except KeyboardInterrupt:
serial.close()
exit()
class GuiThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.root = Tk()
self.lbl = Label(self.root, text="")
def run(self):
self.lbl(pack)
self.lbl.after(1000, self.updateGUI)
self.root.mainloop()
def updateGUI(self):
msg = "Data is True" if data else "Data is False"
self.lbl["text"] = msg
self.root.update()
self.lbl.after(1000, self.updateGUI)
if __name == "__main__":
SensorThread().start()
GuiThread().start()
try:
while True:
# A bunch of analysis that sets either data = True or data = False based on serialdata
except KeyboardInterrupt:
exit()
顺便说一句给我这个错误:
Exception in thread Thread-2: Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner self.run() File "analysis.py", line 52, in run self.lbl1.pack() File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 1764, in pack_configure + self._options(cnf, kw)) RuntimeError: main thread is not in main loop
当我看一看这一错误时,我多见有人试图从两条不同的路面与窗户互动的哨所,但我认为我没有这样做。 任何想法? 感谢如此之多!