0

I'm currently trying to implement a scrollbar on a canvas, since I learned that I can't do it instantly on a frame. I can make it appear, but I cannot actually make it work. I'm still a beginner when it comes to python and tkinter and the previous posts in this matter haven't helped me that much. Here's my code(I'm open to advice on anything else I've done that's considered bad practice as well):

from tkinter import *

class myApp():
    def __init__(self,root):
        myApp.f2=Frame(root)
        myApp.f2.pack()
        myApp.canv=Canvas(self.f2)
        myApp.canv.pack()
        myApp.f1=Frame(self.canv)
        myApp.f1.pack(side=LEFT, fill=BOTH, expand=TRUE)
        myApp.scroll=Scrollbar(self.f1,orient=VERTICAL,
        command=myApp.canv.yview)
        myApp.scroll.grid(row=0,column=6)
        myApp.canv.config(yscrollcommand=myApp.scroll.set)

I have to use grid for the rest of the widgets, that I haven't included here.

Jonnyolsen1
  • 31
  • 1
  • 5
  • You're trying to make the scrollbar a grandchild of the canvas (by way of frame `f1`), which is bizarre - normally a scrollbar and its scrolled widget are siblings. Also, you normally do not add children to a canvas via `.grid()` or `.pack()` - you have to use `.add_window()` to create children that will actually scroll. – jasonharper Jul 19 '17 at 15:57

1 Answers1

0

i don't really understand how the bind works, but here's the code i use for a scrollbar in a Toplevel, it's not from me but i don't remember where i found it (i think it's on stackoverflow, you should search more, i'm sure you will find something). it should work but you can scroll the bar only if you hover it with the mouse though

    Toplevel = tk.Toplevel(self)

    #create canvas to make a scrollbar
    canvas = tk.Canvas(Toplevel, borderwidth=0)
    #create frame which will contains your widgets
    frame = tk.Frame(canvas)

    #create and pack your vsb to the Toplevel and link it to the canvas yview
    vsb = tk.Scrollbar(Toplevel, orient="vertical", command=canvas.yview)
    canvas.configure(yscrollcommand=vsb.set)
    vsb.pack(side="right", fill="y")
    canvas.pack(side="left", fill="both", expand=True)
    canvas.create_window((5,5), window=frame, anchor="nw")


    #i don't understand this line
    frame.bind("<Configure>", lambda event, canvas=canvas: canvas.configure(scrollregion=canvas.bbox("all")))

    #add your widgets to the frame
    ...

PS : Don't use from tkinter import * you can(and will) have name collisions, use import tkinter or import tkinter as tk

Edit : this question is my source.