Why does Tkinter do this?

Mark Wright mwright at pro-ns.net
Fri Sep 20 14:19:15 EDT 2002


I'm trying to create a resizeable Label with an image.  bind()ing to
the configure event allows me to resize the image whenever the label
is resized, but if I set the size of the image to the new size of the
label, it is too big (which produces cascading 'configure' events and
a steadily expanding Label).  OK, so I try subtracting the borderwidth
- which is no good either.  But if I subtract the borderwidth *times
two*, it works.  Can anyone explain this to me?

Here's my code:

from Tkinter import *
from PIL import Image, ImageTk


class PhotoLabel(Label):

	def __init__(self, parent, fileName):
		self.startImage = Image.open(fileName)
		self.currentImage = self.startImage
		self.photo = ImageTk.PhotoImage(self.startImage)
		Label.__init__(self, image=self.photo, bd=5)
		self.bind('<Configure>', self.OnConfigure)
	
	def OnConfigure(self, event):
		if event.type == '22':
			# why * 2?
			borderwidth = 2*int(self['borderwidth'])
			newHeight = max(0, event.height - borderwidth)
			newWidth = max(0, event.width - borderwidth)
			self.currentImage = self.startImage.resize((newWidth, newHeight))
			self.photo = ImageTk.PhotoImage(self.currentImage)
			self.configure(image=self.photo)



More information about the Python-list mailing list