2017-03-04 11 views
0

以下のpython pythonスクリプトを実行すると、埋め込みmatplotlibプロットは表示されません。ただし、エラーメッセージも表示されません。スクリプトを実行すると、左側に4つのボタンを表示し、右側にリアルタイムグラフを表示するGUIが表示されます。グラフは、スクリプトと同じディレクトリにあるテキストファイル'sample_graph_data.txt'からその入力を受け取ります。スクリプトの何が間違っていて、それをどうすればいいのですか?matplotlib canvasを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

[MCVE]小さいTkinterのアプリのを作成し、 –

+0

が、私はエラーを発生させる必要があるいくつかの問題を見ることを実行できるかどうかを確認してください。これを端末から実行して、エラーを見てください。依然として助けが必要な場合は、試してみることができる完全な例(テストデータを含む)を提供してください。複数のファイルの場合、githubリポジトリを作成し、ここにリンクを投稿するのが最善の方法です。 – Novel

答えて

0

は実際に実行されたときに、質問からのコードでトリガは、エラーはありませんが、予想通り、それはまた、実行されていません。一部のエラーは、コードのそれぞれの部分が呼び出されないため、トリガされません。あなたが実際にアニメーション外FrameFigureCanvasを追加する必要が


  • には、いくつかの問題があります。
  • 作業したいオブジェクトへの参照を常に保持してください。特にアニメーションは生き続ける必要があります。これは、selfを使用してクラス属性にすることをお勧めします。
  • 実際にフレームをルートウィンドウに配置する必要があります。これはpackを使用して行うことができます。
  • FuncAnimationの内部では、関数をアニメーション化するのではなく、単純に引数として渡す必要があります。また、アニメーションを開始するには、いくつかのフレームをアニメートする必要があります。 FuncAnimation(fig, self.animate_graph, frames=12)代わりの FuncAnimation(fig, self.animate_graph(someargument))
  • アニメーション関数は、フレーム番号(又は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() 
関連する問題