Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #98: The vendor put the bug there.


comp / comp.lang.tcl / Re: Curious event behaviour

SubjectAuthor
* Curious event behaviourHelmut Giese
`* Re: Curious event behaviourRich
 `* Re: Curious event behaviourHelmut Giese
  `* Re: Curious event behaviourRich
   `- Re: Curious event behaviourHelmut Giese

1
Subject: Curious event behaviour
From: Helmut Giese
Newsgroups: comp.lang.tcl
Organization: ratiosoft
Date: Sat, 31 Aug 2024 13:49 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: hgiese@ratiosoft.com (Helmut Giese)
Newsgroups: comp.lang.tcl
Subject: Curious event behaviour
Date: Sat, 31 Aug 2024 15:49:33 +0200
Organization: ratiosoft
Lines: 53
Message-ID: <v176dj9u5vmca0l5cc320f2ph8dd8tnmne@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 31 Aug 2024 15:49:33 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a8329fd40d1c6b042d0cd442fe39256c";
logging-data="1097857"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iP1/QHEgA2jYEcwg0bTIq"
Cancel-Lock: sha1:iF9+BYeBVybJx3upfbcgMKRh+aA=
X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
View all headers

Hello out there,
take the following script:
---
package require Tk
foreach ch [winfo children "."] {destroy $ch}

# This variable controls whether the events happen during the script's
# execution or are scheduled for some time after
set evInScript 0

set c [canvas .c -width 400 -height 300 -bd 0 -bg lightyellow]
pack $c
set id1 [$c create rectangle 100 100 200 200 -fill red]
set id2 [$c create rectangle 200 100 300 200 -fill green]

