我是python编程的新手,很抱歉,如果这是一个愚蠢的问题 .

我正在使用python,matplotlib和tkinter在tkinter gui中绘制数据的实时图表 . 因此我按照这个链接的教程:https://pythonprogramming.net/plotting-live-bitcoin-price-data-tkinter-matplotlib/

这里,使用动画功能来绘制数据 . 我想通过按下gui上的按钮来启动和停止这个animationFunc,但不知道如何实现它 .

我将不胜感激任何帮助!谢谢

这是代码:

import Tkinter as tk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style 
import serial
import sys

ser = serial.Serial(port='COM3', baudrate = 19200, bytesize=8, parity='N', 
stopbits=1, timeout=None,rtscts=0)

LARGE_FONT = ("Verdana", 12)
style.use('ggplot')
f = Figure(figsize=(5,5), dpi = 100)
a = f.add_subplot(111)
yList = []

def animate(i):
    # I receive data as a string with a hex number, so here I read it, take 
    #the number and append it to the yList
    r = ser.readline()
    he = r.split(" ")[2]
    g = int(he,16)
    d = str(g)
    yList.append(g)
    a.clear()
    a.plot(yList)

class Sensors(tk.Tk):

    def __init__(self,*args, **kwargs ):
        tk.Tk.__init__(self,*args,**kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for F in (ChooseSensor, Sensor3):   
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(ChooseSensor)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    #here I want to define the function to stop the animate function and to 
    #stop the serial port
    def stopPlot(event,cont):
        #command to serial port to stop sensor        
        ser.write(b'S1,P,0\r\n')

    #here i want to create the function to start the animate function
    def animate_start(self,cont):   
        #command to configure and start sensor
        ser.write(b'S1,S,2\r\n')
        ser.write(b'S1,T,1\r\n')
        ser.write(b'S1,P,1\r\n')

class ChooseSensor(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "Choose Sensor", font= LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self,text="Graph Page",
                        command=lambda:controller.show_frame(Sensor3))
        button1.pack()

class Sensor3(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text = "Graph Page", font= LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self,text="Start Plot",
                        command=lambda:controller.animate_start(self))
        button1.pack()

        button2 = tk.Button(self,text="Stop Plot",
                        command=lambda:controller.stopPlot(self))
        button2.pack()

        button3 = tk.Button(self,text="Back to Home",
                        command=lambda:controller.show_frame(ChooseSensor))
        button3.pack()

        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side=tk.TOP, fill = tk.BOTH, expand = True)

app = Sensors()
ani = animation.FuncAnimation(f,animate, interval=1000)
app.mainloop()

编辑:我试图做这样的事情:

def animate_start(self,cont):
    global anim_start
    anim_start=True
    #Konfiguriert und schreibt Sensor1
    ser.write(b'S1,S,2\r\n')
    ser.write(b'S1,T,1\r\n')
    ser.write(b'S1,P,1\r\n')
    return anim_start

def stopPlot(event,cont):
    anim_start=False
    ser.write(b'S1,P,0\r\n')
    return anim_start

然后只在anim_start == True时执行动画功能:

app = Sensors()
if anim_start == True:
    ani = animation.FuncAnimation(f,animate, interval=1000)
app.mainloop()

现在我可以看到,发送命令并接收数据(USB串口桥上的LED指示灯闪烁),但是没有数据添加到yList,因此没有绘制图表