-1

I am newbie in programming, don't hate me pls :)

Why scroll is not working on my canvas widget? I added loop with 30 rows and I cannot scroll down. Its look like it because of create_text() method or maybe not. I've written code for example below.

from tkinter import *

root = Tk()
root.geometry('200x150')
frame = Frame(root)

yscrollbar = Scrollbar(frame, orient=VERTICAL)
yscrollbar.pack(fill=Y, side=RIGHT)

canvas = Canvas(frame,
                yscrollcommand=yscrollbar.set,
                bg='white')
canvas.pack(fill=BOTH)
yscrollbar.config(command=canvas.yview)

n=12
for i in range(1,31):
    canvas.create_text(10,n,text=i)
    n+=12

frame.pack()
root.mainloop()

my code

  • Does this answer your question? [Adding a scrollbar to a group of widgets in Tkinter](https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter) – stovfl Dec 26 '19 at 10:23

1 Answers1

1

Scrolling is not responsive because you need to tell the canvas to limit the scrolling to a given area.

You can use the bbox method to get a bounding box for a given object, or a group of objects.

canvas.bbox(ALL) returns the bounding box for all objects on the canvas.

Link: http://effbot.org/zone/tkinter-scrollbar-patterns.htm you can check other methods to do this in this link

Here is the working code:

from tkinter import *

root = Tk()
root.geometry('200x150')
frame = Frame(root)

yscrollbar = Scrollbar(frame, orient=VERTICAL)
yscrollbar.pack(fill=Y, side=RIGHT)

canvas = Canvas(frame,
                yscrollcommand=yscrollbar.set,
                bg='white')
canvas.pack(fill=BOTH)
yscrollbar.config(command=canvas.yview)

n=12
for i in range(1,31):
    canvas.create_text(10,n,text=i)
    n+=12

frame.pack()

# Add this line to tell the canvas the area over to scroll
canvas.config(scrollregion=canvas.bbox(ALL))

root.mainloop()

Somraj Chowdhury
  • 983
  • 1
  • 6
  • 14