ttk.Notebook - how to bind to tab events (tab caption has focus or mouseover)
I'm using the ttk.Notebook widget. What object do I bind to capture events related to specific tabs? I would like to trap when a tab caption gets keyboard focus ('<FocusIn>') and when the mouse pointer is over a specific tab caption ('<Enter>'). I can trap mouse clicks on tabs (<Button-1>, <Button-3>), but I can't find a way to determine what tab a user clicked on in the case of a right click. Thank you, Malcolm
Thus spoketh python@bdurham.com unto us on Wed, 15 Dec 2010 09:28:51 -0500:
I'm using the ttk.Notebook widget. What object do I bind to capture events related to specific tabs?
I would like to trap when a tab caption gets keyboard focus ('<FocusIn>') and when the mouse pointer is over a specific tab caption ('<Enter>').
Maybe you can use the <<NotebookTabChanged>> virtual event instead?
I can trap mouse clicks on tabs (<Button-1>, <Button-3>), but I can't find a way to determine what tab a user clicked on in the case of a right click.
Here's a brief example how to identify the tab which received the right-click; the same callback could be bound to Motion events which might be used as a replacement for the Enter event. ################################## from Tkinter import * import ttk root = Tk() nb = ttk.Notebook(root) nb.pack(fill='both', expand=1) t = Text(nb) nb.add(t, text='foo') c = Canvas(nb) nb.add(c, text='bar') def on_button_3(event): if event.widget.identify(event.x, event.y) == 'label': index = event.widget.index('@%d,%d' % (event.x, event.y)) print event.widget.tab(index, 'text') nb.bind('<3>', on_button_3) root.mainloop() ################################## Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Deflector shields just came on, Captain.
Michael, Thank you so much for your example!! Re: Detect mouse events on tabs. The trick for me was understanding how to use the index method. Your technique below is exactly the solution I was looking for (I wasn't aware of the '@%d,%d' formatting technique). index = event.widget.index('@%d,%d' % (event.x, event.y)) Re: Detect when a tab caption has keyboard focus (<FocusIn> or similar). The <<NotebookTabChanged>> does not correspond to the tab caption gaining keyboard focus. And the notebook widget generates a single <FocusIn> event when it gets focus. Is there a way to drill-down to the Tk label controls in the captions and bind to their <FocusIn> events? Or is there a way to expose the tabs as actual widgets whose events can be bound to? Thanks again for all your help in these forums! Malcolm
Hi, Thus spoketh python@bdurham.com unto us on Wed, 15 Dec 2010 14:48:18 -0500: (...)
Re: Detect when a tab caption has keyboard focus (<FocusIn> or similar).
The <<NotebookTabChanged>> does not correspond to the tab caption gaining keyboard focus.
And the notebook widget generates a single <FocusIn> event when it gets focus.
Is there a way to drill-down to the Tk label controls in the captions and bind to their <FocusIn> events? Or is there a way to expose the tabs as actual widgets whose events can be bound to?
I don't think this is possible, it looks like the notebook widget itself is one window that catches the <Tab> event and decides what to do - draw the focus indicator onto the next tab or pass the focus to another window. But, a far as I see, at least here with IceWm any time a tab receives the keyboard focus it will be selected as well, so maybe you can play around with the select mechanism, depending on what exactly you want to achieve. Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. Each kiss is as the first. -- Miramanee, Kirk's wife, "The Paradise Syndrome", stardate 4842.6
Michael,
Is there a way to drill-down to the Tk label controls in the captions and bind to their <FocusIn> events? Or is there a way to expose the tabs as actual widgets whose events can be bound to?
I don't think this is possible, it looks like the notebook widget itself is one window that catches the <Tab> event and decides what to do - draw the focus indicator onto the next tab or pass the focus to another window.
But, a far as I see, at least here with IceWm any time a tab receives the keyboard focus it will be selected as well, so maybe you can play around with the select mechanism, depending on what exactly you want to achieve.
Thank you for taking the time to investigate this behavior. I just posted an advanced example of a ttk.Notebook with close controls on the tabs. Apparently, it is possible to drill down into the child tab controls of the ttk Notebook using out-of-the-box ttk (without resorting to tcl magic). I'm studying this example now to see what may be possible regarding raising a got focus event for individual tab widgets ... as well as other advanced capabilities as well. Cool stuff!! Malcolm
Although not quite an answer, I found this useful... _notebooknotify = {} def addopenlistener(self,page,action): self._notebooknotify[str(page)] = action def notify(event): id = str(event.widget.select()) if id in self._notebooknotify: self._notebooknotify[id].run() self.addopenlistener(child,action) # causes action.run() to be invoked every time child is opened and it also give you a good place to put in tracing/logging/etc -- View this message in context: http://python.6.n6.nabble.com/ttk-Notebook-how-to-bind-to-tab-events-tab-cap... Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
participants (3)
-
hamiljf -
Michael Lange -
python@bdurham.com