0

I'm programming something in python and I need to create a scrollbar that updates when new widgets are added to the window. I don't know how to do this and I haven't found the answer in any place. Is there a command for this? This is my code right now.

import tkinter as tk root = tk.Tk() root.geometry("600x400") my_canvas = tk.Canvas(root) my_canvas.pack(side = "left", fill = "both", expand = 1) my_scrollbar = tk.Scrollbar(root, orient = "vertical", command = my_canvas.yview) my_scrollbar.pack(side = "right", fill = "y") my_canvas.configure(yscrollcommand = my_scrollbar.set) my_canvas.bind("<Configure>", lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all"))) my_frame = tk.LabelFrame(my_canvas, width = my_canvas.winfo_width()) my_frame.grid_propagate(0) my_frame.pack() for i in range(10): my_label = tk.Label(my_frame, text = "Label") my_label.pack() my_canvas.create_window((0, 0), width = 600, anchor = "nw", window = my_frame) def create_label(scroll, canv, fram): my_label = tk.Label(fram, text="Label") my_label.pack() my_button = tk.Button(my_frame, text = "Button", command = lambda: create_label(my_scrollbar, my_canvas, my_frame)) my_button.pack() root.mainloop() 

1 Answer 1

1

Bind to the <Configure> event of the inner frame. That event fires whenever the frame changes size.

def adjust_scrollregion(event): my_canvas.configure(scrollregion=my_canvas.bbox("all")) my_frame.bind("<Configure>", adjust_scrollregion) 

Unrelated to the question that was asked... calling my_frame.grid_propagate(0) is pointless since you use pack inside of my_frame.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.