Home > Software engineering >  Why does my Tkinter Scrollbar's bar fill entire length of my window?
Why does my Tkinter Scrollbar's bar fill entire length of my window?

Time:03-06

I have working horizontal scrollbar, but for some reason it does not rezise the "visible" bar. It does scroll perfectly, but I even didin't notice there was a bar before I added style clam.

Numbers go up to "a99"

Here is my code:

from tkinter import *
from tkinter import ttk

root = Tk()

style = ttk.Style()
style.theme_use('clam')

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

my_canvas = Canvas(main_frame)
my_canvas.pack(side=TOP, fill=BOTH, expand=1)

my_scrollbar = ttk.Scrollbar(main_frame, orient=HORIZONTAL, command=my_canvas.xview)
my_scrollbar.pack(side=BOTTOM, fill=X)

my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))

seccond_frame = Frame(my_canvas)

my_canvas.create_window((0,0), window=seccond_frame, anchor="nw")

for i in range(100):
    Label(seccond_frame, text=f"a{i}").grid(row=0, column=i)

root.update()

root.mainloop()

CodePudding user response:

There are two issues in your code:

  1. my_canvas.configure(yscrollcommand=my_scrollbar.set) should be my_canvas.configure(xscrollcommand=my_scrollbar.set)

  2. when adding items to seccond_frame, it is seccond_frame get resized, not my_canvas. So you need to bind <Configure> on seccond_frame instead of my_canvas

  • Related