同时运行Tkinter窗口和Matplotlib绘图

2024-06-02 08:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个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() 

我已经测试了这个代码的所有组件,它们都是单独工作的。我似乎无法把这一切都放在一起。我猜我将需要使用一些多线程排序,但我没有在网上找到任何类似的问题


Tags: totextconfigdeflinepltrootglobal