Question

I am new to Python and Tkinter.

I am practicing on the use of Frames, Canvas, and Scrollbars and I got a problem:

I have a Canvas that acts as my Master and a Frame that is on the specific coordinate of Canvas.

I will put Buttons and labels in the frame so I chose not to use listbox.

Now, how do I make the scrollbar on the frame in such that if the label in the frame exceeds more than 5 (Each label are put in vertical way), then ScrollBar will appear?

Here is the sample Code:

from Tkinter import *

class GUI():
    def __init__(self):
        self.namelist = ["Mark","Anna","Jason","Lenna","Leo","Zucharich","Robinson","AReallyLongNameThatMightExist"]
        self.canvas = Canvas(width=1200,height=700)
        self.canvas.pack(expand=YES,fill=BOTH)
    def Friends(self):        
        controlframe = Frame(self.canvas)
        controlframe.place(x=600,y=300)
        #Frame for showing names of friends
        for x in self.namelist:
            frame = Frame(controlframe)
            frame.pack()
            Name = Label(frame,text="%s "%x).pack(side=LEFT)
            chatButton = Button(frame,text="Chat").pack(side=LEFT)
            delButton = Button(frame,text="Delete").pack(side=LEFT)
            setcloseButton = Button(frame,text="Set Close").pack(side=LEFT)
            setgroupButton = Button(frame,text="Set Group").pack(side=LEFT)
        mainloop()

GUI = GUI()
GUI.Friends()
Was it helpful?

Solution

You can not scroll the Frame but you can scroll the canvas. Be sure to set the scroll area of the canvas to the frame area. Then you can add a scrollbar with pack() and hide it with pack_forget().

Maybe if you bind to the "configure" event of the frame you get to know when the size changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top