0

I am making a gui using tkinter that takes in data points "dollars" and plots them onto a matplotlib chart. I understand to embed a matplotlib plot onto tkinter requires using FigureCanvasTkAgg and NavigationToolbar2TkAgg. This method works when I didn't use inheritance between parent class and child class.

When I tried to implement multiple pages into my program, like the link below:

Using buttons in Tkinter to navigate to different pages of the application?

My function is under the Page1 class, which is a subclass of the Page class. I started getting an unresponsive toolbar whenever I pressed buttons. With the TypeError "Label object is not callable.

I know the error is coming from declaring the NavigationToolbar2TkAgg object. Please help, I suspect I might have an inheritance issue since I created a separate frame for toolbar and Figure canvas.

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
Alanlyyy
  • 67
  • 2
  • 6

1 Answers1

0

I have fixed my issue. The code above does not contain any issues. I actually declared one of my instance variables in my class incorrectly.

Here is the error I made:

TypeError: Label object is not callable Ex) tk.Label = tk.Label(.....)

Feel free to use the function above to embed a figure and navigation bar to your tkinter GUI as it works.

Alanlyyy
  • 67
  • 2
  • 6