2017-09-13 27 views
1

以及停止按鈕停止按鈕。按下開始按鈕後,它運行一個python程序。在我終止Python代碼之前停止不起作用。我該怎麼辦?這是我的代碼:開始和我創建了一個啓動按鈕的Python的Tkinter窗口

#!/usr/bin/python 
import Tkinter, tkMessageBox, time 

Freq = 2500 
Dur = 150 

top = Tkinter.Tk() 
top.title('MapAwareness') 
top.geometry('200x100') # Size 200, 200 

def start(): 
    import os 
    os.system("python test.py") 


def stop(): 
    print ("Stop") 
    top.destroy() 

startButton = Tkinter.Button(top, height=2, width=20, text ="Start", 
command = start) 
stopButton = Tkinter.Button(top, height=2, width=20, text ="Stop", 
command = stop) 

startButton.pack() 
stopButton.pack() 
top.mainloop() 

這些是我正在使用的2個功能。然後我創建了一個開始和停止按鈕。

+0

什麼是你的'頂部'變量? –

+0

Toplevel小部件用於顯示額外的應用程序窗口,對話框和其他「彈出式」窗口。 top = Tkinter.Tk() –

回答

2

原因停止按鈕不起作用,直到關閉該程序是因爲os.system塊調用程序(它在前臺運行test.py)。由於您是從需要活動事件循環的GUI調用它,因此程序會掛起,直到test.py程序完成。解決方案是使用subprocess.Popen命令,它將在後臺運行test.py進程。啓動test.py後,您應該按下停止按鈕。

#!/usr/bin/python 
import Tkinter, time 
from subprocess import Popen 

Freq = 2500 
Dur = 150 

top = Tkinter.Tk() 
top.title('MapAwareness') 
top.geometry('200x100') # Size 200, 200 

def start(): 
    import os 
# os.system("python test.py") 
    Popen(["python", "test.py"]) 


def stop(): 
    print ("Stop") 
    top.destroy() 

startButton = Tkinter.Button(top, height=2, width=20, text ="Start", 
command = start) 
stopButton = Tkinter.Button(top, height=2, width=20, text ="Stop", 
command = stop) 

startButton.pack() 
stopButton.pack() 
top.mainloop() 
相關問題