Does anyone know how to trigger a function once a different tab is selected in the tkinter notebook?
This is hat i what to accomplish.
Lets say i have two tabs, tab1 and tab2:
if tab1 is selected:
canvas3.unbind_all()
canvas2.bind_all('<MouseWheel>', lambda event: canvas2.yview_scroll(int(-1 * (event.delta / 120)),"units"))
elif tab 2 is selected:
canvas2.unbind_all()
canvas3.bind_all('<MouseWheel>', lambda event: canvas3.yview_scroll(int(-1 * (event.delta / 120)), "units"))
You can bind <<NotebookTabChanged>>
event on the notebook:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
nb = ttk.Notebook(root, width=800, height=600)
nb.pack()
frame1 = ttk.Frame(nb)
frame2 = ttk.Frame(nb)
nb.add(frame1, text='Tab1')
nb.add(frame2, text='Tab2')
def on_tab_change(event):
tab = event.widget.tab('current')['text']
if tab == 'Tab1':
#canvas3.unbind_all()
#canvas2.bind_all('<MouseWheel>', lambda event: canvas2.yview_scroll(int(-1 * (event.delta / 120)),"units"))
elif tab == 'Tab2':
#canvas2.unbind_all()
#canvas3.bind_all('<MouseWheel>', lambda event: canvas3.yview_scroll(int(-1 * (event.delta / 120)), "units"))
nb.bind('<<NotebookTabChanged>>', on_tab_change)
root.mainloop()