Search code examples
tcltk-toolkit

wait until the window is the right size before executing my function


I need to execute a function as soon as my window changes size, this function is quite consuming like this :

set originalWidth 500
set originalHeight 500

bind .l <Configure> [list procConfig %W %w %h]

proc procConfig  {W w h} {

    if {($w != $::originalWidth) || ($h != $::originalHeight)} {
        myfunction "arg1" "arg2"
    }
}

The problem I'm having is that myfunction is executed as soon as the dimensions change, not once I've released the button on my mouse (or that my widget .l is set to the right size). Is there a way of blocking the execution of myfunction and, once the desired dimension has been obtained, executing it?

Edit:

I tried to implement another solution with after command

procConfig2 .l ; # recursive proc

proc procConfig2 {W} {

    if {([winfo width $W] != $::originalWidth) || ([winfo height $W] != $::originalHeight)} {
        myfunction "arg1" "arg2"
    }
    after 10 [list procConfig2 $W]
}

but I got approximately the same result.


Solution

  • You can set a flag in the <Configure> callback to say that you will call your function, and then actually only call it when you determine that it is time to do it. The difficulty is just that sometimes it is hard to work out when the right time is; if it is driven by internal events then you can do it on <ButtonRelease> or something like that, but when it is driven by instructions from the window manager then there are no events at all to say "user has stopped changing the size". I guess you could set a delay so that it happens, say, 100 ms after the last motion event, but that's always going to be a guess. (You do that by setting an after event up as you had, but cancelling the event at the start of the handler for the next motion event.)