-1

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"

12
  • Possible duplicate of How to open external programs in Python Commented Sep 10, 2019 at 2:35
  • It's saying that the programmer didn't use the full path and got an error. I used the full path and got no error. Commented Sep 10, 2019 at 2:38
  • Your code doesn't define path, so there's no way we can reproduce your problem. Are you absolutely certain the either a) the path is absolute, or b) the path is correctly relative not to the location of the script, but to the current working directory? Commented Sep 10, 2019 at 2:39
  • "I've tried os.startfile(path) but that doesn't open the file." - what does it do that's different than what you expect? Does it throw an error? If so, what's the error? Nobody cares about the actual path, you can fake it as long as the fake path is representative of the real path. Commented Sep 10, 2019 at 2:40
  • Bryan, Furas, there is no error, its just that nothing happened at all when I cllck the run button. My path is correct because I've used the same pathing method I used with other files Commented Sep 10, 2019 at 2:42

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

This code doesnt work... Am I doing something wrong?
@JaydenCollis Not sure what you are doing. I have added screen shot of a successful execution of code. It calls another python file which has just one statement print("Hello World"). As you can see the code executes and the return code is 0.
I know this is a year late but I've just gotten back to this issue and this solution works fine. Thanks
0

have you tried "import os"?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.