How to launch another program in GUI (python tkinter) - python

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.

Related

terminate python script with key press outside of console window (on windows)

I am writing a script to automate stuff in another program (let's call it the main program) on Windows (the main program is only available for Windows). The script reacts to certain visual elements in the main program by clicking on them. Now I would like to add some way of terminating the script while I'm in fullscreen mode in the main program. Right now I have to alt-tab into the console where my script is running and press ctr+c. The problem here is, that if I alt-tab while my script is running, it might click some random stuff on my desktop.
Ideally, I would like to add some key combination that I have to press while I'm in the main program that would terminate (or pause) the script. I tried to do this with pynput.keyboard, but windows defender marks it as a keylogger (which it technically is) and removes my script. Is there a way of doing this without triggering windows defender?
Alternatively, I guess I could automatically pause my script, whenever the active window is not the main program. I am wondering whether this might be the common approach for cases like mine?

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.

Call external command in python, but stay in 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()

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 terminate the running function in Python Shell?

I thought this is an easy task but I googled around and had no clue at all.
I'm trying to run the pyhook tutorial and once I get to pythoncom.PumpMessages(), the function will stay idle and wait for Windows events.
I don't want to close down the window nor restart the shell because of all imported library and defined functions. Is there any way to termindate just the function?
I tried ctrl+c but it doesn't work at all.
Thanks a lot.

Categories