0

i have made a scrollbar and it works perfect but the size of the scrollbar doesn't match the size of the canvas. i tryed removing the scrollbar.place but it puts the scrollbar in the upper left corner and reallt short. my question is how do i change the length of the scrollbar. thank you very much.

the code:

canvas = tkinter.Canvas(self.main_window, width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
canvas.grid()
canvas.place(relx=0.5, rely=0.2, anchor=tkinter.N)
scrollbar = tkinter.Scrollbar(self.main_window, orient=tkinter.VERTICAL, command=canvas.yview)
scrollbar.grid(row=3, column=1, sticky=tkinter.N + tkinter.S + tkinter.E)
scrollbar.place(relx=1, rely=0.5, anchor=tkinter.E)
canvas.configure(yscrollcommand=scrollbar.set, scrollregion=canvas.bbox("all"))
frame = tkinter.Frame(canvas)
canvas.create_window((0,0), window=frame, anchor=tkinter.NW)
frame.bind("<Configure>", lambda event: canvas.configure(scrollregion=canvas.bbox("all")))

1 Answers1

0

After making the following changes:

  • remove canvas.place(...) and scrollbar.place(...)
  • put canvas at the same row (3) of scrollbar
  • set the width and height of frame

Then the height of scrollbar will be the same of canvas.

canvas = tkinter.Canvas(self.main_window, width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
canvas.grid(row=3, column=0, sticky='nsew') # put in same row of scrollbar
#canvas.place(relx=0.5, rely=0.2, anchor=tkinter.N)
scrollbar = tkinter.Scrollbar(self.main_window, orient=tkinter.VERTICAL, command=canvas.yview)
scrollbar.grid(row=3, column=1, sticky=tkinter.N + tkinter.S + tkinter.E)
#scrollbar.place(relx=1, rely=0.5, anchor=tkinter.E)
canvas.configure(yscrollcommand=scrollbar.set)
# give frame a size longer then the canvas to enable scrolling 
frame = tkinter.Frame(canvas, width=CANVAS_WIDTH, height=CANVAS_HEIGHT*2)
canvas.create_window((0,0), window=frame, anchor=tkinter.NW)
frame.bind("<Configure>", lambda event: canvas.configure(scrollregion=canvas.bbox("all")))
acw1668
  • 40,144
  • 5
  • 22
  • 34