By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

Tkinter - Resizing a canvas with a window

P: n/a
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()

Jul 27 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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-----

Jul 27 '05 #2

P: n/a
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()
Jul 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.