2

I'm trying to make 100 Entry widgets. It should be scrolled using a vertical scroll bar. since frame doesn't have scroll bar option, I used canvas. But when I add Entry widgets scroll bar not working. Can someone help me out what mistake I made.

MY CODE:

from tkinter import *
root=Tk()
frame=Frame(root,width=300,height=300)
frame.grid(row=0,column=0)
canvas=Canvas(frame,width=300,height=300,scrollregion=(0,0,500,500))

for _ in range(100):
    Entry(canvas).pack()

vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)

canvas.config(yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)

root.mainloop()
Sundararajan
  • 544
  • 2
  • 9
  • 25

1 Answers1

3

A Canvas can only scroll child widgets that were added to it via .create_window(). Child widgets that were added via .pack() are basically sitting on top of the Canvas, and are not controlled by it.

Note that .create_window() is roughly similar to the .place() geometry manager: the coordinates are entirely up to you, so you'd have to somehow know how far apart to space your Entry widgets, which would vary based on the exact font being used on each system.. It would probably work better to add a single Frame directly to the Canvas, then pack all your Entry widgets inside that - this gives you the benefits of automatic sizing, while still allowing scrolling.

jasonharper
  • 9,450
  • 2
  • 18
  • 42