RevealYourPuss
RevealYourPuss

Reputation: 33

How to close a window in python by name

I'm making a minecraft console client script for a friend It opens the console client based on a txt file

I figured out how to kill/terminate the window but this part doesn't physically close the window it just sets it to a normal command prompt window because it's created using a batch command

How would I close a program completely by name as if I pressed the "x" button in the top right hand corner of the application.

    os.system("taskkill /f /im MinecraftClient.exe")
 //for proc in psutil.process_iter():
   //  if proc.name() == "MinecraftClient.exe":
   //      proc.terminate()
   //      reap_children()

Neither of those actually closes the .exe it just changes it into a command prompt window. And I don't want to have to run through the for proc in psutil.process_iter() again just to close the command prompt windows.

EDIT 1:

How it opens the MinecraftClient.exe please keep in mind it is a console client that you download from github

def connect(file, directory, executeds):
with open(file) as file:
    for line in file:
        #stdout=PIPE, stderr=PIPE, stdin=PIPE,
        lines = line.split(":")
        login = lines[0]
        password = lines[1]
        IP = lines[2]
    //     CREATE_NEW_PROCESS_GROUP = 0x00000200
   //      DETACHED_PROCESS = 0x00000008
        command = "MinecraftClient.exe {0} {1} {2}".format(login, password, IP)
        executed = Popen("start cmd /K " + command, cwd=directory, shell=True)
        out, err = executed.communicate()
        executeds.append(executed.pid)
        if executed.returncode == 0:
            print("Connected {0} to {1} successfully".format(login, IP))
        else:
            print("Failed to connect {0} to {1}.".format(login, IP))
            print("{0}".format(err))

        time.sleep(3)

Some things I'm not using because they were just tests

Upvotes: 3

Views: 13294

Answers (1)

Code Enjoyer
Code Enjoyer

Reputation: 701

I just checked, and I can't find anything called MinecraftClient.exe running for me. The Minecraft process is actually called javaw.exe - try killing that instead. If this is not the problem, I managed it with the subprocess module as such:

import subprocess
subprocess.call("taskkill /f /im javaw.exe", shell=True)

shell=True prevents it from opening a command prompt on your screen.

EDIT
Your screenshots show that it ends up becoming cmd.exe - try killing that instead?
Okay, after downloading the program I managed to successfully kill it using subprocess.call('taskkill /f /im MinecraftClient.exe & taskkill /f /im cmd.exe', shell=True)

Upvotes: 6

Related Questions