Trouble embedding toolbar in Tk using grid

I've been trying to modify the embedding_in_tk.py example to use the
grid manager instead of pack. I can get the plot to show ok but I
can't seem to get the toolbar to show correctly. The following code
does get the toolbar on there but it does use pack for the frame and
also I always end up with an extra blank window (this code is
simplified from something that a Bonnie Douglas posted on this list
recently).

Any suggestions?

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

import Tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from numpy import arange, sin, pi

# create toplevel window
tl=Tk.Toplevel()
tl.title("storage")

# create frame
frame=Tk.Frame(master=tl)

fig=Figure(figsize=(12,6), dpi=100)

# create plots
a1 = fig.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a1.plot(t,s)

# create canvas
canvas=FigureCanvasTkAgg(figure=fig, master=frame)
canvas.show()

c=canvas.get_tk_widget()
c.grid(row=0, column=0)

# problems with toolbar not showing solved by setting the master to
# the toplevel window, not the frame!!!
toolbar=NavigationToolbar2TkAgg(canvas, tl)
toolbar.update()

frame.pack()

Tk.mainloop()

Well I realized my error with the extra window being caused by the
TopLevel() command. I switched this to Tk.Tk() and it works nicely.
However I still have to pack the frame instead of using grid. I can
work around this but I wonder if there isn't something else I'm
missing.

ยทยทยท

On Tue, Mar 23, 2010 at 6:00 PM, Jonno <jonnojohnson@...287...> wrote:

I've been trying to modify the embedding_in_tk.py example to use the
grid manager instead of pack. I can get the plot to show ok but I
can't seem to get the toolbar to show correctly. The following code
does get the toolbar on there but it does use pack for the frame and
also I always end up with an extra blank window (this code is
simplified from something that a Bonnie Douglas posted on this list
recently).

Any suggestions?

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

import Tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from numpy import arange, sin, pi

# create toplevel window
tl=Tk.Toplevel()
tl.title("storage")

# create frame
frame=Tk.Frame(master=tl)

fig=Figure(figsize=(12,6), dpi=100)

# create plots
a1 = fig.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a1.plot(t,s)

# create canvas
canvas=FigureCanvasTkAgg(figure=fig, master=frame)
canvas.show()

c=canvas.get_tk_widget()
c.grid(row=0, column=0)

# problems with toolbar not showing solved by setting the master to
# the toplevel window, not the frame!!!
toolbar=NavigationToolbar2TkAgg(canvas, tl)
toolbar.update()

frame.pack()

Tk.mainloop()

2010/3/24 Jonno <jonnojohnson@...287...>:

Well I realized my error with the extra window being caused by the
TopLevel() command. I switched this to Tk.Tk() and it works nicely.
However I still have to pack the frame instead of using grid. I can
work around this but I wonder if there isn't something else I'm
missing.

If I'm not completely mistaken you can .grid() your Frame as well, but
you might have to .columnconfigure() and/or .rowconfigure() the Tk
instance with argument weight = 1 or something integer positive, to
make the row containing the plot not collapse to zero?

The main problem is, that the NavigationToolbar2TkAgg actually in
matplotlib.backends.backend_tkagg.py:681 calls self.pack(side =
Tkinter.BOTTOM, fill = Tk.X), where the toolbar is actually derived
from Tkinter.Frame. This means, subclass and overload, or:

toolbar_frame = Tkinter.Frame(tl)
toolbar_frame.grid(column = 0, row = 1)

toolbar=NavigationToolbar2TkAgg(canvas, toolbar_frame)

Now I see why your script hangs, because when you try to pack() and
grid() widgets to the same master, Tk somehow hangs up without error
message, weird if you ask me. But this I encounter often.

hth,
Friedrich