简单的应用程序创建Python tkin

2024-04-23 20:46:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试创建一个简单的应用程序。思路如下: 创建一个应用程序,它将运行只与所选课程相关联的脚本文件(单选按钮)。所以,我创建单选按钮列出主题(点击)。一旦选择了主题,用户必须点击Enter按钮。这应该运行所选主题的所有.py文件(execute_script函数)。在

但是,当我运行我的代码时,我得到4个消息框,里面写着“None”。在它们上单击ok之后,我得到一个只有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()

Tags: namepyapp主题executenamesmaindef
1条回答
网友
1楼 · 发布于 2024-04-23 20:46:37

您的脚本存在一些问题:

1)您的execute_script()函数中的错误:for name in been_cliked

2)当您创建单选按钮时,实际上是在调用check()函数。这就是为什么当你运行程序时,你会看到窗口弹出。在

你需要改变这一点:

radio1 = Radiobutton(app, text=name, value=name, \
                     variable=relStatus, command=check(file_name, relStatus))

为此:

^{pr2}$

看看check怎么不再有括号了?实际上,调用函数的意义不是调用函数。当然,您将看到一个直接的问题是您不能再将参数传递给回调函数!这是一个更大的问题。这里有几个链接可以帮助您开始:

解决方案如下:

更改此项:

command=check(file_name, reStatus)

为此:

command = lambda: check(file_name, relStatus)

3)实际上你在任何地方都没有你的单选按钮。在您的for循环中创建单选按钮之后,添加如下内容:radio1.pack(side='top')

4)您对Click Here按钮的回调也有同样的问题。您需要更改命令以不调用函数,而只需引用它:command = execute_script

5)在execute_script()中,确保import subprocessing

6)您确定要在execute_script()函数中使用yield而不是{}?在

7)在所有函数中,您需要确保been_clicked是全局的。在

我想如果你解决了这些问题,你就会更接近你想要的东西。祝你好运。!在

相关问题 更多 >