同时运行Tkinter窗口和Matplotlib图

时间:2018-10-25 20:22:30

标签: python matplotlib tkinter

我正在编写一个Python脚本,该脚本可视化来自压力传感器的数据,并执行定时测试。我将数据绘制在实时Matplotlib图上,并且还有一个Tkinter窗口来显示计时器以及瞬时传感器读数。这是到目前为止我在想的顺序:

  1. 要求用户在Tkinter窗口中输入测试时间。用户输入时间后丢弃此窗口
  2. 创建实时图并开始读取数据
  3. 打开Tkinter窗口以显示计时器和瞬时数据
  4. 测试完成后,处置Tkinter窗口和图形。提示用户输入下一次测试的时间。重复步骤2-4

这是我到目前为止的代码。为了节省空间,我省略了一些不重要的事情:

#imports
#left out imports to save space

#ADS code
adc = Adafruit_ADS1x15.ADS1115(address=0x48, busnum=1)
GAIN = 1

global t
#ask user for input time
def gettime():
  global t
    def gettext(self):
        global e
        global t
        string = e.get()
        t = int(string)
        root.destroy()

root = Tk()
root.geometry("1024x600")
root.title('Enter the time')
root.bind('<KP_Enter>', gettext)

labelfont = ('times', 70, 'bold')

label = Label(root, text="Enter the time:")
label.config(font=labelfont)
label.config(height=3, width=20)
label.pack()

global e
e = Entry(root)
e.pack(ipady=3)
e.focus_set()

root.mainloop()

#create Matplotlib live graph    
def makePlot():

    # Parameters
    x_len = t        # Number of points to display
    y_range = [0,1]  # Range of possible Y values to display

    # Create figure for plotting
    fig = plt.figure()
    mng = plt.get_current_fig_manager()
    mng.resize(*mng.window.maxsize())
    ax = fig.add_subplot(1, 1, 1)
    xs = list(range(0, x_len))
    ys = [0] * x_len
    ax.set_ylim(y_range)

    # Create a blank line. We will update the line in animate
    line, = ax.plot(xs, ys)

    # Add labels
    plt.title('Pressure')
    plt.xlabel('Time')
    plt.ylabel('Pressure(psi)')

    # This function is called periodically from FuncAnimation
    def animate(i, ys):

        #get value from sensor
        x1 = adc.read_adc(0, gain=GAIN)
        x2 = ((x1*4.096)/32767.0)*.00176678

        # Add y to list
        ys.append(x2)

        # Limit y list to set number of items
        ys = ys[-x_len:]

        # Update line with new Y values
        line.set_ydata(ys)

        return line,

    # Set up plot to call animate() function periodically
    ani = animation.FuncAnimation(fig,
        animate,
        fargs=(ys,),
        interval=300,
        blit=True)
    plt.show()


#info panel code    
root = Tk()
root.geometry('%dx%d+%d+%d' % (1024, 75, 0, 0))

x1 = adc.read_adc(0, gain=1)
x2 = ((x1*4.096)/32767.0)*.00176678
x3 = '%.3f' % x2
var = x3

labelfont = ('times', 20, 'bold')

label1 = Label(root, text="[Timer]")
label1.pack(side=LEFT)
label2 = Label(root, text=var)
label2.pack(side=LEFT)

label1.config(font=labelfont)
label1.config(height=3, width=35)

label2.config(font=labelfont)
label2.config(height=3, width=35)

def countdown(count):
    var2 = "Time", "left:", count
    label1.config(text=var2) 

    if count > 0:
        root.after(1000, countdown, count-1)
    if count == 0:
        var3 = "Test", "ended"
        label1.config(text=var3,bg="red")
        return


def update():
    x1 = adc.read_adc(0, gain=1)
    x2 = ((x1*4.096)/32767.0)*.00176678
    x3 = '%.3f' % x2
    var = "Pressure:", x3, "psi"
    label2.config(text=var)
    root.after(500, update)

#This is where I am unsure of what to do...

root.after(500, update)
root.mainloop() 

我已经测试了此代码的所有组件,并且它们都可以单独工作。我似乎无法将所有内容放在一起。我猜我将需要使用某种多线程处理方法,但是我未能在网上找到任何类似的问题。

0 个答案:

没有答案