我有只在滚动条区域滚动的框架。我正在尝试使其在窗口中的任何位置均可滚动,但无法用鼠标滚轮将其绑定。
import tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.canvas = tk.Canvas(root, borderwidth=0)
self.frame = tk.Frame(self.canvas)
self.vsb = tk.Scrollbar(root, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.canvas.create_window((0,0), window=self.frame, anchor="nw",
tags="self.frame")
self.vsb.pack(side="right", fill="y")
self.canvas.pack()
self.frame.bind("<Configure>", self.onFrameConfigure)
self.rectanglesize()
def rectanglesize(self):
for idx in range(20 * 2):
if idx % 2 == 0:
tk.Label(self.frame, text="Size of rectangle:").grid(row=idx, column=0)
tk.Entry(self.frame).grid(row=idx, column=1)
def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all"),width=250,height=500)
def onmousewheel(event):
self.canvas.yview_scroll(-1*(event.delta/120), "units")
if __name__ == "__main__":
root=tk.Tk()
Example(root).pack()
root.mainloop()