Tkinter Resize

Matthew Dixon Cowles matt at mondoinfo.com
Thu Feb 14 17:10:35 EST 2002


On 14 Feb 2002 13:29:52 -0800, Jeffrey <jmsun at bioeng.ucsd.edu> wrote:

Dear Jeff,

>Hi Everyone,

Hi!

>I'm working with a Tkinter Text Widget.  How do you bind a function
>to the Text Widget whenever it resizes?  Plus the function I want to
>bind needs to detect the new size of the Text Widget.  Is it possible
>to get the new width by doing: text_widget.cget( 'width' )?

You can bind a handler to the <Configure> event. The event will give
you the new width and height, but in dots, not characters.

The <Configure> event is documented in Fredrik Lundh's excellent An
Introduction to Tkinter at (wrapped for line length):

http://www.pythonware.com/library/tkinter/
  introduction/events-and-bindings.htm

Fredrik's documentation is enormously valuable if you ask me. I almost
always have a local copy open when I'm doing Tkinter programming.

Alas, it seems that the width and height options of the Text widget
aren't automatically updated. If you need the new size in characters,
it may work to find the size of your characters using the tkFont
module and do some division.

>>> from Tkinter import *
>>> import tkFont
>>> r=Tk()
>>> f=tkFont.Font(font="7x13")
>>> f.measure("x")
6
>>> f.metrics()
{'ascent': 10, 'linespace': 11, 'descent': 1, 'fixed': 1}

But sometimes that seems not to work quite right for me.

Regards,
Matt



More information about the Python-list mailing list