I have implemented my own scrollable frame class in tkinter:
class scrolledFrame(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self)
self.canvas.pack(fill = "both", expand = True, side = "left")
self.scroll = tk.Scrollbar(self, command = self.canvas.yview)
self.scroll.pack(side = "right", fill = "y")
self.canvas.config(yscrollcommand = self.scroll.set)
self.content = tk.Frame(self.canvas)
self.content.bind("<Configure>", self.resizeCanvas)
self.contentWindow = self.canvas.create_window((0,0), window = self.content, anchor = "nw")
self.content.bind("<Enter>", self.enableScrollCanvas)
self.content.bind("<Leave>", self.disableScrollCanvas)
def scrollCanvas(self, event):
self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")
def enableScrollCanvas(self, event):
self.canvas.bind_all("<MouseWheel>", self.scrollCanvas)
def disableScrollCanvas(self, event):
self.canvas.unbind_all("<MouseWheel>")
def resizeCanvas(self, event):
self.update_idletasks()
self.canvas.config(scrollregion = self.canvas.bbox("all"))
self.canvas.itemconfig(self.contentWindow, width = self.canvas.winfo_width())
root = tk.Tk()
exampleFrame = scrolledFrame(root)
exampleFrame.pack()
exampleLabel = tk.Label(exampleFrame.content, text = "I'm in the scrolled frame!")
exampleLabel.pack()
root.mainloop()
This works fine, but the problem is to add widgets to the scrolled frame, the parent has to be exampleFrame.content
. I have looked at several other examples which all have the same limitation. Is it possible to configure the class so exampleFrame
can be the parent of the widgets instead of exampleFrame.content
? Thanks in advance.