adding horizontal and vertical scrollbar into a frame in Tkinter

807 Views Asked by At

I am trying to add a vertical scrollbar and a horizontal scrollbar into a frame. Here is my code:

rom tkinter import *
from tkinter import ttk 


root = Tk()
root.geometry('500x400')

main_frame = Frame(root)
main_frame.pack(fill=BOTH, expand=1)

canvas = Canvas(main_frame)
canvas.pack(side=LEFT, fill=BOTH, expand=1)

vscrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=canvas.yview)
vscrollbar.pack(side=RIGHT, fill=Y)
canvas.configure(yscrollcommand=vscrollbar.set)

hscrollbar = ttk.Scrollbar(main_frame, orient=HORIZONTAL, command=canvas.xview)
hscrollbar.pack(side=BOTTOM, fill=X)
canvas.configure(yscrollcommand=hscrollbar.set)

canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox('all')))

frame = Frame(canvas)

canvas.create_window((0,0), window=frame, anchor='nw')


for i in range(100):
   Button(frame, text=f'button {i}').grid(row=i, column=i)


root.mainloop()

Here is the result result It does not work. How could I add a vertical scrollbar and a horizontal scrollbar into a frame and is there any way to set the first location of the scroll. what i want

0

There are 0 best solutions below