首页 > 解决方案 > 在 tkinter 中嵌入 matplotlib 图的问题

问题描述

我正在尝试将动画图嵌入到我的 GUI 中,但是,每当我尝试根据有关使用画布的教程所看到的内容时,我都会得到一个空白的 tkinter 屏幕。没有错误,什么都没有。

但是,当我使用 plt.show 时,它可以正常工作,但并不局限于 GUI。这是问题所在。

它可能是一个快速修复,但这就是我所拥有的。

任何帮助,将不胜感激!

class popupWindowOscil(tk.Frame):

  def __init__(self,master,ser):

    OscilTop= self.OscilTop= Toplevel(master)
    tk.Frame.__init__(self)
    self.ser = ser   
    self.fig = plt.figure()
    self.ax = self.fig.add_subplot(1, 1, 1)

    self.xs = []
    self.ys = []
    self.xval =0
    self.OscilLoop()


  def OscilLoop(self):

    ani = animation.FuncAnimation(self.fig, self.Oscilliscope, fargs=(self.xs, self.ys))
    #self.canvas = FigureCanvasTkAgg(self.fig, self)
    #self.canvas.draw()
    #self.canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
    plt.show()
    return

# The function that creates the values for the plot.
  def Oscilliscope(self,i,xs,ys):

    tryagain =1
    while tryagain == 1:
        try:
            reading = self.ser.readline().decode()
            tryagain = 0
        except UnicodeDecodeError:
            pass
    Incominglist = str(reading).split(",")
    try:
        numbers = [float(x) for x in Incominglist]
    except ValueError:
        print ('Failure during string decode, Restart and Try again')
        return
    # Add x and y to lists
    self.ys.extend(numbers)

    for val in range(len(Incominglist)):
        if  self.xval == 0 and val ==0:
            self.xs.append(self.xval)  # or any arbitrary update to your figure's data
        else:
            self.xval += 0.005
            self.xs.append(self.xval)

    # Draw x and y lists
    self.ax.clear()
    self.ax.plot(self.xs, self.ys)

    # Format plot
    self.ax.yaxis.set_ticks(np.arange(0,5,0.25))
    plt.subplots_adjust(bottom=0.30)

标签: pythonuser-interfacematplotlibtkinter

解决方案


推荐阅读