2017-03-04 39 views
0

運行下面的python python腳本不顯示嵌入的matplotlib圖。但是它也不會引發錯誤信息。運行該腳本時,應該顯示一個GUI,其左側顯示4個按鈕,右側顯示實時圖形。該圖接收來自文本文件'sample_graph_data.txt'的輸入,該文本與腳本位於同一目錄中。腳本中出現了什麼問題,以及如何使其工作?將matplotlib畫布嵌入到tkinter GUI中 - 繪圖未顯示,但未引發錯誤

#Script begins here 
from tkinter import * 
from tkinter import messagebox 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
from matplotlib import animation 
from matplotlib import style 
from matplotlib.figure import Figure 
PROGRAM_NAME = 'Smart Farm Controller' 
style.use('ggplot') 

fig = Figure(figsize=(5, 30), dpi=100) 
a = fig.add_subplot(111) 

class Controller: 

    def __init__(self, root): 
     self.root = root 
     self.root.title(PROGRAM_NAME) 
     self.root.protocol('WM_DELETE_WINDOW', self.exit_app) 
     self.init_gui() 

    def create_right_graphs(self): 
     right_frame = Frame(self.root) 
     right_frame.grid(row=2, column=6, sticky=N+E+W+S, 
         padx=2, pady=2) 
     anim = animation.FuncAnimation(fig, self.animate_graph(right_frame), 
             interval=1000) 

    def create_left_switches(self): 
     left_frame = Frame(self.root) 
     left_frame.grid(row=2, column=1, columnspan=6, sticky=N+E+W+S, 
         padx=2, pady=2) 
     led_button = Button(left_frame, text='LED') #command=self.on_led_button_clicked) 
     led_button.config(height=2, width=30) 
     led_button.grid(row=2, column=0, padx=4, pady=8) 
     apump_button = Button(left_frame, text='Air Pump') #command=self.on_apump_button_clicked) 
     apump_button.config(height=2, width=30) 
     apump_button.grid(row=3, column=0, padx=4, pady=8) 
     wpump_res_button = Button(left_frame, text='Reservoir Water Pump') 
            #command=self.on_wpump_res_button_clicked) 
     wpump_res_button.config(height=2, width=30) 
     wpump_res_button.grid(row=4, column=0, padx=4, pady=8) 
     wpump_grow_button = Button(left_frame, text='Grow Bucket Water Pump') 
            #command=self.on_wpump_grow_button_clicked) 
     wpump_grow_button.config(height=2, width=30) 
     wpump_grow_button.grid(row=5, column=0, padx=4, pady=8) 

    def animate_graph(self, right_frame): 
     pullData = open("sample_graph_data.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(x)) 

     a.clear() 
     a.plot(xList, yList) 
     canvas = FigureCanvasTkAgg(fig, right_frame) 
     canvas.show() 
     canvas.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=True) 

    def init_gui(self): 
     self.create_right_graphs() 
     self.create_left_switches() 

    def exit_app(self): 
     if messagebox.askokcancel("Quit", "Really quit?"): 
      self.root.destroy() 


if __name__ == '__main__': 
    root = Tk() 
    Controller(root) 
    root.mainloop() 
+0

嘗試創建一個較小的Tkinter應用程序的[MCVE],看看你是否可以運行 –

+0

我看到幾個問題,這些問題應引起錯誤。嘗試從終端運行此操作,以便查看錯誤。如果您仍然需要幫助,請提供我們可以嘗試的完整示例(包括測試數據)。對於多個文件,最好做一個github repo並在此發佈鏈接。 – Novel

回答

0

運行時確實沒有在代碼中從問題中觸發的錯誤,但它也沒有按預期運行。有些錯誤根本不會被觸發,因爲代碼的相應部分從未被調用過。
有幾個問題:

  • 你需要居然FigureCanvas添加到Frame動畫之外。
  • 始終保持對要處理的對象的引用。尤其是動畫必須保持活力。這最好通過使用self使其成爲類屬性來完成。
  • 您需要將幀實際放置到根窗口。這可以使用pack完成。
  • 在FuncAnimation中,您不得調用函數來設置動畫,而只是將其作爲參數提供。另外,您需要提供一定數量的幀以供動畫啓動。 FuncAnimation(fig, self.animate_graph, frames=12)而不是 FuncAnimation(fig, self.animate_graph(someargument))
  • 動畫函數需要參數是framenumber(或參數frames給出的列表中的條目)。如有需要,您可以提供進一步的參數(在這種情況下請參閱文檔)。

我確定我忘了提及一些其他的東西。但這是一個正在運行的代碼。

from tkinter import * 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
from matplotlib import animation 
from matplotlib import style 
from matplotlib.figure import Figure 
style.use('ggplot') 

fig = Figure(figsize=(5, 4), dpi=100) 
a = fig.add_subplot(111) 

class Controller: 

    def __init__(self, root): 
     self.root = root 
     self.create_left_switches() 
     self.create_right_graphs()  

    def create_right_graphs(self): 
     right_frame = Frame(self.root) 
     right_frame.grid(row=2, column=6, sticky=N+E+W+S, 
         padx=2, pady=2) 
     right_frame.pack(fill=X, padx=5, pady=5) 
     self.canvas = FigureCanvasTkAgg(fig,right_frame) 
     self.canvas.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=True) 

     self.anim = animation.FuncAnimation(fig, self.animate_graph, frames=12, 
             interval=500, repeat=True) 
     self.canvas.show() 

    def create_left_switches(self): 
     left_frame = Frame(self.root) 
     left_frame.grid(row=2, column=1, columnspan=6, sticky=N+E+W+S, 
         padx=2, pady=2) 
     left_frame.pack(fill=X, padx=5, pady=5) 
     led_button = Button(left_frame, text='LED') #command=self.on_led_button_clicked) 
     led_button.config(height=2, width=30) 
     led_button.grid(row=2, column=0, padx=4, pady=8) 


    def animate_graph(self, i): 
     pullData = open("sample_graph_data.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)**(1+i/12.)) 

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

if __name__ == '__main__': 
    root = Tk() 
    Controller(root) 
    root.mainloop()