0

I'm currently developing a Windows application that requires the use of an .exe file I've created (called kill_game.exe in the below example). This .exe file accepts serialized objects as arguments. I call this .exe file using subprocess.Popen. When I do so, an exact copy of my current window opens and my .exe file won't run until I close the 2nd window myself. I'm completely stumped as to why it happens. I'd like the .exe file to run without the 2nd GUI opening itself. I tried to switch to multiprocessing as it exists in this example on SO, but that didn't work either.

Below, I'll post a simplified version (my original version is 300+ lines) of my code that illustrates the GUI and the function that is used to open the .exe file. I'll also post pictures of what happens when I press the button.

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()

This is the GUI before the button is pressed:

Before the button is pressed

This is the GUI and the 2nd GUI (which for some reason looks the the GUI of my non-simplified code) after the button is pressed:

enter image description here

Community
  • 1
  • 1
Will
  • 71
  • 8
  • 1
    What happens if you substitute `child_script = "notepad.exe"`? – Eryk Sun Mar 07 '17 at 01:55
  • Are you running the Tk app as a script via pythonw.exe, or is it a frozen executable? – Eryk Sun Mar 07 '17 at 01:56
  • @eryksun if I replace it with notepad.exe, then notepad pops up and so does a dialogue box asking me if I'd like to create a new .txt file – Will Mar 07 '17 at 02:03
  • @eryksun I am unsure of the difference between the two, I believe pythonw.exe. What exactly is a frozen executable? A Google searched didn't help me find an answer. – Will Mar 07 '17 at 02:07
  • Do you get the 2nd copy of your Tk GUI when it runs notepad.exe instead of kill_game.exe? – Eryk Sun Mar 07 '17 at 02:09
  • @eryksun I do not get the second copy of my GUI when I have notepad.exe. – Will Mar 07 '17 at 02:13
  • What about if you rename kill_game.exe to another name like gamekiller.exe? – Eryk Sun Mar 07 '17 at 02:15
  • A frozen executable (e.g. made by py2exe) zips up your script and its dependencies and appends it to a launcher that allows the script to run without installing Python. OTOH, if you're double-clicking on a .py or .pyw script to run the GUI, then .py/.pyw files are probably associated with an installed interpreter (python.exe, pythonw.exe) or the Python 3 launcher (py.exe, pyw.exe). – Eryk Sun Mar 07 '17 at 02:16
  • @eryksun I renamed my actual kill_game.exe file to gamekiller.exe and readjusted my tk script accordingly, the second GUI popped up again. I also set the child_script variable to a .exe file that didn't exist and received a File Not Found Error. – Will Mar 07 '17 at 02:21
  • @eryksun if that's the case, then my kill_game.exe script was made with pyinstaller. My main script, the one that constructs the GUI, I'm running from PyCharm, not as an exe. – Will Mar 07 '17 at 02:28
  • Try running your GUI script from the command prompt instead. – Eryk Sun Mar 07 '17 at 02:53
  • @eryksun I did that with both my original script and my simplified script. Again the second GUI opened, no change from my original GUI. – Will Mar 07 '17 at 03:00
  • Running any other executable doesn't appear to cause the 2nd GUI to pop up, so the natural assumption is that the problem is in kill_game.exe. I assume it's supposed to be a windowless utility program that works in the background to kill a list of games passed on the command line. Maybe you mistakenly configured the pyinstaller setup to include your GUI in kill_game.exe. – Eryk Sun Mar 07 '17 at 03:07
  • @eryksun Alright, I'm unsure of how to do that and will do some deeper digging. I'm basically stumped. If there a better way I could be doing this? – Will Mar 07 '17 at 03:37

1 Answers1

0

I had a similar problem where when I run my program duplicates of my main Tkinter window would open and the function wouldn't run until I close all the duplicates. you fix this by putting the part where the tkiner window is being created behind an if statement to make sure you only create a window in the main window as such

    if __name__ == "__main__":
        root = Tk()