Python Forum
[Tkinter] Scrollbar in tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Scrollbar in tkinter (/thread-28611.html)



Scrollbar in tkinter - PatrickNoir - Jul-26-2020

I don't know why this scrollbar not working. Please help Sad

canvas = Canvas(frame, bd=0,
                xscrollcommand=xscrollbar.set,
                yscrollcommand=yscrollbar.set)

canvas.grid(row=0, column=0, sticky=N+S+E+W)

xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)

frame.pack()
entry1 = Entry(canvas,bg='blue',fg = 'white',width=70)

entry2 = Entry(canvas,bg='red',fg = 'white',width=70)
entry1.pack()
entry2.pack()


def pressed(ro):
    global button1
    y=e.get()
    if type(y)==str and y!="":
        entry1 = Entry(canvas,bg='blue',width=70)
        entry2 = Entry(canvas,bg='red',width=70)
        entry1.pack()
        entry2.pack()
        
         
e=Entry(root,width=124)
e.pack()

button1 = Button(root,text= "Submit",command =lambda: pressed(0))
button1.pack()



RE: Scrollbar in tkinter - Larz60+ - Jul-26-2020

Please provide enough of script to run.
scrollbar should go something like, but cannot test as your code is not runnable without modification.
canvas = Canvas(frame,bd=0)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
cheight = canvas.winfo_height()

yscrollbar = tk.Scrollbar(canvas, orient=tk.VERTICAL, command=canvas.yview)
yscrollbar.grid(row=0, rowspan=self.treeheight, column=1, sticky='ns')
xscrollbar = tk.Scrollbar(canvas, orient=tk.HORIZONTAL, command=canvas.xview)
xscrollbar.grid(row=self.cheight + 1, column=0, sticky='ew')
canvas.congigure(yscroll=yscrollbar)
canvas.congigure(xscroll=xscrollbar)

frame.pack()
entry1 = Entry(canvas,bg='blue',fg = 'white',width=70)
 
entry2 = Entry(canvas,bg='red',fg = 'white',width=70)
entry1.pack()
entry2.pack()
 
 
def pressed(ro):
    global button1
    y=e.get()
    if type(y)==str and y!="":
        entry1 = Entry(canvas,bg='blue',width=70)
        entry2 = Entry(canvas,bg='red',width=70)
        entry1.pack()
        entry2.pack()
         
          
e=Entry(root,width=124)
e.pack()
 
button1 = Button(root,text= "Submit",command =lambda: pressed(0))
button1.pack()



RE: Scrollbar in tkinter - deanhystad - Jul-26-2020

A really nice, concise description of the problem that I grabbed from Bryan Oakley over on stackoverflow
Quote:You can only associate scrollbars with a few widgets, and the root widget and Frame aren't part of that group of widgets.

The most common solution is to create a canvas widget and associate the scrollbars with that widget. Then, into that canvas embed the frame that contains your label widgets. Determine the width/height of the frame and feed that into the canvas scrollregion option so that the scrollregion exactly matches the size of the frame.

Why put the widgets in a frame rather than directly in the canvas? A scrollbar attached to a canvas can only scroll items created with one of the create_ methods. You cannot scroll items added to a canvas with pack, place, or grid. By using a frame, you can use those methods inside the frame, and then call create_window once for the frame.

The rest is here
https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter/3092341#3092341

What is boils down to is you create a canvas because that is one of the few things you can scroll. Next you create all your controls in a frame. Finally you place the frame in the canvas using anvas.create_window.