How to open a program from my computer in python?

Im making a window in tkinter and I can't figure out how to run an external program in python. I am using the full path and am getting no errors, the file is just not loading.

I've tried os.startfile(path) but that doesn't open the file.

Here is my code:

from tkinter import *
from tkinter import ttk
import os

def run_selected():
    if ver.get() == "Test":
        os.startfile(path)

master = Tk()

ver = ttk.Combobox(master, state="readonly", values=["Test"])
ver.pack()
Button(master, text="Run", command=run_selected).pack()

master.mainloop()

How do I fix this so that I can open the file?

EDIT: My (path) is "C:\Backup\Mindustry\Mindustry.exe"

2 answers

  • answered 2019-09-10 02:45 Gro

    You can spawn a process using many different ways. Check here and here

    One of the ways could be

    import subprocess
    
    def run_selected():
        if ver.get() == "Test":
           subprocess.run([path], check=True)
           # e.g. subprocess.run(["ls","-ltr"], check=True)
    
    

    However, since you are spawning it from Tkinter, you could spawn it from a different thread (unless process ends within milliseconds), else, the default Tkinter thread will get busy and the UI will appear frozen

    enter image description here

  • answered 2019-09-10 05:06 Pakium

    have you tried "import os"?

    import os os.startfile(path/.file.py)