【问题标题】:Launch python program from another program从另一个程序启动 python 程序
【发布时间】:2018-05-17 02:19:42
【问题描述】:

我正在为我的计算机科学 A 级课程制作一个库存控制系统。我遇到的问题是我不知道在单击Button1 后如何让python 启动不同的python 程序。

from tkinter import *
top=Tk()

Button1= Button(top,text='UPDATE STOCK', width=40)
Button1.place(x=80, y=20)

mainloop()

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:
    import os
    from tkinter import*
    import subprocess
    
    def otherlaunch():
        subprocess.call(['python.exe', "filename.py"]) # filename.py is the file
    
    top=Tk()
    
    top.title('Stock Control')
    top.geometry('400x200')
    
    Button1= Button(top,text='UPDATE STOCK', width=40,command=otherlaunch)
    Button1.place(x=80, y=20)
    
    
    mainloop()
    

    【讨论】:

    • 感谢 Patrick 更新,我测试并复制了代码,但粘贴错误。下次我会更加小心:)
    【解决方案2】:
    from Tkinter import Tk, Button, mainloop
    import subprocess
    
    def ext_python_script(event):
        subprocess.call(["python2.7", "sss.py"])   
    
    if __name__ == '__main__':
        top = Tk()
    
        Button1 = Button(top, text='UPDATE STOCK', width=20, height=10)
        Button1.place(x=10, y=20)
        Button1.bind("<Button-1>", ext_python_script)
    
    mainloop()
    

    我们可以在这里使用绑定。

    【讨论】:

    • 请在您的答案中添加更多上下文。
    • 我的变种在这里
    【解决方案3】:

    您可以使用import 语句从另一个python 程序加载一个python 程序,并使用command 参数在按钮单击时执行某些操作,如下所示:

    import os 
    from tkinter import*
    top=Tk()
    
    top.title('Stock Control')
    top.geometry('400x200')
    
    def run_other():
        import filename  #notice that it is not a string, and there is no '.py'
    
    Button1= Button(top,text='UPDATE STOCK', width=40, command=run_other)
    Button1.place(x=80, y=20)
    
    
    mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多