foreach id [list $id1 $id2] {
$c bind $id <Button-1> [list puts "<Button-1> for id $id"]
$c bind $id <Shift-Button-1> [list puts "<Shift-Button-1> for id
$id"]
} if {$evInScript} {
after 1000
event generate $c <Button-1> -x 150 -y 150
after 2000
event generate $c <Shift-Button-1> -x 250 -y 150
} else {
after 1000 {event generate $c <Button-1> -x 150 -y 150}
after 2000 {event generate $c <Shift-Button-1> -x 250 -y 150}
} puts "Done for evInScript == $evInScript"
---
It programs two events which are to be executed either "inline" in the
script (evInScript == 1) or scheduled for some time after.
This is the result:
---
Microsoft Windows [Version 10.0.19045.4842]
(c) Microsoft Corporation. Alle Rechte vorbehalten.

d:\proj\_ToolsSE\Skeleton\TclTests>tclsh tst.tcl
Done for evInScript == 0
<Button-1> for id 1
<Shift-Button-1> for id 2

d:\proj\_ToolsSE\Skeleton\TclTests>tclsh tst.tcl
Done for evInScript == 1
---
In the second case the events never happened (or they happened but
were not recognized, which amounts to the same). I consider this a
bug. Should I submit a ticket?

This is on Windows 10 with Tcl 8.6.10 and 8.6.13
Helmut

Subject: Re: Curious event behaviour
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sat, 31 Aug 2024 14:27 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Curious event behaviour
Date: Sat, 31 Aug 2024 14:27:01 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <vav97k$11d4l$2@dont-email.me>
References: <v176dj9u5vmca0l5cc320f2ph8dd8tnmne@4ax.com>
Injection-Date: Sat, 31 Aug 2024 16:27:01 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2f31ca9108b7a3035762c515d9ff524f";
logging-data="1094805"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hR6k2yCKIwjlxCpY6BA1r"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:i2uKj2Ar4pMUs1s36zPpcTkDkJE=
View all headers

Helmut Giese <hgiese@ratiosoft.com> wrote:
> Hello out there,
> take the following script:
> ---
[see prior post for script]
> ---
> It programs two events which are to be executed either "inline" in the
> script (evInScript == 1) or scheduled for some time after.
> This is the result:
> ---
> In the second case the events never happened (or they happened but
> were not recognized, which amounts to the same). I consider this a
> bug. Should I submit a ticket?

It is a bug, but not a bug in Tk. It is a bug in your code.

I made the following initial changes to test on linux:

--- hg~ 2024-08-31 10:13:57.860784063 -0400
+++ hg 2024-08-31 10:12:26.551823871 -0400
@@ -1,9 +1,10 @@
+#!/usr/bin/wish
package require Tk
foreach ch [winfo children "."] {destroy $ch}

# This variable controls whether the events happen during the script's
# execution or are scheduled for some time after
-set evInScript 0
+set evInScript [lindex $argv 0]

set c [canvas .c -width 400 -height 300 -bd 0 -bg lightyellow]
pack $c

Launch in 'wish' and take the initial value from evInScript from the
command line instead of hard coding. Got the exact same results you
do.

Why?

Because Tk is mostly lazy evaluation, meaning when you call commands
such as [canvas] or [$c create] or "$c bind ..." some portion (if not
most) of request you are making is not executed right then and there.
Much of the work is deferred, by queueing it onto the event loop, and
waiting for your script to finish its 'work' and only then do the event
loop work items get executed.

In the "0" case you queue your two generates as events to happen
'later' (1 second and 2 seconds later) and then your script reaches
its end. That lets the event loop queue drain, and the canvas, the two
rectangles, and the bindings get created. So that when the 1 second
later event generate occurs, there is alrealy a fully initialized
canvas, with two rectangles on it, and two bindings on the rectangles.

In the "1" case, you don't do the above. You wait, in your script,
synchronously, for 1 second, never letting the event queue drain, then
you trigger an 'event' on the canvas, but the canvas is not fully
initiaized yet, so the event just 'occurs' with nothing there to
respond to it. Same with the next 2 second wait, synchronous, then an
event to an only 'half-ready' rectangle. This is why you see nothing
for the "1" case, you never let the canvas fully initialize until after
you tried poking it with the two events.

Making this additional change:

--- hg~ 2024-08-31 10:16:44.904805513 -0400
+++ hg 2024-08-31 10:12:26.551823871 -0400
@@ -15,6 +15,7 @@
$c bind $id <Button-1> [list puts "<Button-1> for id $id"]
$c bind $id <Shift-Button-1> [list puts "<Shift-Button-1> for id $id"]
}
+update idletasks
if {$evInScript} {
after 1000
event generate $c <Button-1> -x 150 -y 150

Fixes the "1" case so that both output the same output, just in
different orders:

$ ./hg 0
Done for evInScript == 0
<Button-1> for id 1
<Shift-Button-1> for id 2
^C
$ ./hg 1
<Button-1> for id 1
<Shift-Button-1> for id 2
Done for evInScript == 1
^C

That [update idletasks] drains all the queued Tk "setup" tasks for the
canvas, resulting in it now being fully initialized and ready to listen
to and respond to events.

Subject: Re: Curious event behaviour
From: Helmut Giese
Newsgroups: comp.lang.tcl
Organization: ratiosoft
Date: Sat, 31 Aug 2024 19:55 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: hgiese@ratiosoft.com (Helmut Giese)
Newsgroups: comp.lang.tcl
Subject: Re: Curious event behaviour
Date: Sat, 31 Aug 2024 21:55:13 +0200
Organization: ratiosoft
Lines: 15
Message-ID: <n1s6djp9gfulppbmi0b2cr7hdquhqm5e6h@4ax.com>
References: <v176dj9u5vmca0l5cc320f2ph8dd8tnmne@4ax.com> <vav97k$11d4l$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 31 Aug 2024 21:55:13 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a8329fd40d1c6b042d0cd442fe39256c";
logging-data="1202466"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mziOA6dy+uRE7ccJlWup2"
Cancel-Lock: sha1:NMUYutWt9G9PiZwGhQcQR1C5Eic=
X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
View all headers

Phew, what a relief. Many thanks Rich.
Yes, I tend to forget about 'update' and friends but now I should know
(and remember ).
Just an addendum for other Windows user: 'update idletasks' does not
suffice - I needed a fulll 'update'. 'idletasks' did result in a
change in so far a as window appeared immediatly - but it was empty.
Only with a plain 'update' appeared the complete window with the 2
rectangles immediately - and then the 2 events could fire as you
described in your exhaustive explanation.
I just read up on the documentation of 'update' and the way I
understand it is that 'update idletasks' should work - but I am no
native English speaker, so I let others decide. I only observed that
in my example it didn't.
Rich, thanks again for your help and have a nice weekend
Helmut

Subject: Re: Curious event behaviour
From: Rich
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sat, 31 Aug 2024 21:14 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Curious event behaviour
Date: Sat, 31 Aug 2024 21:14:05 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <vb012t$15428$1@dont-email.me>
References: <v176dj9u5vmca0l5cc320f2ph8dd8tnmne@4ax.com> <vav97k$11d4l$2@dont-email.me> <n1s6djp9gfulppbmi0b2cr7hdquhqm5e6h@4ax.com>
Injection-Date: Sat, 31 Aug 2024 23:14:05 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2f31ca9108b7a3035762c515d9ff524f";
logging-data="1216584"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3jZ2EtAWqZ7CuZ3aoOjcr"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:SDF2wTJZJ69tJjKqyWG0ZY6caHU=
View all headers

Helmut Giese <hgiese@ratiosoft.com> wrote:
> I just read up on the documentation of 'update' and the way I
> understand it is that 'update idletasks' should work - but I am no
> native English speaker, so I let others decide. I only observed that
> in my example it didn't.

It (your exact code, other than my change to make the "boolean"
settable from the CLI) worked correctly under Linux with idletasks.

So what you saw is likely some subtle difference for MSWin.

Subject: Re: Curious event behaviour
From: Helmut Giese
Newsgroups: comp.lang.tcl
Organization: ratiosoft
Date: Sun, 1 Sep 2024 09:40 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: hgiese@ratiosoft.com (Helmut Giese)
Newsgroups: comp.lang.tcl
Subject: Re: Curious event behaviour
Date: Sun, 01 Sep 2024 11:40:14 +0200
Organization: ratiosoft
Lines: 13
Message-ID: <4gd8djh8d07rof72ugd5mpn19h2t3c20ev@4ax.com>
References: <v176dj9u5vmca0l5cc320f2ph8dd8tnmne@4ax.com> <vav97k$11d4l$2@dont-email.me> <n1s6djp9gfulppbmi0b2cr7hdquhqm5e6h@4ax.com> <vb012t$15428$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 01 Sep 2024 11:40:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cb42b425265005a5328d2b6544dfe5d6";
logging-data="1535140"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iZamYNWRvCn+pUVZcCkxA"
Cancel-Lock: sha1:Qn1cIEOSwPKeV2CpAz+X8ZCKiAY=
X-Newsreader: Forte Free Agent 1.93/32.576 English (American)
View all headers

>Helmut Giese <hgiese@ratiosoft.com> wrote:
>> I just read up on the documentation of 'update' and the way I
>> understand it is that 'update idletasks' should work - but I am no
>> native English speaker, so I let others decide. I only observed that
>> in my example it didn't.
>
>It (your exact code, other than my change to make the "boolean"
>settable from the CLI) worked correctly under Linux with idletasks.
>
Yes, I know. That's why I wrote 'an addendum for Windows user'.

>So what you saw is likely some subtle difference for MSWin.
Probably

1

rocksolid light 0.9.8
clearnet tor