[Tkinter-discuss] Create Buttons on the fly

John Jack itsmilesdavis at gmail.com
Mon Aug 31 17:40:07 CEST 2009

Hello all,
I have a long list of objects, which I print to a GUI.  I want to filter
this list, print the filtered list to the GUI, but maintain the ability to
look at the previous lists.

My program currently takes the list (L1), prints it to a Listbox, and adds a
button (B1) for the list.  Next, I filter the list, print the new filtered
list (L2 subset of L1) to the same Listbox (removing the elements of L1 not
in L2), and add an additional button for the new list. And so on...

How do I find a way to associate the old list L1 to the command for the
button (B1)?  Note, I do not want to create a separate command for each
button because I want to run an unlimited number of filters.  I need to have
B1 linked to L1, B2 linked to L2, B3 linked to L3, etc.

Here is some relevant code (n.b., the whole project is much larger, so this
is merely a snippet).

#n.b., L from the description above is all_entities.  So, L1 is
all_entities[1], etc.
#filter_group is an index tracking the current group to be filtered.

    #Filter the list
    def search_entities(self):
        tmp1 = askstring('Search Entity','Entity Type',initialvalue='gene')
        filtered = []
        #Create sublist
        for ent in self.backend.all_entities[self.filter_group]:
            if ent.etype.endswith("findthisstring"):

        #Store sublist
        self.filter_group += 1

    def add_filter_button(self):
        tmp = 'B%s' % self.filter_group
        filt = Button(self,text = tmp,command=self.fill_listbox(self))

    def fill_listbox(self):
       #clear listbox
       if self.ent_listbox.size() > 0:
        #Restore list box
        for ent in self.backend.all_entities[self.filter_group]:

The way my code exists now, I would need to modify the variable filter_group
when I click a button.  How can I accomplish this?  How do I tell
filter_group to be 1 if I click button 1 and 2 if I click button 2?

Sorry if this post is long.  Let me know if you need more information.

Python v2.6 (Linux)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20090831/8dbcb435/attachment.htm>

More information about the Tkinter-discuss mailing list