gpt4 book ai didi

python - 如何从 tkinter 笔记本中触发选项卡更改功能?

转载 作者:行者123 更新时间:2023-12-05 00:57:24 27 4
gpt4 key购买 nike

有谁知道在 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"))

最佳答案

您可以绑定(bind)<<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()

关于python - 如何从 tkinter 笔记本中触发选项卡更改功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60187932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com