【发布时间】: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