需要在带有滚动条的ttk笔记本中安装鼠标滚轮
Need to install a mousewheel in a ttk notebook with a scrollbar
我需要鼠标滚轮在所有选项卡上都起作用。现在它只适用于星期五标签。鼠标滚轮的控制器在第 18 行附近。我有两种可能的方法来控制鼠标滚轮,其中一种被评论为 out.The 如果我将 "bind_all" 替换为 "bind",则第二种方法有效但只有未被小部件覆盖的区域才能通过鼠标滚轮滚动。无论如何,鼠标滚轮在最后一个选项卡上起作用的事实告诉我鼠标滚轮可以在所有选项卡上起作用,我就是想不通。
编辑:我过早地发布了这个问题的解决方案。我不得不收回它,因为我发现 canvas 被小部件覆盖的区域无法用鼠标滚轮滚动。当整个canvas都被覆盖时,鼠标滚轮就没用了。
from tkinter import *
from tkinter import ttk
out_root = Tk()
out_root.title("KLUSTERBOX - Output")
# list the names of the tabs
tab = ["saturday", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday"]
C = ["C0","C1","C2","C3","C4","C5","C6"]
# create a notebook
tabControl = ttk.Notebook(out_root) # Create Tab Control
tabControl.pack()
for i in range(7): # loop to fill notebook pages.
tabs = Frame(tabControl) # put frame in notebook
tabs.pack()
tabControl.add(tabs, text="{}".format(tab[i])) # Add the tab
C[i] = Canvas(tabs) # put canvas inside notebook frame
S = Scrollbar(tabs, command=C[i].yview) # define and bind the scrollbar with the canvas
C[i].config(yscrollcommand=S.set, scrollregion=(0, 0, 10000, 10000)) # bind the canvas with the scrollbar
# Enable mousewheel option 1
C[i].bind_all('<MouseWheel>', lambda event: C[i].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# Enable mousewheel option 2
# if i==0: C[0].bind_all('<MouseWheel>', lambda event: C[0].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==1: C[1].bind_all('<MouseWheel>', lambda event: C[1].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==2: C[2].bind_all('<MouseWheel>', lambda event: C[2].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==3: C[3].bind_all('<MouseWheel>', lambda event: C[3].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==4: C[4].bind_all('<MouseWheel>', lambda event: C[4].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==5: C[5].bind_all('<MouseWheel>', lambda event: C[5].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==6: C[6].bind_all('<MouseWheel>', lambda event: C[6].yview_scroll(int(-1 * (event.delta / 120)), "units"))
S.pack(side=RIGHT, fill=BOTH)
C[i].pack(side=LEFT, fill=BOTH, expand=True)
F = Frame(C[i]) # put a frame in the canvas
F.pack()
C[i].create_window((0, 0), window=F, anchor=NW) # create window with frame
ttk.Label(F, text="hello tab {}".format(tab[i])).grid(row=0) # label for first line
for ii in range(100):
# fill in text for body of document.
Label(F, text="This label spans the entire lenght of the window. Line number: {}".format(ii)).grid(row=ii+1)
out_root.mainloop()
解决方案是让您的鼠标滚轮功能找出哪个 canvas 是可见的 canvas,然后滚动它。一种简单的方法是创建一个绑定,用当前选择的 canvas(或 canvas 的编号)更新全局变量。
首先定义一个在选择选项卡时调用的函数:
def tab_selected(i):
global current_tab
current_tab = i
接下来,当 canvas 的可见性发生变化时调用此函数:
for i in range(7):
...
C[i].bind("<Map>", lambda event, i=i: tab_selected(i))
...
那应该给你一个名为 current_tab
的全局变量,它将始终设置为可见的选项卡。
接下来,创建一个滚动当前选项卡的函数:
def scroll_tab(event):
global current_tab
C[current_tab].yview_scroll(int(-1 * (event.delta / 120)), "units")
最后,在循环外创建一个全局绑定,以便在使用鼠标滚轮时调用此函数:
out_root.bind_all("<MouseWheel>", scroll_tab)
我需要鼠标滚轮在所有选项卡上都起作用。现在它只适用于星期五标签。鼠标滚轮的控制器在第 18 行附近。我有两种可能的方法来控制鼠标滚轮,其中一种被评论为 out.The 如果我将 "bind_all" 替换为 "bind",则第二种方法有效但只有未被小部件覆盖的区域才能通过鼠标滚轮滚动。无论如何,鼠标滚轮在最后一个选项卡上起作用的事实告诉我鼠标滚轮可以在所有选项卡上起作用,我就是想不通。
编辑:我过早地发布了这个问题的解决方案。我不得不收回它,因为我发现 canvas 被小部件覆盖的区域无法用鼠标滚轮滚动。当整个canvas都被覆盖时,鼠标滚轮就没用了。
from tkinter import *
from tkinter import ttk
out_root = Tk()
out_root.title("KLUSTERBOX - Output")
# list the names of the tabs
tab = ["saturday", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday"]
C = ["C0","C1","C2","C3","C4","C5","C6"]
# create a notebook
tabControl = ttk.Notebook(out_root) # Create Tab Control
tabControl.pack()
for i in range(7): # loop to fill notebook pages.
tabs = Frame(tabControl) # put frame in notebook
tabs.pack()
tabControl.add(tabs, text="{}".format(tab[i])) # Add the tab
C[i] = Canvas(tabs) # put canvas inside notebook frame
S = Scrollbar(tabs, command=C[i].yview) # define and bind the scrollbar with the canvas
C[i].config(yscrollcommand=S.set, scrollregion=(0, 0, 10000, 10000)) # bind the canvas with the scrollbar
# Enable mousewheel option 1
C[i].bind_all('<MouseWheel>', lambda event: C[i].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# Enable mousewheel option 2
# if i==0: C[0].bind_all('<MouseWheel>', lambda event: C[0].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==1: C[1].bind_all('<MouseWheel>', lambda event: C[1].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==2: C[2].bind_all('<MouseWheel>', lambda event: C[2].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==3: C[3].bind_all('<MouseWheel>', lambda event: C[3].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==4: C[4].bind_all('<MouseWheel>', lambda event: C[4].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==5: C[5].bind_all('<MouseWheel>', lambda event: C[5].yview_scroll(int(-1 * (event.delta / 120)), "units"))
# if i==6: C[6].bind_all('<MouseWheel>', lambda event: C[6].yview_scroll(int(-1 * (event.delta / 120)), "units"))
S.pack(side=RIGHT, fill=BOTH)
C[i].pack(side=LEFT, fill=BOTH, expand=True)
F = Frame(C[i]) # put a frame in the canvas
F.pack()
C[i].create_window((0, 0), window=F, anchor=NW) # create window with frame
ttk.Label(F, text="hello tab {}".format(tab[i])).grid(row=0) # label for first line
for ii in range(100):
# fill in text for body of document.
Label(F, text="This label spans the entire lenght of the window. Line number: {}".format(ii)).grid(row=ii+1)
out_root.mainloop()
解决方案是让您的鼠标滚轮功能找出哪个 canvas 是可见的 canvas,然后滚动它。一种简单的方法是创建一个绑定,用当前选择的 canvas(或 canvas 的编号)更新全局变量。
首先定义一个在选择选项卡时调用的函数:
def tab_selected(i):
global current_tab
current_tab = i
接下来,当 canvas 的可见性发生变化时调用此函数:
for i in range(7):
...
C[i].bind("<Map>", lambda event, i=i: tab_selected(i))
...
那应该给你一个名为 current_tab
的全局变量,它将始终设置为可见的选项卡。
接下来,创建一个滚动当前选项卡的函数:
def scroll_tab(event):
global current_tab
C[current_tab].yview_scroll(int(-1 * (event.delta / 120)), "units")
最后,在循环外创建一个全局绑定,以便在使用鼠标滚轮时调用此函数:
out_root.bind_all("<MouseWheel>", scroll_tab)