我有一個Tk python程序,它在當前目錄中創建一個python文件列表,併爲它們中的每一個生成一個按鈕。當你點擊一個按鈕時,相應的python程序將通過子進程在新的gnome終端中啓動。在子進程在新終端上完成執行後,我想將按鈕的顏色切換爲紅色。不幸的是,該按鈕幾乎立即改變顏色。在子進程完成後爲按鈕着色
from Tkinter import *
import os, subprocess
root = Tk()
buttonsD = {}
def launch(ourfile):
p=subprocess.Popen(["gnome-terminal","-e","python " + ourfile], shell=False)
buttonsD[ourfile].configure(bg='red')
dirlist=os.listdir(os.getcwd())
for fname in dirlist:
if fname.endswith('py') and fname != 'gui2.py':
buttonsD[fname] = Button(root,text=fname,command=lambda i=fname: launch(i))
buttonsD[fname].pack(side=TOP,expand=YES,fill=BOTH)
root.mainloop()
幾乎就意味着我可以等待p.poll ==無,看到它花費的gnome-terminal要創建的時刻。但是一旦終端被創建,按鈕就會變紅,即使一個進程仍在新終端中運行。我無法創建一個新的gnome終端,然後傳遞我想要運行的進程。看起來gnome-terminal只是創建一個bash的新實例,然後返回完成,所以如果我嘗試與其stdin進行通信,那麼會出現管道錯誤。