1

Is there a way to close a program using Python? I want to create a GUI that opens a program with a specific path when pressing a button, and then closes that program when pressing a different button. I want to open and close an STL file that uses the Windows program app Print 3D to view the file. I am able to open the file but can't close it.

Please see my code below:

from tkinter import *
import os

#Works to open program
def openProg():
    os.startfile(r'C:\Users\me\Desktop\sphere.stl')

#Does not work to close the program
def closeProg():
    os.close(r'C:\Users\me\Desktop\sphere.stl')

root = Tk()
root.geometry('370x260')
root.title('File Remote')

B1 = Button(root, text= 'OPEN', width=24, height =12, command=openProg).place(x=5, y=5)
B2 = Button(root, text= 'CLOSE', width=24, height =12, command=closeProg).place(x=187, y=5)

root.mainloop()
Forrest
  • 157
  • 14
  • To terminate a process you need the process ID, if your application is starting the app, then you should store that value of the initiating code. That is if the initiating code returns the process handle. Otherwise you need to query the system process manager to get the process ID and depending on the OS, may not have the complete path of the executable to interrogate. – Itanex Oct 24 '19 at 16:40
  • Is there a quick way for Python to find the PID? I know I can find it manually in the TaskManager, but that isn't the way I want to go. – Forrest Oct 24 '19 at 16:55
  • I do not know the specifics, though I would start with this question: https://stackoverflow.com/questions/2940858/kill-process-by-name and then search for Sys libraries for managing processes. – Itanex Oct 24 '19 at 17:00
  • Thanks for the help – Forrest Oct 24 '19 at 17:22

0 Answers0