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

binding <Enter> to Button class

74 views
Skip to first unread message

xol odho

unread,
Jul 27, 2022, 3:06:09 PM7/27/22
to
Hello. Learning some Tk basics here.

It seems that binding <Enter> to the Button class does not work as I would expect. For instance, this:

pack [button .b1 -text b1 -command {exit 1}]
bind Button <Enter> {puts "Button <Enter> : %W"}

When the mouse cursor enters the button area, it doesn't highlight properly, and does not respond to click so the command is not executed. However, if the event is bound to the specific button widget then it works ok:

pack [button .b1 -text b1 -command {exit 1}]
bind .b1 <Enter> {puts ".b1 <Enter>"}

Now the button reacts properly.

So why is the behavior different? In both cases it is the same proc that is bound to the same <Enter> event, just the binding tag is different. I find this confusing. What's wrong?

Schelte

unread,
Jul 27, 2022, 4:18:05 PM7/27/22
to
On 27/07/2022 21:06, xol odho wrote:
> So why is the behavior different? In both cases it is the same proc that is bound to the same <Enter> event, just the binding tag is different. I find this confusing. What's wrong?

The difference is that by binding to the <Enter> event in the Button
class, you overwrite the normal class binding that takes care of
highlighting the button and setting up variables needed when clicking
the button. That's why those things stop working.

If you would add your binding instead of replacing the standard binding,
it works (note the "+"):

bind Button <Enter> {+puts "Button <Enter> : %W"}


Schelte.


xol odho

unread,
Jul 27, 2022, 6:31:38 PM7/27/22
to
I see! Hey, many thanks for the tip, it works now. I had a suspicion that binding <Enter> to the whole Button class was disrupting something. Now it's clear. So one has to be careful with these types of general bindings.

Gerald Lester

unread,
Jul 28, 2022, 11:21:43 AM7/28/22
to
On 7/27/22 17:31, xol odho wrote:
> I see! Hey, many thanks for the tip, it works now. I had a suspicion that binding <Enter> to the whole Button class was disrupting something. Now it's clear. So one has to be careful with these types of general bindings.

Generally it is a good idea to look at what bindings exist on a class
before doing a binding to it.

bind <Button>

will return all the events currently bound to the Button class.


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

nemethi

unread,
Jul 28, 2022, 12:23:08 PM7/28/22
to
Am 28.07.22 um 17:21 schrieb Gerald Lester:
> On 7/27/22 17:31, xol odho wrote:
>> I see! Hey, many thanks for the tip, it works now. I had a suspicion
>> that binding <Enter> to the whole Button class was disrupting
>> something. Now it's clear. So one has to be careful with these types
>> of general bindings.
>
> Generally it is a good idea to look at what bindings exist on a class
> before doing a binding to it.
>
> bind <Button>
>
> will return all the events currently bound to the Button class.
>
>

You meant surely

bind Button

rather than

bind <Button>

--
Csaba Nemethi https://www.nemethi.de mailto:csaba....@t-online.de

Gerald Lester

unread,
Jul 29, 2022, 9:12:03 AM7/29/22
to
On 7/28/22 11:23, nemethi wrote:
> Am 28.07.22 um 17:21 schrieb Gerald Lester:
>> On 7/27/22 17:31, xol odho wrote:
>>> I see! Hey, many thanks for the tip, it works now. I had a suspicion
>>> that binding <Enter> to the whole Button class was disrupting
>>> something. Now it's clear. So one has to be careful with these types
>>> of general bindings.
>>
>> Generally it is a good idea to look at what bindings exist on a class
>> before doing a binding to it.
>>
>> bind <Button>
>>
>> will return all the events currently bound to the Button class.
>>
>>
>
> You meant surely
>
>     bind Button
>
> rather than
>
>     bind <Button>
>
Yeap
0 new messages