add/remove button in runtime
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) [part of code] class ADRButton(Button): def __init__(self, apObj, data, index, master=None, **d): txt = data[index]["jmeno"] Button.__init__(self, master,text=txt,command=lambda x=index: apObj.selectADR(x), **d) self.pack(fill=X,padx=5, pady=2) class App(Tk): def loadADR(self): self.datADR = (...) ### SORTED LIST, I'm going to use PICKLE for load/save ### def __init__(self, master=None): Tk.__init__(self, master) self.loadADR() self.selectedADR = -1 .... fADR=Frame(self,width=90,bg="#707070") fADR.pack_propagate(0) fADR.pack(side=LEFT,fill=Y) Frame(fADR,height=20,bg="#707070").pack(side=TOP,fill=X,fill=Y) self.ADRbuttons=[] for i in range(len(self.datADR)): self.ADRbuttons.append(ADRButton(self,self.datADR,i,fADR, activebackground="white", anchor='w', justify=LEFT)) .... -- View this message in context: http://old.nabble.com/add-remove-button-in-runtime-tp29254237p29254237.html Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
Hi, On Sat, 24 Jul 2010 03:41:21 -0700 (PDT) petroff <petr0ff@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)
This sounds like the grid() geometry manager is the perfect tool for you, is there any special reason why you want to use pack() instead? With grid() you could easily do something like: for i in range(len(self.datADR)): new = Button(self) new.grid(row=0, column=i) self.ADRbuttons.append(new) If you want to change the location of a button later, simply call grid() again with the new row and column. I hope this helps Michael
In article <29254237.post@talk.nabble.com>, petroff <petr0ff@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
The -before option in pack does in fact work, and would save time regenerating all the widgets. Mick On Mon, Jul 26, 2010 at 8:41 PM, Russell E. Owen <rowen@uw.edu> wrote:
In article <29254237.post@talk.nabble.com>, petroff <petr0ff@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
_______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
participants (4)
-
Michael Lange -
Michael O'Donnell -
petroff -
Russell E. Owen