[Tkinter-discuss] ttk.Notebook - how to bind to tab events (tab caption has focus or mouseover)

Michael Lange klappnase at web.de
Wed Dec 15 16:21:21 CET 2010


Thus spoketh python at 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.


More information about the Tkinter-discuss mailing list