Python 2.7 使用类类型格式将代码插入Tkinter窗口

Python 2.7 使用类类型格式将代码插入Tkinter窗口,python-2.7,user-interface,matplotlib,tkinter,Python 2.7,User Interface,Matplotlib,Tkinter,我很难将以下代码放入具有类格式的tkinter窗口中。 当前形式的代码从Arduino板读取数据,并将其连续绘制到两个单独的图形上。我感兴趣的是将其放入Tkinter GUI中,这样我就可以操作数据,最终添加新功能并构建应用程序 我所要做的就是让这个代码函数按照它的方式运行,但要重新格式化,使其适合包含类的Tkinter窗口 任何帮助都将不胜感激 import serial import matplotlib.pyplot as plt import numpy as np from matpl

我很难将以下代码放入具有类格式的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)

我建议你自己重新格式化,如果你被卡住了,再回来问一个具体的问题。正如所写的,这有点太宽泛了,因为您要求我们为您重写代码。更具体地说。在将代码转换为Tkinter时,是否有一个特定的或通用的模板可以遵循?我建议您自己尝试重新格式化它,如果遇到问题,请返回这里并提出一个特定的问题。正如所写的,这有点太宽泛了,因为您要求我们为您重写代码。更具体地说。在将代码转换为Tkinter时,是否可以遵循特定的或通用的模板?