0

""" this class will allow you to make a scrollable frame to pack or grid use self.outer_frame you can make the outer frame packed in master in the init method by settingouter_pack = True i have seen alot of codes related to this but for some reasons it does not work for me the class i made will work with the mouse wheal and place the scroll bar is there if you want to show it then pack it """

class ScrollableFrame(Frame):

    def __init__(self, master, outer_pack: bool=False, *args, **kwargs):

        #create the main frame
        self.outer_frame = Frame(master, width=50)
        if outer_pack:
            self.outer_frame.pack()
        #create the canvas

        self.canvas = Canvas(self.outer_frame, bg="white")
        super(ScrollableFrame, self).__init__(master=self.canvas, *args, **kwargs)
        self.canvas.pack(expand=0)
        #add a scroll bar to the canvas
        self.scroll_bar = ttk.Scrollbar(self.outer_frame, orient=VERTICAL, command=self.canvas.yview)
        #configure the canvas
        self.canvas.configure(yscrollcommand=self.scroll_bar.set)
        self.canvas.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
        self.canvas.bind_all("<MouseWheel>", lambda e: self.__on_mousewheel(e))
        #put self in a window in the canvas

        self.pack(fill=X, expand=1, side=RIGHT)
        self.canvas.create_window((0, 0), window=self, anchor="e")

    def __on_mousewheel(self, event):
        self.canvas.yview_scroll(-1 * (event.delta // 120), "units")
  • the mouse wheal binding from [link]https://stackoverflow.com/questions/17355902/tkinter-binding-mousewheel-to-scrollbar – Mohamed Ahmed Jun 01 '22 at 08:40

0 Answers0