tkinter left clicks on a TAB in your GUI. How to detect when user has done this tkinter left clicks on a TAB in your GUI. How to detect when user has done this tkinter tkinter

tkinter left clicks on a TAB in your GUI. How to detect when user has done this


When the tab changes, it emits the event "<<NotebookTabChanged>>", which you can bind to:

def handle_tab_changed(event):    selection = event.widget.select()    tab = event.widget.tab(selection, "text")    print("text:", tab)notebook = ttk.Notebook(...)...notebook.bind("<<NotebookTabChanged>>", handle_tab_changed)

The benefit of using this event rather than binding to a mouse click is that the binding will fire no matter what causes the tab to change. For example, if you define shortcut keys to switch tabs, binding to the mouse won't cause your handler to fire if the user uses one of those shortcut keys.


You were right, this is pretty straight forward, what you did was almost correct, you need to pass the function not the return value of the function to bind. So you will need to get rid of the parentheses after f_x. Another thing is, the bind also automatically pass an argument to the callback called event, so you will need to let f_x accept an argument.

def f_x(event): # accept the event arg    print('entered this method')tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)tab4e.bind("<Button-1>",f_x) # not f_x()


This works now detecting when a TAB Canvas has been brought to focus through a LEFT mouse click

def fTabSwitched(event):        global notebook2        l_tabText = notebook2.tab(notebook2.select(), "text")        if (l_tabText == 'eee'):           print('lets roll')      tab4a = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)    tab4b = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)    tab4c = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)    tab4d = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)    tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)    notebook2.add(tab4a,text='aaa')    notebook2.add(tab4b,text='bbb')    notebook2.add(tab4c,text='ccc')    notebook2.add(tab4d,text='ddd')    notebook2.add(tab4e,text='eee')    notebook2.bind("<ButtonRelease-1>",fTabSwitched) # must be release