2017-03-07 32 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() 
相关问题