运行 来自另一个 tkinter 脚本的现有 tkinter 脚本

Running an existing tkinter script from another tkinter script

我是 python 和 tkinter 的新手。

我有一个工作的 tkinter 脚本(我想避免编辑) 现在我正在编写一个将成为顶级 GUI 的脚本。 这个脚本中的按钮应该使用一些命令行参数启动我现有的脚本(比如 运行 它来自 shell 即 python3.4.1 script.py args)。

我试过以下方法:

  1. 使用os.system

    btn2 = Button(frame2, text="Configure>>", command="os.system('python script.py args')")
    
  2. 使用子进程

    def runSubProcess(self):
        p=subprocess.Popen(["python3.4.1","script.py args"],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output=p.communicate()[0]
    

两种方法都不行。

欢迎提出任何建议,谢谢。

编辑:另外,我不需要与新的 window 进行通信,因为它将写入稍后将使用的文件。只需从子项到父项 window 的控件 return 就足够了(单击“确定”后)

-维奈

您的第一种方法应该有效。但是,您将错误的内容传递给了按钮的 command 参数。您应该将函数而不是字符串传递给命令。你可以使用 lambda:

btn2 = Button(frame2, text="Configure>>", command=lambda: os.system('python script.py args'))