How to kill a particular Tkinter window or python script using CMD

Hello,

I am trying to write batch script that will kill a particular python script or the tkinter windows produced by that python script from CMD.

taskkill /IM python.exe /F won’t do the job since I need other python scripts to remain running.

The tkinter windows don’t have their own PID in task manager and I can’t use the name of the window either (or at lest I’ve tried).

Killing by script name (the name of the script creating the tkinter windows) would also do the job, if possible.

Can anyone help?

2 Likes

One thing you might try is a “named pipe”, which allows for inter-process communication. You basically have a pipe which you can send a shutdown message to from another script. It looks like there is support for them on Windows via pywin32library, in the form of the the win32pipemodule.

The idea is that you have a listener in your tkinter app’s main loop and your second script sends a shutdown message.

This assumes your tkinter app is well behaved and not frozen. In that case, you might be able to write just it’s process id to a file on the disk that your script can read. Even on windows there are process ids and taskkill respects them. You can use os.getpid() to get the process id and then write it to a file, read that with the other script and and use os.kill(pid) to kill it.

Pipes sound like more fun, though.

Thanks for your answers. Pipes does look interesting indeed, though perhaps far above my capabilities right now. I looked it up but could not get my head around it.

I did however find success with “root.destroy()” (not sure how to make code snippets appear with the dark background as you have).

“root.destroy()” worked great from within the main script as one of the “if” conditions however my attempt at importing the same function into another python file to execute it externally didn’t work.

For example:

from GUIscript import root, gui_kill

root.after(1_000, gui_kill)

“GUIscript” is the script running the windows I want to kill and “gui_kill” is the function inside the GUIscript doing the killing.

I don’t even get an error with this but the output is ‘after#0’. I have no idea what is going on here…as you can see, my knowledge is extremely limited here.

Thanks for sharing Info.