I used a scrollbar for my tkinter app with Canvas. I am using Pack to set up the scrollbars. But I have a problem. The horizontal scrollbar works fine, but the vertical scrollbar does not. The location is misconfigured.
My code:
self.scroll_x = tk.Scrollbar(self, orient=tk.HORIZONTAL)
self.scroll_y = tk.Scrollbar(self, orient=tk.VERTICAL)
self.canvas = tk.Canvas(self, width=600, height=100,
xscrollcommand=self.scroll_x.set,
yscrollcommand=self.scroll_y.set)
self.scroll_x.config(command=self.canvas.xview)
self.scroll_y.config(command=self.canvas.yview)
self.frame = tk.Frame(self.canvas)
self.canvas.create_window((0, 0), window=self.frame,anchor=tk.NW)
self.canvas.bind_all("<MouseWheel>", self.on_mousewheel)
self.canvas.config(scrollregion=self.canvas.bbox(tk.ALL))
self.canvas.pack(side=tk.LEFT,expand=True,fill=tk.BOTH)
self.scroll_x.pack(side=tk.BOTTOM,fill=tk.X)
self.scroll_y.pack(side=tk.RIGHT,fill=tk.Y)
self.bind("<Configure>", self.resize)
self.update_idletasks()
def on_mousewheel(self, event):
shift = (event.state & 0x1) != 0
scroll = -1 if event.delta > 0 else 1
if shift:
self.canvas.xview_scroll(scroll, "units")
else:
self.canvas.yview_scroll(scroll, "units")
def resize(self, event):
region = self.canvas.bbox(tk.ALL)
self.canvas.configure(scrollregion=region)