私はTkinterを使ってGUI内にアニメーショングラフを表示します(他のウィジェットと一緒に)。これはうまくいきますが、2番目のmatplotlibウィンドウもTkinterメインウィンドウとともに開きます。これを防ぐ方法を教えてください。Tkinterがextra matplotlib figureを開く
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import Tkinter as tk
import ttk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
#self.a = Arduino() #the data is coming in from an arduino
self.createWidgets()
def createWidgets(self):
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0,0,1,1])
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().place(x=0, y=0)
self.plotbutton=tk.Button(master = root, text="plot", command=lambda: self.plot(canvas,ax))
self.plotbutton.place(x=500, y=0)
self.quitButton = tk.Button(master=root, text="quit", command=self.quit)
self.quitButton.place(x=600, y=0)
def plot(self,canvas,ax):
while(1):
print "plotting"
plt.pause(0.1)
ax.clear() # clear axes from previous plot
#values = self.getData(arduino) #in the full program, I get new theta and r data from an arduino
theta = [0, 1, 2, 3, 4, 5] #arbitrary axis values for testing purposes
r = [0, 1, 2, 3, 4, 5]
ax.plot(r, theta)
plt.xlim([0, 6]) #arbitrary axes limits
plt.ylim([0, 6])
canvas.draw()
def quit(self):
self.master.destroy()
root=tk.Tk()
root.geometry("950x500+300+300")
app=Application(master = root)
app.mainloop()
ありがとうございます!
編集:作業例にプログラムを作成
なし[MCVE]私は推測することができます:あなたはどこかに自分のコードで 'plt.showを()'を使用するので、私は推測します新しいウィンドウが開きます。プロットがtkinterに埋め込まれている場合は、その必要はありません。また、matplotlibのアニメーション関数を使用すると、グラフがよりスムーズになります。 – Novel
@Novel私はコードにplt.show()を持っていません:( あなたが示唆したように私はこの例を修正しました。おそらくそれはもっと役に立つでしょう – janizer