【发布时间】:2016-02-17 21:46:03
【问题描述】:
我正在尝试使用 matplotlib 动画功能更新两个 TKinter“页面”,每个页面都有不同的情节。
问题是只有一页正确显示了情节。
代码如下:
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
import Tkinter as tk
import ttk
a = Figure(figsize=(4,4))
plot_a = a.add_subplot(111)
b = Figure(figsize=(4,4))
plot_b = b.add_subplot(111)
x = [1,2,3,4,5]
y_a = [1,4,9,16,25]
y_b = [25,16,9,4,1]
def updateGraphs(i):
plot_a.clear()
plot_a.plot(x,y_a)
plot_b.clear()
plot_b.plot(x,y_b)
class TransientAnalysis(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
tk.Tk.wm_title(self, "Transient Analysis GUI: v1.0")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in ( GraphPageA, GraphPageB):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(GraphPageA)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class GraphPageA(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Show Graph B",
command = lambda: controller.show_frame(GraphPageB))
button1.grid(row=1, column=0,pady=20,padx=10, sticky='w')
canvasA = FigureCanvasTkAgg(a, self)
canvasA.show()
canvasA.get_tk_widget().grid(row=1, column=1, pady=20,padx=10, sticky='nsew')
class GraphPageB(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = ttk.Button(self, text="Show Graph A",
command = lambda: controller.show_frame(GraphPageA))
button1.grid(row=1, column=0,pady=20,padx=10, sticky='w')
canvasB = FigureCanvasTkAgg(b, self)
canvasB.show()
canvasB.get_tk_widget().grid(row=1, column=1, pady=20,padx=10, sticky='nsew')
app = TransientAnalysis()
app.geometry("800x600")
aniA = animation.FuncAnimation(a, updateGraphs, interval=1000)
aniB = animation.FuncAnimation(b, updateGraphs, interval=1000)
app.mainloop()
更具体地说,正确显示绘图的页面是 for 循环中调用的最后一个页面 for F in ( GraphPageA, GraphPageB):
我也尝试过使用不同的 updateGraphs 函数,每个图一个,但结果是一样的。
如何让两个 TKinter 页面绘制这两个不同的图表? 我正在使用 Python 2.7。
【问题讨论】:
-
我忘了说。 Python 2.7。
-
你可以编辑你自己的问题来更新这样的细节。
-
嗯,这令人抓狂....动画回调和您用于面板的 Tk 细节之间的交互似乎很差,因为问题是其中一个动画永远不会被调用。
-
啊,我整理好了,马上回答。
标签: python matplotlib tkinter tkinter-canvas