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

Making binding work only when given tab is selected in ttk::notebook

62 views
Skip to first unread message

Silas Silva

unread,
Aug 17, 2023, 6:03:39 PM8/17/23
to
Hello.

I'd like to have the same binding for different frames, that are both added as independent tabs in a ttk::notebook widget. See:

ttk::notebook .nb
frame .f1
frame .f2
.nb add .f1 -text f1
.nb add .f2 -text f2
pack .nb
bind .f1 <a> {puts ".f1 event!"}
bind .f2 <a> {puts ".f2 event!"}

Even though I select the right tab, I cannot trigger such event. I can make this event to get triggered, if I bind it to the toplevel window (in this case, .), but then, I cannot differentiate which event I'm trying to trigger (both are bound to the same key).

Ok, there are workarounds such as verifying which tab is active, or even bind/unbind (argh!) as I switch tabs, but I was wondering if there is a more straightforward manner, maybe with bindtags?

Thanks!

LuoChunlei

unread,
Aug 17, 2023, 7:57:32 PM8/17/23
to
On 8/18/23 6:03 AM, Silas Silva wrote:
I think this is general problem, because the frame have ho focus, so the
key <a> can not send to it.

Harald Oehlmann

unread,
Aug 18, 2023, 3:08:47 AM8/18/23
to
I think, that is the point.

You may check with an additional widget, which constantly shows the
current focus. I suppose, the tab label has still the focus.
pack [label .l]
proc showfocus {} {
catch {focus} Focus
::.l configure -text $Focus
after 1000 showfocus
}
showfocus

As a frame does not take focus, it may be difficult to do it like that.

I suppose, binding to the tabbed notebook and then checking the current
front tab would be a better solution.

Take care,
Harald

aldo.w....@gmail.com

unread,
Aug 18, 2023, 9:19:43 AM8/18/23
to
Add this code:

# set focus on the selected slave widget
bind .nb <<NotebookTabChanged>> {
focus [lindex [%W tabs] [%W index current]]
}

aldo.w....@gmail.com

unread,
Aug 18, 2023, 9:45:06 AM8/18/23
to
Another solution could be

bind .f1 <Enter> { focus %W }
bind .f2 <Enter> { focus %W }
...

The only difference with this approach is that after selecting a notebook tab, you must move your mouse-cursor inside the frame
0 new messages