将代码插入具有Class类型格式的Tkinter窗口

时间:2016-08-23 15:47:10

标签: python-2.7 user-interface matplotlib tkinter

我将以下代码放入具有类格式的tkinter窗口时遇到困难。 当前形式的代码从Arduino板读取数据并连续绘制到两个单独的图形上。我有兴趣将它放入Tkinter GUI,以便我可以操作数据并最终添加新功能并构建应用程序。

我要做的就是按照它的方式执行此代码功能,但重新格式化以便它适合带有类的Tkinter窗口。

非常感谢任何帮助。

import serial
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
import plotly.plotly as py
connected = False

comPort = 'COM4'

ser = serial.Serial(comPort, 9600) #sets up serial connection (make sure baud rate is correct - matches Arduino)

while not connected:
    serin = ser.read()
    connected = True


plt.ion()                    #sets plot to animation mode

length = 10                 #determines length of data taking session (in data points)
x = [0]*length               #create empty variable of length of test
y = [0]*length
z = [0]*length

fig = plt.figure()
gs = gridspec.GridSpec(7, 7)                #sets up grid space
ax1 = fig.add_subplot(gs[0,0])              #determines location of graph
ax1.axes.get_xaxis().set_visible(False)     #Hides x,y axis
ax1.axes.get_yaxis().set_visible(False)
ax1.set_ylim([1,30])                        #this is how to set the yaxis
ax2 = fig.add_subplot(gs[0,5])              #determines location of graph
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)

ax2.set_ylim([1,30])


xline, = ax1.plot(x)         #sets up future lines to be modified
yline, = ax2.plot(y)

for i in range(10000):     #while you are taking data
    data = ser.readline()    #reads until it gets a carriage return. MAKE SURE THERE IS A CARRIAGE RETURN OR IT READS FOREVER
    sep = data.split()      #splits string into a list at the tabs


    x.append(int(sep[0]))   #add new value as int to current list
    y.append(int(sep[0]))


    del x[0]
    del y[0]


    xline.set_xdata(np.arange(len(x))) #sets xdata to new list length .. code works without this
    yline.set_xdata(np.arange(len(y)))


    xline.set_ydata(x)                 #sets ydata to new list
    yline.set_ydata(y)


    plt.pause(0.001)                   #in seconds
    plt.show()                         #draws new plot

ser.close() #closes serial connection (very important to his into the cmd line to close the connection)

0 个答案:

没有答案