Canvases not resizable?

Fredrik Lundh fredrik at pythonware.com
Fri Aug 20 03:03:58 EDT 1999


Stuart Reynolds <S.I.Reynolds at cs.bham.ac.uk> wrote:
> Are Tkinter canvases fixed to one size? I don't seem to be able to get
> them to expand to fill their container when its resized even the though
> the canvas pack options are set to fill=BOTH  and expand=1.

> from Tkinter import *
> 
> class CFrame(Frame):
>     """A Frame containing a Canvas
>     """
>     def __init__(self, w, h):
> Frame.__init__(self, master=None)

um.  master is not really a keyword argument,
but never mind...

> #Set the frame's width and height
> self["width"] = w
> self["height"] = h
> 
> #Create a red canvas
> self.canvas = Canvas(master=self, bg="red")
> 
> #Print canvas dimensions when clicked
> self.canvas.bind("<Button-1>", self.printCanvasSize)
> 
> #These should make the canvas fill the frame right??
> self.canvas.pack(fill=BOTH, expand=1)

nope. it makes the canvas tell it's parent to resize,
which will cause the entire application to change
size.

you're better off if you use the width and height
options on the canvas instead.
    
>     def printCanvasSize(self, point):
> print 'w = ', self.canvas["width"]
> print 'h = ', self.canvas["height"]

the width and height options are not live: they
are hints to the geometry manager, not dynamically
updated variables.

use self.winfo_reqwidth() and self.winfo_reqheight()
instead, to get the requested size.  winfo_width and
winfo_heigth returns the current size; it's updated by
a background task, so it may not be what you expect.

> if __name__ == '__main__':
>     
>     cframe = CFrame(w="8i", h="8i")
>     Pack.config(cframe)

this is a very old way to say:

    cframe.pack()

which, of course, uses default values for all
packer options.  see:

http://www.pythonware.com/library/tkinter/introduction/pack.htm
=> options

adding the usual fill=BOTH, expand=1 stuff should
help here.

>     cframe.mainloop()

hope this helps!

</F>





More information about the Python-list mailing list