Scrollbar not functioning properly Tkinter
Question: I am a newbie to Python.

I am trying to create a Scrollbar, which I did but the scrollbar is not
functioning properly.

The frame already shows all labels but what I expected was the frame will only
show up to 5 labels

and if frame contains more than 5 labels, then the scrollbar will function to
show the unseen remaining labels.

Any help?

Code:

    
    
    from Tkinter import *
    
    class GUI():
        def __init__(self):
            root=Tk()
            frame=Frame(root,width=300,height=300)
            frame.grid(row=0,column=0)
            self.canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
            Label(self.canvas,text="1").pack()
            Label(self.canvas,text="2").pack()
            Label(self.canvas,text="3").pack()
            Label(self.canvas,text="4").pack()
            Label(self.canvas,text="5").pack()
            Label(self.canvas,text="6").pack()
            Label(self.canvas,text="1").pack()
            Label(self.canvas,text="2").pack()
            Label(self.canvas,text="3").pack()
            Label(self.canvas,text="4").pack()
            Label(self.canvas,text="5").pack()
            Label(self.canvas,text="6").pack()
            hbar=Scrollbar(frame,orient=HORIZONTAL)
            hbar.pack(side=BOTTOM,fill=X)
            hbar.config(command=self.canvas.xview)
            vbar=Scrollbar(frame,orient=VERTICAL)
            vbar.pack(side=RIGHT,fill=Y)
    
            self.canvas.bind('<Configure>',self._On_Configure())
    
            vbar.config(command=self.canvas.yview)
            self.canvas.config(width=300,height=300)
            self.canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
            self.canvas.pack(side=LEFT,expand=True,fill=BOTH)
            root.mainloop()
        def _On_Configure(self):
            self.canvas.configure(scrollregion=self.canvas.bbox('all'))
    
    GUI = GUI()
    


Answer: I will not implement it for you. Here are some methods which you might be
interested in:

    
    
    from Tkinter import *
    
    class GUI():
        def __init__(self):
        root=Tk()
        frame=Frame(root,width=300,height=300)
        frame.grid(row=0,column=0)
        self.canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
        self.canvas_frame = Frame(self.canvas)
        self.canvas.create_window(0,0, anchor = NW, window = self.canvas_frame)
        self.labels = []
        for i in range(10):
            label = Label(self.canvas_frame,text=str(i))
            label.pack()
            self.labels.append(label)
        hbar=Scrollbar(frame,orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=self.canvas.xview)
        vbar=Scrollbar(frame,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)
    
        self.canvas.bind('<Configure>',self._On_Configure) # MISTAKE! do not use () here
    
        vbar.config(command=self.canvas.yview)
        self.canvas.config(width=300,height=300)
        self.canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        self.canvas.pack(side=LEFT,expand=True,fill=BOTH)
        root.mainloop()
        def _On_Configure(self, event = None):
            print self.canvas.bbox(self.canvas_frame)
            for label in self.labels:
                print label.winfo_pointerxy(), label.winfo_rootx(), label.winfo_rooty(), label.winfo_height(), label.winfo_reqheight()
            print "canvas:", self.canvas.winfo_height(), self.canvas.winfo_pointerxy()
            self.canvas.configure(scrollregion=self.canvas.bbox(self.canvas_frame))
    
    GUI = GUI()
    

The output shows the position of the labels and other pixel stuff.

    
    
    None
    (664, 607) 0 0 21 21
    (664, 607) 0 21 21 21
    (664, 607) 0 42 21 21
    (664, 607) 0 63 21 21
    (664, 607) 0 84 21 21
    (664, 607) 0 105 21 21
    (664, 607) 0 126 21 21
    (664, 607) 0 147 21 21
    (664, 607) 0 168 21 21
    (664, 607) 0 189 21 21
    canvas: 304 (664, 607)
    

With the knowledge about how big the labels are or how big the frame is you
can use store the `vbar` in `self.vbar` and do `self.vbar.pack_forget()` or
`vbar.pack()` whenever you need it or not in the function `_On_Configure`.

When you have your code ready for a scrolled frame then please do not hesitate
to post it. I remember other people also lloking for a frame with scrollbars.

