簡體   English   中英

subprocess.call()與GUI交互(Tkinter)

[英]subprocess.call() interacting with GUI (Tkinter)

有沒有辦法使用subprocess.call()subprocess.Popen()並通過Tkinter的Entry小部件與stdin交互,並將stdout輸出到Text小部件?

我不確定如何處理這樣的事情,因為我是使用subprocess模塊的新手。

我認為我已經掌握了使用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鍵),然后通過標准輸入傳遞給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)

但我建議閱讀人們已經實現這一目標的其他例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM