0

I want to run function when I click on tabs.

For example, I have two tabs

def refresh():
    if str(notebook.index(notebook.select())) == "2":
        refresh2()
    else:
        refresh1()

notebook.bind("<<NotebookTabChanged>>", refresh())

Doesn't work. How can I fix it ?

Alex
  • 11
  • 4
  • 1
    `notebook.bind(..., refresh())` should be `notebook.bind(..., lambda e: refresh())`. Your version will execute `refresh()` immediately and use the result of `refresh()` (which is `None`) as the callback of `.bind()`. – acw1668 Aug 05 '21 at 08:31
  • @acw1668: in this case there is no need for `lambda`, but you're right that the parenthesis need to be removed. – Bryan Oakley Aug 05 '21 at 13:55
  • @Bryan If `lambda` is not used, then `refresh()` should be changed to accept an argument, the event object. – acw1668 Aug 05 '21 at 13:59

1 Answers1

1

You always need to provide the reference of the function, not call the function. Doing refresh() will call the function. And when the function is executed, the returned value will be assigned. In your case, it is None or nothing.

So, you are basically telling tkinter to do nothing when the "<<NotebookTabChanged>>" event is raised.

def refresh():
    if str(notebook.index(notebook.select())) == "2":
        refresh2()
    else:
        refresh1()

notebook.bind("<<NotebookTabChanged>>", refresh)