【发布时间】:2018-11-12 11:22:27
【问题描述】:
我正在使用tkinter 制作一个 gui,它接收数据点“美元”并将它们绘制到 matplotlib 图表上。我知道将matplotlib 图嵌入tkinter 需要使用FigureCanvasTkAgg 和NavigationToolbar2TkAgg。当我没有在父类和子类之间使用继承时,此方法有效。
当我尝试在我的程序中实现多个页面时,如下面的链接:
Using buttons in Tkinter to navigate to different pages of the application?
我的函数在Page1 类下,它是Page 类的子类。每当我按下按钮时,我就开始得到一个无响应的工具栏。用TypeError "Label object is not callable。
我知道错误来自声明 NavigationToolbar2TkAgg 对象。请帮忙,我怀疑我可能有继承问题,因为我为工具栏和 Figure 画布创建了一个单独的框架。
def plot_data(self,dollars):
"""
This function takes in a list for dollars calculates
the years and converts the lists into numpy arrays and plots
them onto a matplotlib figure embedded into tkinter.
"""
#create a figure object to hold the matplotlib plots
self.figure = Figure(figsize=(5,4), dpi = 100)
#create a subplot first row first column
self.a = self.figure.add_subplot(111)
#update the list of dollars for the plot
self.dollars = np.array(dollars)
self.years = np.arange(0,len(dollars),1)
#plots the numpy arrays into a matplotlib figure 'pyplot'
self.a.plot(self.years,self.dollars,linestyle='none',marker='o')
#holds matplotlib figure in a container to be displayed in tkinter window 'window'
self.canvas = FigureCanvasTkAgg(self.figure, master=self)
#show matplotlib figure on tkinter window
self.canvas.show()
#displays the matplotlib figure on the grid
self.canvas.get_tk_widget().grid(row=10,column=1,columnspan=2,rowspan=20)
#create the toolbar synced with canvas
self.toolbarFrame = tk.Frame(self)
self.toolbarFrame.grid(row=31,column=1)
#creates a navigation toolbar linked to the matplotlib figure with
#the master being the toolbar tk.Frame
self.toolbar = NavigationToolbar2TkAgg(self.canvas,self.toolbarFrame)
#update the plot
self.toolbar.update()
【问题讨论】:
标签: python matplotlib tkinter