Call external command in python, but stay in python - python

I'm using python on a Raspberry Pi to display an image slideshow with the program fim.
subprocess.call(["fim /home/pi/slates"], shell=True)
Once fim has opened the focus stays on fim, not python. There are keyboard commands in python that no longer respond.
How do I go back to python after fim has been opened? Is there a way to control fim through python if it's no longer in focus?

subprocess.call() will block until the program called is complete.
If you want the called program to run in parallel, it would be better to use something like subprocess.Popen()

Related

How can I start a GUI program from Python, but have the process end when the user closes the window?

I want to write a Python script which will start a GUI program (as in, run a binary program with subprocess.run or os.system or something). The script should not block until the program is done, it should start it and then keep running.
I'm doing this on Ubuntu.
I tried subprocess.Popen, but if I run, say subprocess.Popen("gedit"), I get strange behavior. If I open the Ubuntu system monitor (process manager), I can see that the gedit process appears when I run the script, and the gedit window itself opens. But if I close the window, the process doesn't end in the system monitor. The process stays there until my python script exits.
How can I get the behavior I want? The only thing I can think of right now is to just call subprocess.run in a different Python thread, is that the only thing I can do?
Try using subprocess.call. This has worked for me before.
subprocess.call(['command', 'arguments'])
The program should end when the window is closed.
You have to kill the subprocess you've created before you exit the program.
Try this.

Using os.system() in Python is open a program, can't see window of launched program

I am trying to launch a program/GUI from within a python code.
From the terminal, I can get the program to launch by simply typing the program name. A few lines get outputted to the terminal, and then a separate window opens with the GUI.
I tried to emulate this in python by running
os.system("<program name>")
The typical output lines, as mentioned above, get printed to the console, but no window opens up with the GUI.
Can os.system() be used to execute programs that have their own separate window?
From the Python manual:
[os.system] is implemented by calling the Standard C function
system()
That being said, you shouldn't have any problems launching a GUI application with os.system. I've just tried it myself and it works fine.
It also mentions in the manual that:
The subprocess module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is
preferable to using this function.
Maybe that's worth a try. Do any other GUI applications work when you spawn them with os.system?
Here is a solution using subprocess
import subprocess
subprocess.Popen("notepad.exe")
Or if you want to run a python program with a specific interpreter:
subprocess.Popen('{0} {1}'.format(PythonInterpreterPath,PythonFilePath.py))

How to make the text shell not appear when running a program

I'm just wondering (as im starting to make a game with pygame) is there a way to 1) Run a program that only opens a GUI and not the shell
and
2) Run the program without having to edit with idle (I'd like to do this so the game will look more professional.
I would like to just have a desktop shortcut or something that when clicked, runs only the GUI and doesnt show my code or show the shell.
If you're on windows, a quick thing could be to make a one-line batch script.
start pythonw.exe <filename>
The start keyword causes the shell to not wait for the program to finish.
pythonw vs python prevents the python terminal from appearing.
If you're on linux/mac, you could do the same with shell scripts.

How to run a python program in the background?

How can I make a python program run in the background?
I don't need the console running since all it does is sends me emails with updates once an hour.
Is there a way to do it when I convert it to exe using py2exe?
I am using python 2.7.8.
Also, is there a way to make it open in the boot menu immediately when the computer turns on from the code?
Thank you
If you are asking strictly how to hide the console as part of the py2exe bundling, that's simple. See - Hiding command-line dialog in py2exe
If you are asking how to have a program without a GUI, just build the program in py2exe as above and run it. Unless you have created a GUI, it will run on it's own until it terminates or is terminated.

How to launch another program in GUI (python tkinter)

i wrote a simple GUI using Python Tkinter. When i click a button it launches another program. But the GUI window stays there waiting for the the program to exit. I want the GUI to launch the program on Button1 and I can click on Button2 to run the test. I used os.system() to launch the program. Pls advise.
thanks
maximus
Yes, os.system() will wait.
As the documentation for os.system() points out, you might want to replace it by using the subprocess module, where there are many different ways to start commands and where you can choose if you want to wait for it to finish or not.

Categories