Mailing List Archive

Canvases not resizable?
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.

The example both creates a Frame with a canvas. Resizing the frame
doesn't change the size of the canvas.

Stuart


---------

from Tkinter import *



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

#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)


def printCanvasSize(self, point):
print 'w = ', self.canvas["width"]
print 'h = ', self.canvas["height"]


if __name__ == '__main__':

cframe = CFrame(w="8i", h="8i")
Pack.config(cframe)
cframe.mainloop()
Canvases not resizable? [ In reply to ]
Hi Stuart,

Stuart Reynolds 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.
No, canvases are resizeable.

from Tkinter import *
mFrame = Frame(master=None)
mFrame.pack(fill=BOTH, expand=1)
canvas = Canvas(master=mFrame, bg="red")
canvas.pack(fill=BOTH, expand=1)
mFrame.mainloop()

The tricky part is where and when to use the correct options for the
packer. In your example, the frame was not resizeable, thus the canvas
was not resizeable.

Peter

--
---------------------------------------------------------------------------
Dr. Peter Stoehr --- Teisenbergweg 6 --- 85435 Erding --- 08122/47232
---------------------------------------------------------------------------
I'm the terror that flaps through the night
Canvases not resizable? [ In reply to ]
Stuart Reynolds <S.I.Reynolds@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

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

> cframe.mainloop()

hope this helps!

</F>