-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"

Jayden Collis
  • 145
  • 11
  • Possible duplicate of [How to open external programs in Python](https://stackoverflow.com/questions/37238645/how-to-open-external-programs-in-python) – rajivas Sep 10 '19 at 02: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. – Jayden Collis Sep 10 '19 at 02: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? – Bryan Oakley Sep 10 '19 at 02: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. – Bryan Oakley Sep 10 '19 at 02: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 – Jayden Collis Sep 10 '19 at 02:42
  • first: you could use `print()` in function to see if it really executed. Second: it may need information what program to use to open it - `os.system("/path/program.exe file_to_open")` – furas Sep 10 '19 at 02:45
  • I tried using ```print()```, nothing happened. Is this a vital piece of info? – Jayden Collis Sep 10 '19 at 02:46
  • wait, wait, wait. Should the path be in quotation marks (")? – Jayden Collis Sep 10 '19 at 02:49
  • did you use `print('some message')` and run code in console/termina/cmd.exe to see result ? If you don't see message then it didn't run function. If you see message then it doesn't know what program use to open file. I don't know if it use system's setting or it has own settings for files. I use Linux and Python doesn't have this function on Linux. – furas Sep 10 '19 at 02:50
  • Im using windows 10 – Jayden Collis Sep 10 '19 at 02:51
  • if `path` is variable with string then it shouldn't have quotation. If `path` means literally `"/path/program.exe file_to_open"` then it has to be in quotation. – furas Sep 10 '19 at 02:52
  • Ok so I checked the GitHub Source code and some other stuff and it turns out there is a problem in calling the file from a directory. Sorry for the hassle. – Jayden Collis Sep 10 '19 at 06:49

2 Answers2

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

Gro
  • 1,613
  • 1
  • 13
  • 19
  • This code doesnt work... Am I doing something wrong? – Jayden Collis Sep 10 '19 at 02:54
  • @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. – Gro Sep 10 '19 at 03:17
  • I know this is a year late but I've just gotten back to this issue and this solution works fine. Thanks – Jayden Collis Jul 30 '20 at 23:28
0

have you tried "import os"?

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

Pakium
  • 293
  • 2
  • 9