Tkinter Scrollbar for Frame
Using a Scrollable Frame
The tkinter package does not provide a built-in vertical scrollbar for a frame. However, you can create one using a combination of Canvas, Scrollbar, and Frame widgets. Here's an example:
class VerticalScrolledFrame(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) # Create a canvas canvas = tk.Canvas(self) canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # Create a vertical scrollbar scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Connect the scrollbar to the canvas scrollbar.config(command=canvas.yview) canvas.config(yscrollcommand=scrollbar.set) # Reset the view canvas.xview_moveto(0) canvas.yview_moveto(0) # Create a frame inside the canvas self.interior = ttk.Frame(canvas) canvas.create_window(0, 0, window=self.interior, anchor=NW)
Binding Scrollbar to Mouse Wheel
By default, Tkinter scrollbars lack mouse wheel scrolling functionality. To enable it, we can bind the mouse wheel event to the yview_scroll method of the canvas:
canvas.bind_all('<MouseWheel>', lambda event: canvas.yview_scroll(int(-event.delta/120), "units"))
Differences from OP's Code
Other Points
The above is the detailed content of How to Create a Scrollable Frame with a Vertical Scrollbar in Tkinter?. For more information, please follow other related articles on the PHP Chinese website!