+ 1

Is there a way to close other windows?

Does anyone know , if there is a way, how to close a non python window from a python window for example closing a Note Pad window? If anyone knows the line of code so I can allow a tkinter window control other windows using buttons... 😊 Cheers

28th Nov 2017, 9:28 PM
Oliver Stannard
Oliver Stannard - avatar
7 Answers
+ 7
You can import subprocess and use a call method to invoke the taskkill.exe. This should do the trick: import subprocess subprocess.call(['taskkill', '/IM', 'notepad.exe']) # closes the Notepad or displays an error (does not raise an exception!) if Notepad not open EDIT: just read the help of taskkill and closing the Notepad is the very first example there! ;)
28th Nov 2017, 9:43 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
BTW, let me know if that worked for you. I tried it on my Win7 and it worked, but I have a pretty simple setup. While taskkill can be pretty powerful... :)
28th Nov 2017, 10:37 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
@Kirk From what I read, it can try to terminate (possibly spawning the confirm window) or forcefully kill a process, even with all other dependent subprocesses.
29th Nov 2017, 7:00 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
@Kirk Just if you were wondering -- yes, I already tried it in Sololearn, as Python here runs on Windows virtual machine. They got it covered, though, as access to all default applications is denied (that still means that they are there!) :) https://code.sololearn.com/chDD2bSY225G/?ref=app
29th Nov 2017, 7:02 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
In plain old Windows Scripting Host (e.g., vbscript), you would "AppActivate" the desired window by title and then send the appropriate key sequence with SendKeys. There's a SendKeys implementation for Python 2.x (I can't test it): https://pypi.python.org/pypi/SendKeys And this method can apparently dispatch to wscript directly: https://win32com.goermezer.de/microsoft/windows/controlling-applications-via-sendkeys.html Edit: Searching, there seems to be a lot of ideas out there (e.g., using ctypes, etc), but not having a Windows box is interfering with me being more specific.
28th Nov 2017, 11:41 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Yup worked perfectly thanks alot Kuba Siekierzyński !!!! 😊😊😊😊
29th Nov 2017, 5:07 PM
Oliver Stannard
Oliver Stannard - avatar
+ 2
@Kuba - Just curious... Does notepad get to run the modal "Save" dialog (or is it literally a force quit, the process is brickwalled)?
29th Nov 2017, 6:36 PM
Kirk Schafer
Kirk Schafer - avatar