Version 1 of sizeinfo

Updated 2003-02-19 23:04:07

MGS In response to a question on Ask, and it shall be given. I knocked up this little package. The question/request was:

"When a user resizes a toplevel window I want to get a continuous feedback about the window's dimensions, so that I am able to display that on a label and user can set the exact size. The problem with <Configure> event bind is that it is fired at the end of the resizing operation.I want dimensions 'live' during resizing." by Neo [email protected].

I'm not sure if this completely satisfies, but here is a little utility to display the size and position of a toplevel (or any?) window in response to a <Configure> event.

 # sizeinfo.tcl --

 # Display window size info while resizing.

 # Version   : 0.0.1
 # Author    : Mark G. Saye
 # Email     : [email protected]
 # Copyright : Copyright (C) 2003
 # Date      : February 19, 2003

 # ======================================================================

   namespace eval sizeinfo {
     package require Tk
     package provide sizeinfo 0.0.1
   }

 # ======================================================================

 # create --

 proc sizeinfo::create {W} {

   toplevel     $W.sizeinfo -bd 0
   wm withdraw  $W.sizeinfo
   update idletasks
   wm transient $W.sizeinfo $W
   wm overrideredirect $W.sizeinfo 1
   label $W.sizeinfo.label -relief raised -bd 2
   pack  $W.sizeinfo.label

 }

 # ======================================================================

 # destroy --

 proc sizeinfo::destroy {W} {

   ::destroy $W.sizeinfo

 }

 # ======================================================================

 # refresh --

 proc sizeinfo::show {W w h x y} {

   variable $W
   upvar  0 $W _

   if { [info exists _(after)] } { after cancel $_(after) }

   if { ![winfo exists $W.sizeinfo] } { create $W }

   set label $W.sizeinfo.label

   $label configure -text "$w x $h + $x + $y"

   set x0 [expr {$x + ($w / 2)}]
   set y0 [expr {$y + ($h / 2)}]

   set _w [winfo reqwidth  $label]
   set _h [winfo reqheight $label]

   set _x [expr {$x0 - ($_w / 2)}]
   set _y [expr {$y0 - ($_h / 2)}]

   wm geometry $W.sizeinfo ${_w}x${_h}+${_x}+${_y}

  if { ![winfo ismapped $W.sizeinfo] } {
    wm deiconify $W.sizeinfo
     update idletasks
   }

   set _(after) [after 1000 [list sizeinfo::destroy $W]]

 }

 # ======================================================================

 proc sizeinfo::sizeinfo {W} {

   if { [string equal $W .] } { set w "" } { set w $W }

   bind $W <Configure> [list sizeinfo::show $w %w %h %x %y]

 }

 # ======================================================================

 # Demo code

   if { [info exists argv0] && [string equal [info script] $argv0] } {
     toplevel .t
     sizeinfo::sizeinfo .
     sizeinfo::sizeinfo .t
   }

 # ======================================================================

Mark G. Saye