【问题标题】:How to run a python file with the click of a button in tkinter?如何通过单击 tkinter 中的按钮来运行 python 文件?
【发布时间】:2020-12-07 19:35:26
【问题描述】:

我正在为一个游戏制作一个菜单屏幕,并希望在您单击按钮时使用 python 文件打开主游戏屏幕。

这是按钮代码:

Play = Button(root, text="PLay(Bot)", fg="blue", bg="cyan", command=open)

游戏文件路径为(C:\Users\PCCF(HOFF)\PycharmProjects\RPS pythonGUI\GAME.py)

【问题讨论】:

    标签: python file tkinter tk


    【解决方案1】:

    试试这个

    import subprocess
    Play = Button(t, text="PLay(Bot)", fg="blue", bg="cyan", 
                  command=lambda:subprocess.Popen(args=['python', r'C:\Users\PCCF(HOFF)\PycharmProjects\RPS pythonGUI\GAME.py'])))
    

    【讨论】:

    • 如果我手动关闭打开的窗口然后再次单击按钮,这仍然有效吗?
    • 是的,当你点击按钮时,它会运行游戏很多次。
    猜你喜欢
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 2020-10-03
    • 2016-07-15
    • 2015-04-19
    相关资源
    最近更新 更多