Python:使用Tkinter时脚本不会退出

时间:2014-02-22 10:54:15

标签: python python-2.7 tkinter subprocess exit

我创建了3个脚本,现在我在Tkinter中创建了一个简单的前端菜单。当我单独使用脚本时,它们会按预期工作和退出,所以我知道它们没有问题。问题必须出在我的菜单上(如下)。

从菜单中选择其中一个工具,调用另一个脚本并运行它。脚本只是挂起并等待,直到我按下键盘输入。当我按Enter键时,脚本会退出。如何让它自动退出而不是我必须按Enter键?

提前致谢。

from Tkinter import *
import Tkinter
import subprocess

root = Tkinter.Tk()
root.title("SimonsSoftware, 2014")
root.geometry('255x200+200+200')
text = Text(root)
text.insert(INSERT, "Please select which tool\nyou wish to use...")


def close_window():
    root.withdraw()

def kill_window():
    root.destroy()

def callDuff():
    print "Call back works"
    subprocess.Popen("python duff.duplicateFileFinder\duff.py", shell=True)
    kill_window()

def callFibs():
    print "Call back works"
    subprocess.Popen("python fibs.FileInvestigationBiteSize\\fibs.py", shell=True)
    close_window()

def callShift():
    print "Call back works"
    subprocess.Popen("python shift.SimonsHashInfoFinderTool\shift.py", shell=True)
    close_window()


buttonOne = Tkinter.Button(root, text ="DUFF", relief=FLAT, command=callDuff)
buttonTwo = Tkinter.Button(root, text ="FIBS", relief=FLAT, command=callFibs)
buttonThree = Tkinter.Button(root, text ="SHIFT", relief=FLAT, command=callShift)

buttonOne.pack()
buttonTwo.pack()
buttonThree.pack()
text.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:1)

明确等待子进程将解决您的问题。 (使用subprocess.Popen.wait

def callDuff():
    print "Call back works"
    proc = subprocess.Popen("python duff.duplicateFileFinder\duff.py", shell=True)
    #^^^^^^
    kill_window()
    proc.wait() # <-----

BTW,root.withdraw()不会终止该程序。它只是隐藏了主窗口。