我有一个tkinter程序,当我最初运行它时显示matplotlib图。但是,我遇到了麻烦,在我的另一个弹出窗口中嵌入了第二个matplotlib图。
理想情况下,我希望能够单击一个按钮并显示一个带有matplotlib的窗口。
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
import Tkinter as Tk
class Window():
def __init__(self, master):
self.master = master
self.frame = Tk.Frame(master)
self.button1 = Tk.Button(self.master, text = 'Open Graph Window', width = 25,command= self.new_window)
self.button1.pack()
self.f = Figure( figsize=(7, 6), dpi=80 )
self.ax0 = self.f.add_axes( (0.05, .05, .50, .50), axisbg=(.75,.75,.10), frameon=True)
self.ax0.plot(np.max(np.random.rand(100,10)*10,axis=1),"r-")
self.frame = Tk.Frame(root)
self.frame.pack(side=Tk.LEFT, fill=Tk.BOTH, expand=1)
self.canvas = FigureCanvasTkAgg(self.f, master=self.frame)
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.canvas.show()
def new_window(self):
self.newWindow = Tk.Toplevel(self.master)
self.app = Graph(self.newWindow)
class Graph():
def __init__(self, master):
self.master = master
self.frame = Tk.Frame(master)
self.f1 = Figure( figsize=(10, 9), dpi=80 )
self.ax10 = self.f1.add_axes( (0.05, .05, .50, .50), axisbg=(.75,.75,.10), frameon=True)
self.ax10.plot(np.max(np.random.rand(100,10)*10,axis=1),"r-")
self.quitButton = Tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
self.canvas = FigureCanvasTkAgg(self.f1, master=self.master)
self.canvas.show()
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
if __name__ == '__main__':
root = Tk.Tk()
app = Window(root)
root.title( "MatplotLib with Tkinter" )
root.update()
root.deiconify()
root.mainloop()
当我打开另一个窗口时,我有我认为应该使用的代码来生成另一个matplotlib图。然而,这不起作用。 点击“打开窗口”按钮后,我该怎么做才能显示第二张图?
非常感谢任何帮助
答案 0 :(得分:3)
您忘记在Graph
课程中打包画布。添加行
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
__init__
Graph
方法中的,图表应显示在顶层。