如何多次执行while循环?

时间:2016-11-07 14:30:57

标签: python user-interface graph while-loop

我有一个程序,可以作为几个arduino板的GUI。我试图在动态图表中显示传感器的结果。

问题:为什么我的GUI单元没有打印5次? - 它没有图表就可以工作

我对最终结果的看法如下: enter image description here 不幸的是我的结果是:

enter image description here 我似乎无法弄清楚为什么我的代码不打印GUI 5次。 这是我的图表代码:

# pre-load dummy data
    for i in range(0, 30):
        self.values.append(0)

    for a in range(0, 30):
        self.values2.append(0)

    while True:
        while (self.serialArduino.inWaiting() == 0):
            pass
        self.data = self.serialArduino.readline().decode('utf-8')  # maakt connectie met arduino

        self.valueRead, self.valueRead2 = [int(x.encode()) for x in self.data.split(",")]

        self.valueInInt = int(self.valueRead)
        self.valueInInt2 = int(self.valueRead2)
        print("lux %d" % self.valueInInt)
        print("temp %d" % self.valueInInt2)

        self.values.append(self.valueInInt)
        self.values.pop(0)
        self.values2.append(self.valueInInt2)
        self.values2.pop(0)
        drawnow(self.plotValues)

        self.f.tight_layout(pad=0.8, w_pad=0.5, h_pad=1.0)
        self.canvas = FigureCanvasTkAgg(self.f, master=self.frame1, )
        self.canvas.get_tk_widget().grid(row=8, column=0, columnspan=2)

def plotValues(self):
    self.f = plt.Figure(figsize=(4, 5), dpi=90)
    self.a = self.f.add_subplot(211)
    self.a.plot(self.values2, 'bx-', label='TEMP')
    self.a.set_title('Tempertuur')
    self.a.set_xlabel('Uren')
    self.a.set_ylabel('Celsius')
    self.a.grid(True)


    self.b = self.f.add_subplot(212)
    self.b.plot(self.values, 'rx-', label='LUX')
    self.b.set_title('Lux')
    self.b.set_xlabel('Uren')
    self.b.set_ylabel('Lichtintensiteit')
    self.b.grid(True)

包含图表的类的构造函数:

self.values = []
    self.values2 = []

    self.plt = plt.ion()
    self.cnt = 0

    self.serialArduino = serial.Serial('COM7', 9600)

    self.frame1 = Frame(master)
    self.frame1.pack(side=LEFT)

GUI本身是来自不同类的调用:

class GUI_Root:
def __init__(self):
    print("hoi")
    window = Tk()
    window.title("Project: Embedded Systems")

    rootframe = Frame(window, width=1800, height=750)
    rootframe.pack()
    for y in range(0, 5):
        Unit(rootframe)

    window.mainloop()

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。它可能不是代码,但它可以工作。 每个Gui单位代表一个Arduino单位。所以我检查是否使用了comport。对于正在使用的每个comport,都会创建一个Gui单元。这解决了我的问题。

class GUI_Root:
def __init__(self):
    print("hoi")
    window = Tk()
    print("test1")
    window.title("Project: Embedded Systems")
    print("test2")
    rootframe = Frame(window, width=1800, height=750)
    rootframe.pack()


    try:
        Unit(rootframe, 'COM3' )

    except: print("Com3 is niet aangesloten")

    try:
        Unit(rootframe, 'COM4' )
    except:
        print("Com4 is niet aangesloten")

    try:
        Unit(rootframe, 'COM5' )
    except:
        print("Com5 is niet aangesloten")

    try:
        Unit(rootframe, 'COM6' )
    except:
        print("Com6 is niet aangesloten")

    try:
        Unit(rootframe, 'COM7' )
    except:
        print("Com7 is niet aangesloten")

    try:
        Unit(rootframe, 'COM8' )
    except:
        print("Com8 is niet aangesloten")

    window.mainloop()

问题出在GUI_Root类中。