Tkinter - Resizing a canvas with a window

G

Gordon Airporte

I'm trying to get my canvas to resize to fill its frame within a window,
but I can't figure out how to handle the callback data from the window's
<Configure> properly. It has very strange behavior - resizing randomly
or growing by itself, shrinking to 0. The following works passably but
jumps around at random if you move the window and goes nuts if you add
the button (watch any wrapping):


from Tkinter import *

class testApp2:
def __init__( self, master ):

self.ma = master
self.f = Frame( self.ma )
self.f.pack()
self.cv = Canvas(self.f, width=25, height=25, bg='red')
self.cv.pack()
#self.b1 = Button( self.f, text='hello', command=None )
#self.b1.pack(side='bottom')

self.ma.bind('<Configure>', self.resize )


def resize( self, event ):
#print '(%d, %d)' % (event.width, event.height)
self.cv.configure( width = event.width-4, height = event.height-4 )


root = Tk()
app = testApp2(root)
root.mainloop()
 
J

Jeff Epler

You should just use 'pack' properly. Namely, the fill= and expand=
parameters. In this case, you want to pack(fill=BOTH, expand=YES).
For the button, you may want to use pack(anchor=E) or anchor=W to make
it stick to one side of the window.

The additional parameters for the button (both creation and packing)
give a geometry that is closer to the standard buttons in Windows 95
/ Windows 2000. Use those or not, as you see fit.

Here's the new program:

from Tkinter import *

class testApp2:
def __init__( self, master ):
self.ma = master
self.f = Frame( self.ma )
self.f.pack(fill=BOTH, expand=YES)
self.cv = Canvas(self.f, width=25, height=25, bg='red')
self.cv.pack(fill=BOTH, expand=YES)
self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1)
self.b1.pack(side=BOTTOM, anchor=E, padx=4, pady=4)



root = Tk()
app = testApp2(root)
root.mainloop()

Jeff
PS thanks for including a full, runnable program in your post!

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFC5uHTJd01MZaTXX0RAoMyAJ0aJ5eGeaXM18BC7CY7/1RQt46fqQCgg02s
vrD3TQnEeYV7V00+gwig9E8=
=dt77
-----END PGP SIGNATURE-----
 
G

Gordon Airporte

Thanks you very much. I found something interesting though, the canvas's
width and height properties are not updated when it is resized by its
packing. Looks like an oversight to me, but I've just demonstrated that
I don't have a complete grasp of Tk, so... I can use a Configure
callback to keep track of the values, however.


from Tkinter import *

class testApp3:
def __init__( self, master ):
self.ma = master
self.f = Frame( self.ma )
self.f.pack(fill=BOTH, expand=YES)
self.cv = Canvas(self.f, width=125, height=125, bg='red')
self.cv.pack(fill=BOTH, expand=YES)
self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1, \
command = self.howbig )
self.b1.pack(side=BOTTOM, anchor=S, padx=4, pady=4)
self.cv.bind('<Configure>', self.resize )

def howbig( self ):
print self.cv['width'], self.cv['height']
print self.cvw, self.cvh

def resize( self, event ):
print '(%d, %d)' % (event.width, event.height)
self.cvw, self.cvh = event.width-4, event.height-4

root = Tk()
app = testApp3(root)
root.mainloop()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top