[Tkinter-discuss] Force Frame to resize after removing all widgets.

Carlos Zuniga carlos.zun at gmail.com
Wed Nov 13 13:54:22 CET 2013


Hi,

I have a problem with frames. If I remove a widget, it resizes to fit
the remaining widgets, but if I remove all of them then the frame
doesn't resize at all (in this case, the window is not resizable by
the user).

Here's a small example showing this:

import Tkinter
import ttk

root = Tkinter.Tk()
root.resizable(False, False)
frame = ttk.Frame(root)
frame.grid()
#sep = ttk.Separator(frame)
#sep.grid()

labels = []

for i in range(5):
    l = ttk.Label(frame, text='label %d' % i)
    labels.append(l)

add_all(labels)

buttons = ttk.Frame(root)
buttons.grid()

def remove_all(labels):
    for i in labels: i.grid_forget()

def add_all(labels):
    for i in labels: i.grid()

b1 = ttk.Button(buttons, text='Remove All', command=lambda:remove_all(labels))
b2 = ttk.Button(buttons, text='Remove 4',
command=lambda:remove_all(labels[:4]))
b3 = ttk.Button(buttons, text='Add All',    command=lambda:add_all(labels))
b1.grid()
b2.grid()
b3.grid()

root.mainloop()


I have a workaround by adding a Separator to the frame (uncommenting
the #sep lines above), then it will always have one widget and resize
every time. But I wonder if there a way to force it to resize instead?


Thanks
-- 
Carlos Z.


More information about the Tkinter-discuss mailing list