【发布时间】:2020-10-10 18:03:39
【问题描述】:
我正在尝试制作一个带有标签的应用程序。我将在标签上有很多按钮,所以我需要在每个标签上都有一个可滚动的框架。以下是我从here 获取的代码:
import tkinter as tk
from tkinter import ttk
# ************************
# Scrollable Frame Class
# ************************
class ScrollFrame(tk.Frame):
def __init__(self, parent):
super().__init__(parent) # create a frame (self)
self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff") #place canvas on self
self.viewPort = tk.Frame(self.canvas, background="#ffffff") #place a frame on the canvas, this frame will hold the child widgets
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview) #place a scrollbar on self
self.canvas.configure(yscrollcommand=self.vsb.set) #attach scrollbar action to scroll of canvas
self.vsb.pack(side="right", fill="y") #pack scrollbar to right of self
self.canvas.pack(side="left", fill="both", expand=True) #pack canvas to left of self and expand to fil
self.canvas_window = self.canvas.create_window((4,4), window=self.viewPort, anchor="nw", #add view port frame to canvas
tags="self.viewPort")
self.viewPort.bind("<Configure>", self.onFrameConfigure) #bind an event whenever the size of the viewPort frame changes.
self.canvas.bind("<Configure>", self.onCanvasConfigure) #bind an event whenever the size of the viewPort frame changes.
self.canvas.bind_all("<MouseWheel>", self.onScroll) #bind an event whenever the size of the viewPort frame changes.
self.onFrameConfigure(None) #perform an initial stretch on render, otherwise the scroll region has a tiny border until the first resize
def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all")) #whenever the size of the frame changes, alter the scroll region respectively.
def onCanvasConfigure(self, event):
'''Reset the canvas window to encompass inner frame when required'''
canvas_width = event.width
self.canvas.itemconfig(self.canvas_window, width = canvas_width) #whenever the size of the canvas changes alter the window region respectively.
def onScroll(self,event):
if event.state == 0:
self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")
return "break"
elif event.state == 1:
self.canvas.xview_scroll(int(-1*(event.delta/120)), "units")
return "break"
# ********************************
# Example usage of the above class
# ********************************
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.scrollFrame = ScrollFrame(self) # add a new scrollable frame.
# Now add some controls to the scrollframe.
# NOTE: the child controls are added to the view port (scrollFrame.viewPort, NOT scrollframe itself)
for row in range(100):
a = row
tk.Label(self.scrollFrame.viewPort, text="%s" % row, width=3, borderwidth="1",
relief="solid").grid(row=row, column=0)
t="this is the second column for row %s" %row
tk.Button(self.scrollFrame.viewPort, text=t, command=lambda x=a: self.printMsg("Hello " + str(x))).grid(row=row, column=1)
# when packing the scrollframe, we pack scrollFrame itself (NOT the viewPort)
self.scrollFrame.pack(side="top", fill="both", expand=True)
def printMsg(self, msg):
print(msg)
if __name__ == "__main__":
root=tk.Tk()
main_frame = tk.Frame(root)
nb = ttk.Notebook(main_frame)
frame = Example(nb)
#frame = tk.Frame(main_frame)
frame.pack()
nb.add(frame,text = "frame1")
frame1 = Example(nb)
frame1.pack()
nb.add(frame1,text = "frame2")
nb.pack(side = "left")
main_frame.pack()
root.mainloop()
问题是我只能用鼠标滚轮滚动笔记本中最后添加的标签。对于单个选项卡,可滚动框架按预期工作。但是当添加多个标签时,我只能通过鼠标滚轮滚动最后添加的标签。
【问题讨论】:
-
当我运行你的代码时,滚动在两个选项卡中都有效,但是当鼠标在滚动条内时
-
是的,只有当鼠标在滚动条内时它才有效。我希望它在鼠标在画布上时工作。
标签: python-3.x tkinter tkinter-canvas