2

I made a class that creates a scrollable frame

class ScrollableFrame(ttk.Frame):
    def __init__(self, container, *args, **kwargs):
        super().__init__(container, *args, **kwargs)
        canvas = tk.Canvas(self)
        scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
        self.scrollable_frame = ttk.Frame(canvas)

        self.scrollable_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(
                scrollregion=canvas.bbox("all")
            )
        )

        canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")

        canvas.configure(yscrollcommand=scrollbar.set)

        canvas.pack(side="left", fill="both", expand=True)
        scrollbar.pack(side="right", fill="y")

I now want to add a function move_to_end(self) that will scroll the canvas to end whenever called.

I tried using

    def move_to_last(self):
        canvas.yview_moveto(1)

But it doesn't help.

Actually, I am new to OOPs so facing problem that which is the widget I want to scroll

Satyajit
  • 115
  • 9
  • `canvas` is just a local variable of `__init__`. You need to make it a class attribute by changing it to `self.canvas` so you can ref it when calling class methods. – Henry Yik Nov 28 '20 at 07:55
  • Thanks @HenryYik , it helped me I just changed all ```canvas``` to to ```self.canvas``` and then the function worked fine – Satyajit Nov 28 '20 at 08:04
  • Go to this link, here is a implementation using ntk library, https://stackoverflow.com/questions/33400918/why-wont-the-python-tkinter-canvas-scroll/65610290#65610290 – Nj Nafir Jan 07 '21 at 10:12

0 Answers0