我目前正在开发一个扫描文件夹的程序,查找所有.exe文件并将它们放入准备运行的列表中。
到目前为止,我已经设法获取列表,创建按钮,但不是然后运行程序本身。相反,它在打开运行python代码时运行程序。
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()
非常感谢任何帮助。
答案 0 :(得分:0)
command
需要函数名称 - 它表示没有()
和参数。
您可以使用lambda
执行此操作
command=lambda:runit(t)
但您是在for
循环中创建的,因此t
可能会出现问题。
lambda
使用对t
的引用,而不是从t
复制的值 - 因此在循环中创建的所有lambda
将使用相同的值 - 分配给{{1的最后一个值}}。
但您可以将t
中的值复制到t
lambda
-
command=lambda x=t:runit(x)
表示“运行函数command=runit(t)
,结果分配给runit(t)
”。有时它可能很有用。
-
BTW:在Tkinter中command
和bind()
也需要功能名称。