2017-03-07 77 views
0

我目前正在開發一個Windows應用程序,需要使用我創建的一個.exe文件(在下面的例子中稱爲kill_game.exe)。這個.exe文件接受序列化的對象作爲參數。我使用subprocess.Popen來調用這個.exe文件。當我這樣做時,我的當前窗口的確切副本打開,我的.exe文件將不會運行,直到我自己關閉第二個窗口。我完全被難住爲什麼會發生。我希望.exe文件在沒有第二個GUI打開的情況下運行。我試圖切換到multiprocessing,因爲它存在於this example on SO中,但這也不起作用。創建一個子進程打開不需要的窗口

下面,我將發佈我的代碼的簡化版本(我的原始版本是300多行),說明GUI和用於打開.exe文件的函數。我還會發布圖片,當我按下按鈕時會發生什麼。

from tkinter import * 
from tkinter import Tk 
import subprocess 

root = Tk() 

sizex = sizey = 500 
posx = posy = 100 
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy)) 

myframe = Frame(root, relief=GROOVE, width=500, height=500, bd=1) 
myframe.place(x=0, y=0) 

canvas = Canvas(myframe) 
frame = Frame(canvas) 
canvas.pack(side="left") 
canvas.create_window((0, 0), window=frame, anchor='nw') 

button = Button(frame, text="Hello World") 
button.grid(row=0, column=2) 

games_to_stop = [] 
button.bind("<Button>", 
      lambda event, game_list=games_to_stop: some_function(game_list)) 

def some_function(game_list): 
    #This function is the function of interest, which is bound to my "Hello 
    #World" Button 
    child_script = r"C:\Users\Willy\AddictKiller\dist\kill_game.exe" 
    subprocess.Popen([child_script, game_list]) 

root.mainloop() 

這是按鈕被按下之前的GUI:

Before the button is pressed

這是GUI和第二GUI(由於某種原因看起來的我的非簡化代碼的GUI)按下按鈕後:

enter image description here

+1

如果你代替'child_script =「記事本,會發生什麼部分解決這個問題。 EXE「'? – eryksun

+0

你是否通過pythonw.exe作爲腳本運行Tk應用程序,還是凍結的可執行文件? – eryksun

+0

@eryksun如果我用notepad.exe替換它,然後記事本彈出,對話框也會彈出,詢問我是否要創建一個新的.txt文件 – Will

回答

0

我在那裏,當我跑我的馬我的程序重複類似的問題在Tkinter窗口中將打開並且該功能在我關閉所有重複項之前不會運行。 你通過把在那裏被後面的創建tkiner窗口if語句,以確保您只在主窗口中創建一個窗口,這樣

if __name__ == "__main__": 
     root = Tk() 
相關問題