Problem with Matplotlib and the Tk-Backend

James Stroud jstroud at mbi.ucla.edu
Fri Apr 27 06:24:52 CEST 2007


thorstenkranz wrote:
> Hi everyone,
> 
> I'm new here and I have a question ( I guess as everybody who is new
> here ;-) ),
> 
> I'm having some strange problem with Matplotlib, using it in a Tkinter
> application.
> 
> I create a  Canvas, a figure, subplot and then a toolbar. It works
> fine, but only without the toolbar! When I want to add the toolbar, I
> get an error
> 
>  File "./tkViewer.py", line 102, in setupGUI
>     self.toolbar = NavigationToolbar2TkAgg( self.canvas, master )
>   File "/usr/lib/python2.4/site-packages/matplotlib/backends/
> backend_tkagg.py", line 537, in __init__
>     NavigationToolbar2.__init__(self, canvas)
>   File "/usr/lib/python2.4/site-packages/matplotlib/backend_bases.py",
> line 1107, in __init__
>     self._init_toolbar()
>   File "/usr/lib/python2.4/site-packages/matplotlib/backends/
> backend_tkagg.py", line 577, in _init_toolbar
>     borderwidth=2)
>   File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 2378, in __init__
>     Widget.__init__(self, master, 'frame', cnf, {}, extra)
>   File "/usr/lib/python2.4/lib-tk/Tkinter.py", line 1865, in __init__
>     self.tk.call(
> _tkinter.TclError: bad screen distance "500.0"
> 
> Here is the part my code:
> 
>         self.canvFrame = Frame(master)
>         self.canvFrame.pack(side=TOP, fill=BOTH, expand=1)
>         self.canvFrame2 = Frame(self.canvFrame)
>         self.canvFrame2.pack(side=LEFT, fill=BOTH, expand=1)
>         self.f = Figure(figsize=(5,4), dpi=100)
>         self.a = self.f.add_subplot(111)
>         self.subplAxis = self.f.get_axes()
>         self.canvas = FigureCanvasTkAgg(self.f,
> master=self.canvFrame2)
>         self.canvas.show()
>         self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH,
> expand=1)
>         self.toolbar = NavigationToolbar2TkAgg( self.canvas,
> self.canvFrame2 )
>         self.toolbar.update()
>         self.canvas._tkcanvas.pack(side=TOP, fill=X, expand=1)
> 
> 
> master is a parameter passed to my method, which actually is set to
> Tk()
> 
> What did I get wrong? What is the problem? Thanks in advance...
> 
> Thorsten
>

Just prior to "self.toolbar = ...", try :

         master.winfo_toplevel().update_idletasks()

James



More information about the Python-list mailing list