需要在带有滚动条的ttk笔记本中安装鼠标滚轮

时间:2018-06-28 00:26:55

标签: python tkinter scrollbar mousewheel ttk

我需要鼠标滚轮才能在所有选项卡上起作用。现在,它仅在“星期五”选项卡上起作用。鼠标滚轮的控制器在第18行附近。我有两种可能的方法来控制鼠标滚轮,其中一种已被注释掉。第二种方法是,如果我将“ bind_all”替换为“ bind”,但仅将那些小部件未覆盖的区域可通过鼠标滚轮滚动。无论如何,鼠标滚轮可以在最后一个选项卡上工作的事实告诉我,鼠标滚轮可以在所有选项卡上工作,我只是想不通。

编辑:我过早发布了针对此问题的解决方案。我不得不退缩,因为我发现小部件覆盖的画布区域无法使用鼠标滚轮滚动。当整个画布被覆盖时,鼠标滚轮没有用。

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

1 个答案:

答案 0 :(得分:0)

解决方案是让鼠标滚轮功能找出哪个画布是可见的画布,然后滚动它。一种简单的方法是创建一个绑定,用当前选择的画布(或画布的编号)更新全局变量。

首先定义一个在选择选项卡时要调用的函数:

def tab_selected(i):
    global current_tab
    current_tab = i

接下来,在画布的可见性发生变化时调用此函数:

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)