【问题标题】:Running an existing tkinter script from another tkinter script从另一个 tkinter 脚本运行现有的 tkinter 脚本
【发布时间】:2017-03-22 07:09:01
【问题描述】:

我是 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]
    

这两种方法都不起作用。

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

编辑:我也不需要与新窗口进行通信,因为它将写入稍后将使用的文件。只需将控件从子窗口返回到父窗口就足够了(单击确定后)

-维奈

【问题讨论】:

    标签: user-interface tkinter python-3.4


    【解决方案1】:

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

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

    【讨论】:

    • 嗨,它适用于 lambda。我还使用 subprocess.call('python script args', shell=true) 让它工作。感谢您的帮助。
    • 我很高兴它成功了。如果您认为此答案有帮助,您可以选择投票和/或接受此答案(单击左侧的复选符号。)
    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多