Menu

#56 tkcon: [puts] causes idletasks to be processed

None
open
nobody
None
3
2017-09-03
2017-09-03
No

In tkcon, the [puts] command calls [update idletasks]. This can cause very weird errors that are hard to indentify if one does not know that.

Here's a simple (and silly) example that illustrates the issue:

package require Tk 8.6

label .l1 -text "Label 1"
label .l2 -text "Label 2"
button .b -text "Start test" -command {startTest}
grid .l1
grid .l2
grid .b

bind .l1 <Configure> {confProc %W}
bind .l2 <Configure> {confProc %W}

proc startTest {} {
    .l1 configure -width 20
    puts "configured .l1"
    .l2 configure -width 20
    puts "configured .l2"

    return
}

proc confProc {w} {
    puts "ConfigureEvent for $w"

    return
}

Pressing the "Start test" button, one would expect this output:

configured .l1
configured .l2
ConfigureEvent for .l2
ConfigureEvent for .l1

Instead, the result is:

configured .l1
ConfigureEvent for .l2
ConfigureEvent for .l1
configured .l2
ConfigureEvent for .l2

The problem seems to be known. Around line 3800, tkcon.tcl says:

    ## WARNING: This update should behave well because it uses idletasks,
    ## however, if there are weird looping problems with events, or
    ## hanging in waits, try commenting this out.
    if {$len} {
        tkcon console see output
        update idletasks
    }

Commenting out that line does indeed solve the problem. The downside is that, without that [updtade], the output of a longer task will show only after it is complete.

I am aware that there is no perfect solution to this problem (at least, I couldn't think of any).

A few things could be improved, though:

  • Document the issue
  • Make the behaviour of [puts] configurable. IMHO, the default setting shoud be not to use [update]. It's the safer choice: the user who's not aware of the issue will notice that there's a problem with the output of [puts], and it's likely that they'll find out pretty quickly that it has to do with tkcon. The other way around, finding the cause of the unexpected results can be very tricky - you just don't expect that [puts] will enter the event loop!

Discussion


Log in to post a comment.