我试图创建一个简单的应用程序用于说明目的。想法如下: 创建一个应用程序,该应用程序将运行仅与所选课程关联的脚本文件(单选按钮)。所以,我创建了列出主题的单选按钮(点击)。一旦选择了主题,用户必须按下Enter
按钮。这应该运行所选主题的所有.py
文件(execute_script
函数)。简单的应用程序创建Python tkinter
但是,当我运行我的代码时,我得到4个带有'None'的messageboxes。点击确定之后,我会看到一个只有enter
按钮的方形窗户。我能做些什么来纠正这个问题?
def check(file_name, relStatus):
radioValue = relStatus.get()
tkMessageBox.showinfo('You checked', radioValue)
been_clicked.append(file_name)
return
def execute_script():
for name in been_cliked:
subprocess.Popen(['python', 'C:\Users\Max\Subjects\{}'.format(name)])
yield
def main():
#Create application
app = Tk()
app.title('Coursework')
app.geometry('450x300+200+200')
#Header
labelText = StringVar()
labelText.set('Select subjects')
#Dictionary with names
product_names = {}
names = []
file_name = []
names = ['Math', 'Science', 'English', 'French']
file_name = ['calc.py', 'physics.py', 'grammar.py', 'livre.py']
product_names = OrderedDict(zip(names, file_name))
#Create radio buttons
global been_clicked
been_clicked = []
relStatus = StringVar()
relStatus.set(None)
for name,file_name in product_names.iteritems():
radio1 = Radiobutton(app, text=name, value=name, \
variable=relStatus, command=check(file_name, relStatus))
button = Button(app, text='Click Here', width=20, command=execute_script())
button.pack(side='bottom', padx=15, pady=15)
app.mainloop()
if __name__ == '__main__': main()
由于',command = execute_script()'是_calling_'execute_script',奇怪的是'for name in been_cliked'问题不会导致'NameError:全局名''was_cliked'未定义'错误。 – martineau
我同意。原因是他有'yield'而不是'return'。使用'yield'不会导致'NameError'被引发。不知道这是为什么。 – Symmitchry
啊,答案在这里:http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained 'yield'会导致一个生成器被返回,但直到使用生成的代码,代码尚未运行。 – Symmitchry