首页 > 解决方案 > Python,Tkinter,使用子进程 Popen,运行外部程序

问题描述

首先我是python初学者,所以我需要很多人的建议。

我正在制作一个与另一个外部程序同时运行的 GUI 程序。

并且外部程序根据用户命令行输入运行。

它的命令行与 shell 或 python 不同。它有自己的命令行。

这是外部程序的外观。

左边是我的 GUI 程序,右边是根据自己的命令行参数运行的外部程序

到目前为止,我能够使用 subprocess.Popen 运行外部程序,但我没有使用打开 PIPE 的 stdin、stdout、stderr 参数,这意味着我没有将此外部程序与另一个程序连接。它只是独立运行,不与他人交流。

但这是问题所在

我想将终端嵌入到 GUI 程序中,以便它可以与外部程序通信。

所以我在我的 GUI 程序中插入了文本框和输入框,并尝试将 GUI 程序和外部程序完全集成。

为此,我在 subprocess.Popen 实例中添加了 (stdin = subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 选项。

然而问题在于,当 subprocess.Popen 开始其工作时,外部程序会立即终止。外部程序在基于 subprocess.Popen 和 stdin、stdout、stderr 选项运行时立即终止

当我没有添加标准输入、标准输出、标准错误选项时,这个问题没有发生。

我也认为我需要解释一下这个外部程序

外部程序基于 C++ 语言。所以我想解释一下这个程序的基本机制。

该程序使用 while(1) 无限循环等待用户输入。它基于内置的命令行参数执行多项工作。

因此外部程序假设连续运行,而不是立即终止,因为它基于 while(1) 无限循环和用户输入工作。

这是我的代码

def main_window():

        root = Tk()

        root.title("Seoul Cell")
        root.geometry("1300x1000")
        root.resizable(True, True)
            
        global monitor1
        monitor1 = Layout(root)
        monitor1.pack(fill="both", expand=True)

        # 메뉴바 생성
        menu = Menu(root)
        root.config(menu=menu)

        menu_file = Menu(menu, tearoff=0)
        menu_file.add_command(label="Open Cell Information", command = open_cell_info)
        menu_file.add_command(label="Partitioning", command = open_partition_info)
        menu_file.add_command(label="Pairing", command= open_pairing)
        menu_file.add_command(label="Placement", command = open_placement)
        menu_file.add_command(label="Routing", command = open_routing)
        menu.add_cascade(label="Result Display", menu = menu_file )

        menu_file2 = Menu(menu, tearoff=0)
        menu_file2.add_command(label="Live result from program", command= live_work)
        menu.add_cascade(label="Live Result Display", menu = menu_file2)

        menu_work = Menu(menu, tearoff= 0)
        menu_work.add_command(label="Open layerinfo file", command= open_layerinfo)
        menu_work.add_command(label="save as layerinfo.txt", command = save_as)
        menu.add_cascade(label="TechInfo", menu = menu_work)

        # 2번째 메뉴바

        menu2_frame = Frame(root)
        menu2_frame.pack(fill = "x")
        layer_but = Button(menu2_frame, text = "Layers", command = open_window)
        layer_but.pack(side = "left", padx=3, pady=3)

        # Script

        script_frame = LabelFrame(root, text = "script")
        script_frame.pack(fill='x', side="bottom",padx=10, pady=10)

        script_menu_frame = Frame(script_frame)
        script_menu_frame.pack(fill='x')

        # Script Text box

        scrollbar_script = Scrollbar(script_frame)
        scrollbar_script.pack(side='right',fill="y")


        global script
        script = Text(script_frame, height=10, yscrollcommand=scrollbar_script.set)
        script.pack(fill='x', side="bottom", padx=5, pady=5)
        scrollbar_script.config(command=script.yview)

        process_exe = exe_program()

        root.mainloop()


def exe_program():

    top = Tk()
    top.title('program')
    top.geometry("600x400")

    program = filedialog.askopenfilename(title = 'Choose the execution File')
    process_exe = subprocess.Popen(program, stdin= subprocess.PIPE, stdout= subprocess.PIPE, stderr= subprocess.STDOUT, universal_newlines= True)
    (out, err) = process_exe.communicate()
    print(out)

    top.mainloop()

    return process_exe




if __name__ == '__main__':

    main_window()

我迫切需要帮助

感谢大家阅读我的作品

标签: pythontkintersubprocessstdoutpopen

解决方案


推荐阅读