views:

68

answers:

1

I'm making a small script in python with ttk and I have a problem where a function runs where it shouldn't. The button code looks as follows:

btReload = ttk.Button(treeBottomUI, text="Reload", width=17, command=loadModelTree(treeModel))
btReload.pack(side="left")

and the function is as this:

def loadModelTree(tree):
    print ("Loading models...")
    allModels = os.listdir(confModPath)
    for chunk in allModels:
        ...

For some reason, the function runs without the button being pressed. Why?

+1  A: 

Well, as I found the answer, I'll answer my own question. It appers that ttk.button commands does not support sending arguments to functions so the work around is to do as follows:

btReload = ttk.Button(treeBottomUI, text="Reload", width=17, command=lambda i=treeModel: loadModelTree(i))
btReload.pack(side="left")

Simple as pie!

Markus Kothe