Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove added binding command

64 views
Skip to first unread message

Harald Oehlmann

unread,
Apr 12, 2019, 9:39:01 AM4/12/19
to
Hi TCL'ers,

is there a common method to remove added binding commands:

Existing binding command:
bind . a otherhandler

My own binding;
bind . a +myhandler

Another module adds its handler:
bind .a +anotherhandler

Is there a common way to remove myhandler from the list of bindings
without touching the other ones ?

Thank you,
Harald

Harald Oehlmann

unread,
Apr 12, 2019, 9:54:57 AM4/12/19
to
I wrote this procedure to do the job:

# Remove first Cmd from bind pattern.
# @return true if removed
proc gui::guiBindRemove {Win Pattern CmdIn} {
set CmdCur [bind $Win $Pattern]
while {-1 != [set Pos [string first $CmdIn $CmdCur]]} {
# >> Check if found string starts at the beginning of after a
new-line
if { $Pos == 0 || [string index $CmdCur $Pos-1] eq "\n" } {
# >> Check if found string ends at end or before a new-line
set PosEnd [expr {$Pos + [string length $CmdIn]}]
if { $PosEnd == [string length $CmdCur]
|| [string index $CmdCur $PosEnd] eq "\n"
} {
# Binding found
if {$Pos != 0} {
set CmdNew [string range $CmdCur 0 $Pos-2]
} else {
set CmdNew ""
}
if {$PosEnd!= [string length $CmdCur]} {
if {$CmdNew ne ""} {
append CmdNew \n
}
append CmdNew [string range $CmdCur $PosEnd+1 end]
}
bind $Win $Pattern $CmdNew
return 1
}
}
}
return 0
}

Is this wise ?
Should it go to the wiki ?
Or in a TIP for bind Win Pattern -CmdIn ?

Thanks,
Harald

Christian Gollwitzer

unread,
Apr 12, 2019, 10:59:07 AM4/12/19
to
Am 12.04.19 um 15:38 schrieb Harald Oehlmann:
Yes, don't add it in this way, it is a misdesign of Tk events. There is
another level of indirection, the bindtags. Bindtags are a list, and
therefore you can easily add and remove bindings. Snit, e.g., manages
this for you, if you use a snit::widgetadaptor.

Another way, if you "misuse" virtual events for signalling between
modules in a big program, I suggest you take a look at uevent. This is a
module in tcllib which implements an event mechanism disconnected from
Tk, and there each handler/listener gets a token (similar to an afterid)
with which you can unsubscribe. This is way cleaner of you have non-GUI
objects.

Christian

Harald Oehlmann

unread,
Apr 12, 2019, 11:28:33 AM4/12/19
to
Thank you, Christian, so I am not missing the obvious.

I am in AndroWish where a lot of information valid for a lot of modules
is delivered by virtual events.

bind . <<WillEnterBackground>> +ScannerUnregister

I wanted to add a command without disturbing any other modules using the
same virtual event.

I suppose, I can not use nor bindtags, nor uevent for this purpose.

By the way, in my posted program, there is an infinite loop.

Thanks,
Harald

Harald Oehlmann

unread,
Apr 12, 2019, 11:56:33 AM4/12/19
to
I have add all this on a wiki page:

https://wiki.tcl-lang.org/page/bind%3A+remove+one+command

Enjoy,
Harald

Christian Gollwitzer

unread,
Apr 12, 2019, 1:49:23 PM4/12/19
to
Am 12.04.19 um 17:28 schrieb Harald Oehlmann:
> I am in AndroWish where a lot of information valid for a lot of modules
> is delivered by virtual events.
>
> bind . <<WillEnterBackground>> +ScannerUnregister

This is the classic case of "misusing" Tk events - an event which has
multiple listeners cannot be cleanly expressed in Tk.
>
> I wanted to add a command without disturbing any other modules using the
> same virtual event.
>
> I suppose, I can not use nor bindtags, nor uevent for this purpose.

Yes, you can - if you have all the modules under your control. Refire
the event as a uevent in the main setup:

bind . <<WillEnterBackground>> { uevent::generate AndroidWish GoesDown }

then, in each module, you do:

set myid [uevent::bind AndroidWish GoesDown mymethod]

and when you unload the module

uevent::unbind $myid


We use uevent in aRTist precisely to do this - signalling general events
that are not connected to a widget.

Christian

Mike Griffiths

unread,
Apr 12, 2019, 5:12:42 PM4/12/19
to
You can still use bindtags:

> bindtags
. Wish all
> bindtags . [linsert [bindtags .] 1 CustomFoo]
> bindtags .
. CustomFoo Wish all
> bind CustomFoo <<WillEnterBackground>> [list ScannerUnregister]

(Output above is on Wish on Windows; default bindtags may be different on AndroWish, or already altered by other libraries, but by using [linsert] as above you can inject your new bindtag safely regardless)

Gerald Lester

unread,
Apr 13, 2019, 5:45:09 PM4/13/19
to
On 4/12/19 12:49 PM, Christian Gollwitzer wrote:
> Am 12.04.19 um 17:28 schrieb Harald Oehlmann:
>> I am in AndroWish where a lot of information valid for a lot of modules
>> is delivered by virtual events.
>>
>> bind . <<WillEnterBackground>> +ScannerUnregister
>
> This is the classic case of "misusing" Tk events - an event which has
> multiple listeners cannot be cleanly expressed in Tk.

This is not so much a misuse as a very old way of doing things that
predates bindtags.

IMHO, this should not appear in any modern code, instead make use of
bindtags.

>>...

--
+----------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+----------------------------------------------------------------------+

sled...@gmail.com

unread,
Apr 19, 2019, 2:54:13 PM4/19/19
to
slightly off topic...uevent is a super solution.

However, Komodo IDE crashes upon "package require uevent". Otherwise, I like the IDE

Is there another IDE\debugger that you would recommend? Have tried Ramdebugger, but like Komodo (for tcl) , not really supported

Rick
0 new messages