subprocess.call()与GUI交互(Tkinter)(subprocess.call() interacting with GUI (Tkinter))

编程入门 行业动态 更新时间:2024-10-28 10:31:02
subprocess.call()与GUI交互(Tkinter)(subprocess.call() interacting with GUI (Tkinter))

有没有办法使用subprocess.call()或subprocess.Popen()并通过Tkinter的Entry小部件与stdin交互,并将stdout输出到Text小部件?

我不确定如何处理这样的事情,因为我是使用subprocess模块的新手。

Is there any way to use subprocess.call() or subprocess.Popen() and interact with the stdin via Tkinter's Entry widget, and output the stdout to a Text widget?

Im not really sure how to approach something like this, as i am new to using the subprocess module.

最满意答案

我认为我已经掌握了使用Entry作为stdin进行子进程的基础知识。 您可能需要根据自己的需要进行微动(re:输出到Text小部件)。

此示例调用测试脚本:

# test.py: #!/usr/bin/env python a = raw_input('Type something!: \n') #the '\n' flushes the prompt print a

只需要一些输入(来自sys.stdin )并打印它。

调用它并通过GUI与它交互完成:

from Tkinter import * import subprocess root = Tk() e = Entry(root) e.grid() b = Button(root,text='QUIT',command=root.quit) b.grid() def entryreturn(event): proc.stdin.write(e.get()+'\n') # the '\n' is important to flush stdin e.delete(0,END) # when you press Return in Entry, use this as stdin # and remove it e.bind("<Return>", entryreturn) proc = subprocess.Popen('./test.py',stdin=subprocess.PIPE) root.mainloop()

现在无论输入到Entry e (后跟Return键),都会通过stdin传递给proc 。

希望这可以帮助。


还要检查这个有关子流程问题的stdout的想法。 你需要编写一个新的stdout来将stdout重定向到textwidget,例如:

class MyStdout(object): def __init__(self,textwidget): self.textwidget = textwidget def write(self,txt): self.textwidget.insert(END,txt) sys.stdout = MyStdout(mytextwidget)

但我建议阅读人们已经实现这一目标的其他例子。

Think I've got the basics of using an Entry as stdin to subprocess. You may have to jiggle it about for your own needs (re: output to Text widget).

This example calls a test script:

# test.py: #!/usr/bin/env python a = raw_input('Type something!: \n') #the '\n' flushes the prompt print a

that simply requires some input (from sys.stdin) and prints it.

Calling this and interacting with it via a GUI is done with:

from Tkinter import * import subprocess root = Tk() e = Entry(root) e.grid() b = Button(root,text='QUIT',command=root.quit) b.grid() def entryreturn(event): proc.stdin.write(e.get()+'\n') # the '\n' is important to flush stdin e.delete(0,END) # when you press Return in Entry, use this as stdin # and remove it e.bind("<Return>", entryreturn) proc = subprocess.Popen('./test.py',stdin=subprocess.PIPE) root.mainloop()

Now whatever is typed into Entry e (followed by the Return key), is then passed via stdin to proc.

Hope this helps.


Also check this for ideas about stdout of subprocess question. You'll need to write a new stdout to redirect stdout to the textwidget, something like:

class MyStdout(object): def __init__(self,textwidget): self.textwidget = textwidget def write(self,txt): self.textwidget.insert(END,txt) sys.stdout = MyStdout(mytextwidget)

but I would recommend reading other examples where people have achieved this.

更多推荐

本文发布于:2023-08-07 18:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:call   subprocess   GUI   interacting   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!