シリアルポート接続からデータを取得し、そのデータに基づいてリアルタイムでTkinterウィンドウを自動的に更新するプログラムを作成しようとしています。シリアルデータに基づいて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
私はこのエラーをGoogleに取り上げると、2人の異なるスレッドからウィンドウとやりとりしようとしている投稿がほとんどですが、私はそれをやっているとは思いません。何か案は?本当にありがとう!
スレッドではなくTKパートを実行しようとしましたか?つまり、スレッドでシリアルポートのものを実行するだけで、TKのものはメインプロセスに残ります。私はそれが動作する可能性があると思う... –
シリアルポートのデータを取得するためのスレッドと同様に、データ解析ループのための別のスレッド?私はそのショットを与えるでしょう。 – user1363445