[Tkinter-discuss] add/remove button in runtime

Russell E. Owen rowen at uw.edu
Mon Jul 26 20:41:55 CEST 2010


In article <29254237.post at talk.nabble.com>, petroff <petr0ff at seznam.cz> 
wrote:

> I want to allow changing in (sorted) list of buttons:
> self.ADRbuttons=[B1,B2...]
> REMOVE => button.destroy (it works correctly)
> ADD is a problem; I don't know how to insert button on correct location in
> button panel (pack method)
>...

You could try the "before" option to the pack method. I've not used it 
myself and I'm not sure it'll work.

Personally I would lay out all the buttons every time you remove or 
insert one. If you use the grid manager (which I usually use because it 
is more flexible than pack):
- keep the buttons in a collection, e.g.: a list of
   (button_name, button) so you can sort it by calling the sort method
- to add one, ungrid all in the current collection with grid_remove or 
grid_forget (the latter is fine in this case since you're not going to 
regrid it in the same place)
- add the new button to the collection
- grid all buttons in the collection

-- Russell



More information about the Tkinter-discuss mailing list