0

マイクロ プロセッサから受信した信号を継続的にプロットする GUI を作成しようとしています。クラスのみを使用してこれを実現しようとしましたが、GUI クラスのみが oppend であったため失敗しました。今、私はスレッド化を実装しました (または、少なくとも実装したと思います!?) が、各スレッドは 1 回しか実行されません。tkinter のメインループがどのように機能するかを理解していないと思わせるので、スレッドがアクティブになるようにコードを作り直すことはできますか?

import Tkinter
import tkMessageBox as messagebox
from serial import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
import time
import threading

go=0

x=[0.0001,0.0002,0.0003]
y=[3600,1000,2000]
stid=time.time()

root = Tkinter.Tk()
root.title("Serial gui")

class SensorThread(threading.Thread):
    def run(self):
        global run
        global x
        global y
        global stid
        print "run er ok"
        if go==1:
            print "go er ok"
            ser = Serial(5, 9600, timeout=1)
            f=ser.read(4)
            ser.close()
            x.append(time.time()-stid)
            y.append(f)


class Foo:
    def __init__(self, master):
        print "foo ok"

        frame = Tkinter.Frame(root)

        self.button_left = Tkinter.Button(frame,text="Start",
                                command=self.start)
        self.button_left.pack(side="left")

        self.button_right = Tkinter.Button(frame,text="Stop",
                                command=self.stop)
        self.button_right.pack(side="right")

        self.button_midt = Tkinter.Button(frame, text='Quit', command=self.terminate)
        self.button_midt.pack(side="bottom")

        fig = Figure()
        ax = fig.add_subplot(111, axisbg='black')

        canvas = FigureCanvasTkAgg(fig,master=master)
        canvas.show()
        canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        frame.pack()

        line1, = ax.plot(x, y, 'r-') # Returns a tuple of line objects, thus the comma
        line1.set_ydata(y)
        fig.canvas.draw()


    def start(self):
        global go
        go=1
        print go

    def stop(self):
        global go
        go=0
        print go

    def terminate(self):
        root.quit()     # stops mainloop
        root.destroy()  # this is necessary on Windows to prevent
                        # Fatal Python Error: PyEval_RestoreThread: NULL tstate

if __name__ == "__main__":
    SensorThread().run()
    Foo(root)
    root.mainloop() 

このコードをプロットをリアルタイムで更新するプログラムにするのを手伝ってくれる人がいるといいのですが。

Super I は、プログラムで次のように変更しました。

class SensorThread(threading.Thread):
def run(self):
    global run
    global x
    global y
    global stid
    #print "run er ok"
    if go==1:
        print "go er ok"
        ser = Serial(17, 9600, timeout=1)
        f=ser.read(4)
        ser.close()
        x.append(time.time()-stid)
        y.append(f)
        SensorThread().start()
    else:
        SensorThread().start()

class Foo:
....

if __name__ == "__main__":
SensorThread().start()
Foo(root)
root.mainloop() 

しかし、プロットされている図はまだ更新されません。Foo クラスでこれを行うべきではありませんか? また、Python スクリプトを終了または終了しても、まだ CPU パワーの 50% を使用しているのは、おそらくセンサー スレッドが永久に実行されるためです!?

4

1 に答える 1

0

このレシピをチェックしてください、それはそれを行う方法を示しています:

http://code.activestate.com/recipes/82965-threads-tkinter-and-asynchronous-io/

于 2013-01-23T14:40:50.667 に答える