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

    From Silas Silva@21:1/5 to All on Thu Aug 17 15:03:36 2023
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From LuoChunlei@21:1/5 to All on Fri Aug 18 07:57:24 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Fri Aug 18 09:08:42 2023
    Am 18.08.2023 um 01:57 schrieb LuoChunlei:
    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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Fri Aug 18 06:19:38 2023
    Add this code:

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Fri Aug 18 06:45:03 2023
    Il giorno venerdì 18 agosto 2023 alle 15:19:43 UTC+2 [email protected] ha scritto:
    Add this code:

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

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)