Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Matplotlib Navigation Toolbar in Tkinter via grid

Tags:

I'm developing a small Tkinter GUI to draw matplotlib-plots. (It contains a few Entries and assembles the plot according to their content.)

I have designed my plotting widget according to http://matplotlib.org/examples/user_interfaces/embedding_in_tk.html, only I use grid instead of pack:

canvas = FigureCanvasTkAgg(fig, master=root) canvas.get_tk_widget().grid(row=1,column=4,columnspan=3,rowspan=20) 

That part works. But embedding the NavigationToolbar in the same fashion does not. Tkinter breaks down without error when I include the lines:

toolbar = NavigationToolbar2TkAgg( canvas, root ) canvas._tkcanvas.grid(row=22,column=4) 

I know this is because NavigationToolbar calls pack internally, and pack and grid don't get along. However, I like grid and would hate to have to redesign my whole GUI just to be able to use the NavigationToolbar.

Is there a workaround so I can use NavigationToolbar2TkAgg via grid? (I have found the advice to "subclass and overload" here, but don't know how to do that.)

Any help greatly appreciated!

like image 410
CodingCat Avatar asked Oct 16 '12 11:10

CodingCat


People also ask

How to use grid function in Tkinter Python?

To create a grid, you need to use the .The grid() method allows you to indicate the row and column positioning in its parameter list. Both row and column start from index 0. For example grid(row=1, column=2) specifies a position on the third column and second row of your frame or window.

Does tkinter work with Matplotlib?

Matplotlib charts by default have a toolbar at the bottom. When working with Tkinter, however, this toolbar needs to be embedded in the canvas separately using the NavigationToolbar2Tk() class.


1 Answers

Can you create an empty frame, then put the NavigationToolbar in that frame? I assume the NavigationToolbar will then pack itself in that frame. You can then use grid on the frame.

like image 68
Bryan Oakley Avatar answered Oct 08 '22 12:10

Bryan Oakley