-1

I'm currently working on a program to scan a folder, find all the .exe files and place them in a list ready to run.

So far, I have managed to get the list, create the buttons, but not then run the program itself. Instead it is running the programs upon opening running the python code.

import os
import Tkinter

top = Tkinter.Tk()

def run(exe):
    os.system(exe)

exes = []
for root, dirs, files in os.walk(r'./'):
    for file in files:
        if file.endswith('.exe'):
            exes.append(file)

def dispgames(exes):
    exes = exes[:-4]
    return exes

def runit(game):
    os.system(game)

#print(exes)
#print(dispgames(exes))


def radioCreate(typeArray):
    for t in typeArray:
        b = Tkinter.Button(top, text = dispgames(t), command=runit(t))
        b.pack()

radioCreate(exes)

Tkinter.Button(top, text = "Display").pack()

top.mainloop()

Any help would be greatly appreciated.

Mat
  • 202,337
  • 40
  • 393
  • 406

1 Answers1

0

command expects function name - it means without () and arguments.

You can use lambda to do this

command=lambda:runit(t)

but you create it in for loop so there can be problem with t.

lambda use reference to t , not value copied from t - so all lambda created in loop will use the same value - last value assigned to t.

But you can copy value from t to other variable in lambda

command=lambda x=t:runit(x)

-

command=runit(t) means "run function runit(t) and result assign to command". Sometimes it can be usefull.

-

BTW: in Tkinter bind() and after() also expect function name.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you. this helped a little. I've tried this with 2 programs in the folder "test1.exe" and "test2.exe". when I click the button for test1.exe it's runing test2.exe, but the button for test2.exe works as it should. – scottdominic94 Jan 01 '16 at 16:02
  • if you use `lambda:runit(t)` in `for` loop then all lambda use the same value - because they use reference to the same place in memory which has last value assigned to `t` (`test2.exe`). if you use `lambda x=t:runit(x)` then `x=t` will copy value from `t` and every lambda has different value in `x`. – furas Jan 01 '16 at 18:33