0

Can anybody please tell me what I am doing wrong? The scrollbar seems not to reflect the area of the widget it is bound to.

Many thanks.

from tkinter import *
import tkinter as tk

#panel1
root = tk.Tk()
frame1 = tk.Frame(master=root, width=900, height=800)
canvas = tk.Canvas(frame1, width=900, height= 900)
vsb = tk.Scrollbar(frame1, orient=VERTICAL)
canvas.configure(yscrollcommand=vsb.set, 
scrollregion=canvas.bbox("all"))
vsb.configure(command=canvas.yview)
canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
vsb.pack(fill=Y, side=RIGHT, expand=FALSE)
# notebook.add(frame1, text="1")
frame1.pack(expand=True, fill=BOTH)
root.mainloop()
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
Chal.lo
  • 41
  • 1
  • 1
    Tried this with Python 3.6.5 on Windows10 and the layoyt of the widgets looks fine. The scrolling doesn't work though. Have a look at [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) where you can find some good example code. – figbeam Apr 30 '19 at 17:28
  • 1
    There's nothing in the canvas, and you've set the scrollregion to `None`, so there's nothing for the scrollbars to scroll. – Bryan Oakley Apr 30 '19 at 17:38

1 Answers1

0

If canvas is empty then there is nothing to scroll. And scrollbar reflects it correctly.

You have to add something to Canvas to scroll it.

I put two Frames bigger than window's size and now you have somthing to scroll.

You have to use scrollregion= after you put items in canvas. Or you can use after() to use scrollregion= after tkinter shows window.

import tkinter as tk

#def resize():
#    canvas.configure(scrollregion=canvas.bbox("all"))

root = tk.Tk()

frame1 = tk.Frame(root, width=900, height=800)
frame1.pack(expand=True, fill='both')

canvas = tk.Canvas(frame1, width=900, height= 900)
canvas.pack(side='left', fill='both', expand=True)

vsb = tk.Scrollbar(frame1, orient='vertical')
vsb.pack(fill='y', side='right', expand=False)
vsb.configure(command=canvas.yview)

item_1 = tk.Frame(canvas, bg='red', width=500, height=500)
canvas.create_window(0, 0, window=item_1, anchor='nw')

item_2 = tk.Frame(canvas, bg='green', width=500, height=500)
canvas.create_window(500, 500, window=item_2, anchor='nw')

canvas.configure(yscrollcommand=vsb.set, scrollregion=canvas.bbox("all"))

#root.after(100, resize)

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148