【问题标题】:Need to install a mousewheel in a ttk notebook with a scrollbar需要在带有滚动条的ttk笔记本中安装鼠标滚轮
【发布时间】:2018-06-28 00:26:55
【问题描述】:

我需要鼠标滚轮才能在所有选项卡上运行。现在它只适用于星期五标签。鼠标滚轮的控制器在第 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()

【问题讨论】:

  • 发布您的答案并接受它

标签: python tkinter scrollbar mousewheel ttk


【解决方案1】:

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

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

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)

【讨论】:

  • Bryan Oakley,我插入了您发布的代码,并且运行顺利。这比我想象的要复杂一些,但它是有道理的,而且它最有效。谢谢!你就是男人!
  • 在将这段代码转移到一个更大的程序中时,我遇到了一个问题,因为调用 scroll_tab() 的代码位于函数内部。有一个问题,因为“C”没有通过。我这样修改了代码:我使用了“def tab_selected(i)...”和“C[i].bind(""...”来调用它。但没有使用“def scroll_tab( event):..." 和 "out_root.bind_all.." 我使用了 "C[current_tab].bind_all('', lambda event: C[current_tab].yview_scroll(int(-1 * (event.delta / 120)), "units"))" 在 "C[i].bind(""..." 下的行中这似乎有效。
猜你喜欢
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多