如何从 tkinter notebook 触发选项卡更改功能?

how to trigger a function on tab change from tkinter notebook?

有谁知道如何在 tkinter 笔记本中选择不同的选项卡后触发功能?

这就是我要完成的事情。

假设我有两个选项卡,tab1 和 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"))

您可以在笔记本上绑定<<NotebookTabChanged>>事件:

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()