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
.
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:
my_canvas.configure(yscrollcommand=my_scrollbar.set)
should bemy_canvas.configure(xscrollcommand=my_scrollbar.set)
when adding items to
seccond_frame
, it isseccond_frame
get resized, notmy_canvas
. So you need to bind<Configure>
onseccond_frame
instead ofmy_canvas