class Lnput_lnitialization(): def __init__(self, top, Number_of_rows): self.top = top int(data_demo.window_width // 3.9875) int(data_demo.window_height // 1.138) self.frame = tk.Frame(self.top, bg='Black', highlightthickness=2, highlightbackground='Black') self.frame.place(relx=0.739, rely=0.083, width=int(data_demo.window_width // 3.87), height=int(data_demo.window_height // 1.15)) self.canvas = tk.Canvas(self.frame) self.canvas.place(relx=0, rely=0, width=int(data_demo.window_width // 3.9875), height=int(data_demo.window_height // 1.15)) scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview) scrollbar.pack(side="right", fill="y") self.canvas.configure(yscrollcommand=scrollbar.set) self.button_frame = tk.Frame(self.canvas, bg='Yellow') self.button_frame.pack_propagate(False) # 禁止自动调整大小 self.button_frame.pack(fill=BOTH, expand=True) # 使其自适应大小 self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw") self.button_frame.columnconfigure(0, weight=1) self.button_frame.columnconfigure(1, weight=1) self.button_frame.columnconfigure(2, weight=1) self.button_frame.columnconfigure(3, weight=1) self.button_frame.columnconfigure(4, weight=1) self.button_frame.columnconfigure(5, weight=1) self.Content(Number_of_rows)为什么用不了滚轮了
时间: 2024-03-18 13:42:44 浏览: 123
这段代码中使用了tkinter的Canvas组件来创建一个可滚动的窗口,但是在代码中并没有对Canvas绑定滚轮事件的处理程序。因此,当鼠标滚轮事件发生时,Canvas并没有响应。解决方法是在代码中使用canvas.bind()方法绑定滚轮事件,并在绑定的处理程序中调用Canvas的yview()方法来实现滚动。具体实现可以参考下面的示例代码:
```python
class Lnput_lnitialization():
def __init__(self, top, Number_of_rows):
self.top = top
int(data_demo.window_width // 3.9875)
int(data_demo.window_height // 1.138)
self.frame = tk.Frame(self.top, bg='Black', highlightthickness=2, highlightbackground='Black')
self.frame.place(relx=0.739, rely=0.083, width=int(data_demo.window_width // 3.87), height=int(data_demo.window_height // 1.15))
self.canvas = tk.Canvas(self.frame)
self.canvas.place(relx=0, rely=0, width=int(data_demo.window_width // 3.9875), height=int(data_demo.window_height // 1.15))
scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview)
scrollbar.pack(side="right", fill="y")
self.canvas.configure(yscrollcommand=scrollbar.set)
self.button_frame = tk.Frame(self.canvas, bg='Yellow')
self.button_frame.pack_propagate(False) # 禁止自动调整大小
self.button_frame.pack(fill=BOTH, expand=True) # 使其自适应大小
self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw")
self.button_frame.columnconfigure(0, weight=1)
self.button_frame.columnconfigure(1, weight=1)
self.button_frame.columnconfigure(2, weight=1)
self.button_frame.columnconfigure(3, weight=1)
self.button_frame.columnconfigure(4, weight=1)
self.button_frame.columnconfigure(5, weight=1)
self.Content(Number_of_rows)
self.canvas.bind("<MouseWheel>", self.on_mousewheel)
def on_mousewheel(self, event):
self.canvas.yview_scroll(-1 * int(event.delta / 120), "units")
```
阅读全文