0

I'm using TreeView with ListStore. my code goes like this:

class a:
    def __init__(self):
        .
        .
        types = int, str, str, int
        myTree, myStore = b.create_treeview(types)
        .
        .


class b:
    def create_treeview(types):
        #create gtk tree view object
        #return treeview and store (for the columns)
        store = gtk.ListStore(types[0], types[1], types[2], types[3])
        treeView = gtk.TreeView(store)
        treeView.set_rules_hint(True)
        return treeView, store

As you can see, class b doing some code work for class b. The variable types helping me send all types to the store. The problem is the way I'm using it at class b. Is there any why to make it more generic for re-use?

Thanks

Eli
  • 4,576
  • 1
  • 27
  • 39

1 Answers1

2

There's no need to define types in a, unless you use it again elsewhere:

myTree, myStore = b.create_treeview((int, str, str, int))

and you can make b more generic with:

store = gtk.ListStore(*types)

which will "unpack" the tuple of types into separate arguments to ListStore (see this question for further explanation).


Neater still might be:

def create_treeview(*types):
    store = gtk.ListStore(*types)
    ...

Now you can pass separate arguments to create_treeview:

myTree, myStore = b.create_treeview(int, str, str, int)

Or

myTree, myStore = b.create_treeview(*types)
Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437