我试图将一个名为checks的变量列表传递给函数installFunc,由于某些原因,它似乎不起作用,下面是我(认为是)的相关代码:
def installFunc(checks):
subprocess.call("md c:\MGInstall", shell=True)
subprocess.call (u"net use w: \\it01\files")
if checks[0] == 1:
subprocess.call(u"w:\\software\\snagitup.exe")
if checks[1] == 1:
subprocess.call(u"w:\\software\\camtasia.exe")
if checks[2] == 1:
urllib.urlretrieve(u"LONGURL", u"c:\\MGinstall\\gotomeeting.exe")
subprocess.call (u"c:\\MGinstall\\gotomeeting.exe")
if checks[3] == 1:
sixtyfourcheck()
if is64bit == True:
urllib.urlretrieve(u"LONGURL", u"c:\\MGinstall\\tortoiseSVN.exe")
elif is64bit == False:
urllib.urlretrieve(u"LONGURL", u"c:\\MGinstall\\tortoiseSVN.exe")
#urllib.urlretrieve(u"LONGURL", u"c:\\MGinstall\\MGinstall.exe")
#subprocess.call (u"c:\\MGinstall\\MGinstall.exe")
#subprocess.call (u"w:\\printers\\installer\\printer.exe")
app = Tk()
w = Label(app, text="IT Automatic Installer")
w.pack()
text = ["Snagit", "Camtasia", "GotoMeeting", "TortoiseSVN"]
variables = []
for name in text:
variables.append(IntVar())
Checkbutton(text=name, variable=variables[-1]).pack()
checks = [variable.get() for variable in variables]
b = Button(text="Install", command= lambda : installFunc(checks))
b.pack()
app.mainloop()
现在,我尝试了一些不同的方法-实际上是由stack overflow提供的lamba部分-我在理解它的工作原理时遇到了一些困难。你知道吗
但我遇到的最大问题是-为什么没有将检查传递给installFunc()?我想将检查的完整列表(输入的项目数)传递给installFunc()。你知道吗
variable.get()
返回IntVar
实例在被调用时的值,该值甚至在应用程序启动之前。所以,它将充满零。你知道吗此外,还需要将
variables
作为默认参数传递给lambda,这样就不会与全局和局部变量发生冲突。你知道吗你至少可以用几种方法来做。就我个人而言,我会将安装程序抽象为一个对象,正如Hugh Bothwell的评论所提到的那样。这具有最大的灵活性以及对状态的简洁包容。但是,如果您只使用一个函数,那么可以使用functools来“curry”函数:动态创建一个嵌入给定参数的新函数。以下是您需要做的更改。。。你知道吗
问题是变量.get()创建一个不可变的int,并且您的列表“checks”永远不会更改。你可能真正想要的是。。。你知道吗
根据我之前的评论,重构版本:
相关问题 更多 >
编程相关推荐