2017-03-15 55 views
0

我是python新手,我想在單擊按鈕後在新窗口中顯示圖形,但我不知道在跟隨文檔時發生的錯誤是什麼。我不斷收到如下的錯誤。Python。錯誤使用animation.FuncAnimation

Traceback (most recent call last): 
    File "C:\Users\User\Desktop\IMPORTANT NOTES\python to firebase\GUI\increment.py", line 88, in <module> 
    ani = animation.FuncAnimation(f, animate, interval=1000) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\animation.py", line 1462, in __init__ 
    TimedAnimation.__init__(self, fig, **kwargs) 
    File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\animation.py", line 1225, in __init__ 
    event_source = fig.canvas.new_timer() 
AttributeError: 'NoneType' object has no attribute 'new_timer' 

這是我的代碼。

from tkinter import * 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import matplotlib.animation as animation 
import matplotlib.pyplot as plt 
from matplotlib import style 

style.use("ggplot") 

f = Figure(figsize = (5,5), dpi = 100) 
a = f.add_subplot(111) 
#a.plot([1,2,3,4,5,6,7,8],[5,4,8,6,3,2,7,9]) 


def animate(i): 
    pullData = open ("sampleData.txt","r").read() 
    datalist = pullData.split('\n') 
    xList = [] 
    yList = [] 
    for eachLine in dataList: 
     if len (eachLine)>1: 
      x,y=eachLine.split(',') 
      xList.append(int (x)) 
      yList.append(int (y)) 

    a.clear() 
    a.plot(xList,yList) 



class Application(Frame): 
    """A GUI app with some buttons.""" 

    def __init__(self, master): 
     """ Initialze frame""" 
     Frame.__init__(self, master) 
     self.grid() 
     self.button_clicks=0 #count the number of button click 
     self.create_widgets() 

    def create_widgets(self): 
     """Create button which displays number of clicks.""" 

     #Button1 
     self.button1 = Button(self) 
     self.button1 ["text"]="in" 
     self.button1["command"] = self.update_news 
     self.button1.grid() 



    def update_news(self): 
     toplevel = Toplevel() 



     canvas = FigureCanvasTkAgg(f,toplevel) 
     canvas.show() 
     canvas.get_tk_widget().pack() 

     toolbar = NavigationToolbar2TkAgg(canvas,toplevel) 
     toolbar.update() 
     canvas._tkcanvas.pack() 


#Building the window 
root = Tk() 
root.title("Buttons") 
root.geometry("200x300") 

app = Application(root) 

#MainLoop 

ani = animation.FuncAnimation(f, animate, interval=1000) 
root.mainloop() 
+1

嘗試使用'F = plt.figure(figsize =(5,5),DPI爲100)' – davedwards

+0

感謝,沒有錯誤了,但數據不displayed.I認爲動畫功能沒有打過電話。 –

回答

0

您需要確保只在圖形添加到畫布上時纔開始動畫。保持對FuncAnimation實例的引用也至關重要。

這樣做的一種直觀方式是在update_news方法中創建圖形,並且只在圖形綁定到畫布後才創建動畫。使用類變量(self)作爲圖形,軸和畫布確保不會鬆脫參考。

from tkinter import * 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import matplotlib.animation as animation 
import matplotlib.pyplot as plt 
from matplotlib import style 
import numpy as np 

style.use("ggplot") 


class Application(Frame): 
    """A GUI app with some buttons.""" 

    def __init__(self, master): 
     """ Initialze frame""" 
     Frame.__init__(self, master) 
     self.grid() 
     self.button_clicks=0 #count the number of button click 
     self.create_widgets() 

    def create_widgets(self): 
     """Create button which displays number of clicks.""" 

     #Button1 
     self.button1 = Button(self) 
     self.button1 ["text"]="in" 
     self.button1["command"] = self.update_news 
     self.button1.grid() 

    def update_news(self): 
     toplevel = Toplevel() 

     self.f = Figure(figsize = (5,5), dpi = 100) 
     self.a = self.f.add_subplot(111) 


     self.canvas = FigureCanvasTkAgg(self.f,toplevel) 
     self.canvas.show() 
     self.canvas.get_tk_widget().pack() 

     toolbar = NavigationToolbar2TkAgg(self.canvas,toplevel) 
     toolbar.update() 
     self.canvas._tkcanvas.pack() 

     self.ani = animation.FuncAnimation(app.f, app.animate, frames=100,interval=100) 

    def animate(self,i): 
     """ 
     pullData = open ("sampleData.txt","r").read() 
     datalist = pullData.split('\n') 
     xList = [] 
     yList = [] 
     for eachLine in dataList: 
      if len (eachLine)>1: 
       x,y=eachLine.split(',') 
       xList.append(int (x)) 
       yList.append(int (y)) 
     """ 
     xList = np.arange(i+1) 
     yList = np.sin(xList/10.) 
     self.a.clear() 
     self.a.plot(xList,yList) 


#Building the window 
root = Tk() 
root.title("Buttons") 
root.geometry("200x300") 

app = Application(root) 

#MainLoop 
root.mainloop()