5

I have a GUI written in tkinter and all works fine. I want to enhance it so that when a user left clicks on a certain tab with the mouse, a method is executed. I thought this would be straight forward but I can't get it working. My code is

def f_x():
    print('entered this method')

tab4e = ttk.Frame(notebook2,width=C_WIDTH,height=C_TAB_HEIGHT)
tab4e.bind("<Button-1>",f_x())
Ab Bennett
  • 1,391
  • 17
  • 24

3 Answers3

9

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
3

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()
Taku
  • 31,927
  • 11
  • 74
  • 85
  • @Ab Bennett From [review](http://stackoverflow.com/review/suggested-edits/16185184), that edit did not make any improvements to the answer, if you don't want the comments, please remove them yourself after you have copied it. They are for hinting everyone who's (going to) reading this. – Taku May 20 '17 at 23:27
  • This works in terms of executing when a user left clicks on the TAB canvas once it has been entered. What I want is to execute a function when they left click the TAB itself to enter the tab Canvas. I hope this makes sense. Thanks for the error with my code though – Ab Bennett May 20 '17 at 23:29
  • this question involves no key strokes. When a user LEFT mouse clicks on a TAB in a gui to bring it to focus, I want a function to execute. The code I/you have only executes when a user LEFT mouse clicks on the TAB Canvas itself after it has come to focus.i.e. after the fact – Ab Bennett May 20 '17 at 23:37
  • @AbBennett You can bind do `notebook2.bind(...)` instead, and place the creation of `tac4e = ttk.Frame(...)` into the callback `f_x`, to create it when the user click on the `notebook2` thing (I don't know what it is since you never told us). Please add more relevant code. This question is becoming a [X Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Taku May 20 '17 at 23:46
  • Note that the bound function won't be called if the user uses the keyboard to switch tabs, or if the tab is changed programatically from some other part of the code. – Bryan Oakley May 21 '17 at 00:49
0

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
Ab Bennett
  • 1,391
  • 17
  • 24
  • 1
    Note that the bound function won't be called if the user uses the keyboard to switch tabs, or if the tab is changed programatically from some other part of the code. – Bryan Oakley May 21 '17 at 00:51