Reputation: 21
I'm trying to display a pie chart using matplotlib embedded in tkinter. Here's the class declaration:
class Graph:
def __init__(self, data, directory, scan_date, frame):
self.data = data
self.directory = directory
self.scan_date = scan_date
self.frame = frame
def pie_chart(self):
# try:
# self.canvas.get_tk_widget().pack_forget()
# except AttributeError:
# pass
piechart = graphs.make_pie_chart(self.data, self.directory, self.scan_date)
self.canvas = FigureCanvasTkAgg(piechart, master=self.frame)
self.canvas.draw()
self.canvas.get_tk_widget().pack()
And here's where it's instantiated:
def scan_and_display(directory):
# runs the main scan function
data, scanDate = main(directory)
pie_frame = Frame(root, height=700, width=700)
graph1 = Graph(data, directory, scanDate, pie_frame)
graph1.pie_chart()
In the line self.canvas = FigureCanvasTkAgg(piechart, master=self.frame)
, if I change master
to root
, it works. But I would like to embed it in its on frame so I can more easily add more elements as I continue building the GUI.
Upvotes: 0
Views: 96
Reputation: 13729
Looks like you forgot to layout the frame. Add this to the end:
pie_frame.pack()
Upvotes: 0