I put together a simple script to encode movies, now while the script works just fine I am having difficulties on changing the window title on script start instead on when its terminated. since is kind of useless to mark each window after it finished. can I use subprocess to perform such task? so change the title while the script runs?
I am using this in a bash script to change the title:
PS1="\e]2;$1\a\[$1 \W]\$"
if you are running the sub process without invoking any new terminal , you can rename the windows with below code
# your subprocess code here
import sys
sys.stdout.write(b'\33]0;title you want\a')
Related
I have a problem with how to show a program window if it was opened
how I open it is using
import os
os.startfile('path/to/progarm.exe')
But if progarm.exe is opened and I forgot to close
when I run that script again, A program.exe doesn't show on the
screen when I was on another window.
So which script can show up the opened program?
The main reason is that your script lost the focus the opened programs windows.
You can control windows with parameters.
os.startfile(path[, operation][, arguments][, cwd][, show_cmd])
also, you can check detail information here.
https://docs.python.org/3/library/os.html#os.startfile
To do what you want, you need to provide the path to the program in your machine.
import os
ms_word = r"C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"
os.startfile(ms_word)
I would not recommend doing what you are doing the way you are doing it.
Python has a subprocess module to do things like these, with functions specifically designed to do what you are doing. The easiest way to do what you are trying to do is to use the subprocess.runfunction.
import subprocess
subprocess.run(['path/to/app.exe', 'param1', 'param2'..], shell=True, check=True)
# params are optional.
However, subprocess.run is blocking, i.e., the script will not exit unless you close your launched application.
You can in that case use the subprocess.Popen class. This invokes the process and allows you to communicate with it asynchronously. However, if your objective is only to launch an app and shut down your script, then just call it as you made a call to run. The links I have provided has some examples. there are platform level considerations to make in the case of the parent-child process relationships, e.g. keep child running if the parent dies, kill the child with the parent, keep both of them running independently and allow them to die separately. probably this answer and this answer would provide you with some hints.
However, if you just want to launch an application and nothing else, just use the system shell, no?
I am trying to find the best method to pass data from python script2.py back to the calling script (script1.py). I am using Python 3 on Linux.
Script1 calls script2.py which then uses Selenium to open a browser window and do something things. When script2 ends, the browser remains open and script1 continues. What I want is to then be able to work on the browser window that was opened in script2, from within script1.
So far I have failed to find a suitable way to pass the information about the open browser window from script2 back to script1. I believe part of the issue is because when script2 is called, script1 waits until it has closed before continuing on, so there is no way to grab the variables open in script2. I do not want to change this aspect either.
in script1.py the below code calls script2 like this:
cmd = ['python3', '/home/admin/Desktop/Python_stuff/live-scripts/script2.py']
subprocess.Popen(cmd).wait() # Python will now wait for script to end before continuing
in script2 the below code opens the browser initially (which currently stays open after script2 closes) like this:
driver.get(targetURL)
I have looked at Pickle but could not get it to work how I wanted and will probably next try to use import JSON. But I wondered if I am missing a more simple method to achieve what I want here.
Thanks to pguardiario in the comments for setting me on the right track. The answer to this is to use script2 as a module.
I had actually tried to do this already, but it was crashing my script1 because I had dashes in the naming of my scripts, which works fine when they are scripts, but for some reason does not work when they are used as modules and yet gives you no error either. It just bombs out of the script altogether (on my linux/python 3 setup this is the case).
The solution was surprisingly simple once I resolved the file naming issues.
in script1.py I now have
import script2
then I call the main() function from the second script with
script2.main
I did not have to make any changes to script2, it runs from the local folder so needs no additional path setting, and I can now remove all the script calling code as well.
The main point being don't use dashes in the naming of modules as it gives you no error warnings but wont import them.
OS = windows 7
I have a python program (works) that is listening to activity on the usb bus. I want to perform a lot of tests that require a particular user input at a particular time. I would like to pop up a window that says, "press button xxx". The key point is that the mainloop needs to continue running because it's looking for events. I don't care about the window or if it remains or not and I don't need to capture any information from the window. I just want a message to the user to press the correct button at the correct time. Any type of signaling would work; it doesn't have to be a gui window. It doesn't have to look pretty. Appreciate any suggestions or links to something like this. thx
It sounds like the operation of the Python script you're running does not depend upon the user input you request. To run another process without interrupting the Python script execution you can use:
import subprocess
subprocess.Popen([exe,arg1,arg2,arg3])
where
exe = executable/script to run from your OS command line
arg1= first argument to pass to exe
arg2= second argument to pass to exe
etc... (as many arguments as your OS supports in a list)
This separate exe process could request input from the user.
I made a simple PyGTK - Glade GUI for an application. I made the button, and the on_button_click calls a bash script.
I would like to show a popup window while the bash script is running, and hide it after it is done. I made the window called runningWindow in Glade and wrote the following code:
def on_button1_clicked(self,widget):
self.glade.get_object("runningWindow").show()
os.system('bash run.sh')
self.glade.get_object("runningWindow").hide()
This code shows nothing while run.sh is running. If I remove the hide() line, the window is displayed correctly, but only AFTER the run.sh process has finished.
The init function that starts the GUI:
def __init__(self):
self.gladefile = "MyFile.glade"
self.glade = gtk.Builder()
self.glade.add_from_file(self.gladefile)
self.glade.connect_signals(self)
self.glade.get_object("MainWindow").show_all()
How can I display the window before the os.system is called?
Thank you for your help!
You probably want to look into the subprocess module, and run your subprocess in a background task.
I couldn't solve the problem, but I could get around it. Maybe others find it useful:
First, one should use subprocess.Popen() instead of subprocess.call(). It puts the subprocess automatically in background, so the running window is displayed. The problem was that if I didn't remove the .hide() command, the window immediately disappeared after popping up. Otherwise, I couldn't hide the window when the run was finished.
To solve this, I created the runningWindow in a new (runningWindow.glade and runningWindow.py) file.
Then I created a wrapper bash script that says:
python runningWindow.py &
pid=$!
bash run.sh
kill pid
And called this bash script with subprocess.Popen() from the main python script.
I have a Tk python program that creates a list of python files in the current directory and generates a button for each of them. When you click a button the corresponding python program is launched via subprocess in a new gnome-terminal. I'd like to switch the button's color to red after the subprocess has finished executing on the new terminal. Unfortunately, the button is changing color almost immediately.
from Tkinter import *
import os, subprocess
root = Tk()
buttonsD = {}
def launch(ourfile):
p=subprocess.Popen(["gnome-terminal","-e","python " + ourfile], shell=False)
buttonsD[ourfile].configure(bg='red')
dirlist=os.listdir(os.getcwd())
for fname in dirlist:
if fname.endswith('py') and fname != 'gui2.py':
buttonsD[fname] = Button(root,text=fname,command=lambda i=fname: launch(i))
buttonsD[fname].pack(side=TOP,expand=YES,fill=BOTH)
root.mainloop()
Almost immediately means that I can wait while p.poll == None, and see that it takes a moment for gnome-terminal to be created. But as soon as the terminal is created the button goes red, even though a process is still running in the new terminal. I can't create a new gnome-terminal and then communicate the process I'd like to run either. It seems gnome-terminal just creates a new instance of bash and then returns done, so there's a pipe error if I try to communicate to its stdin.
I believe gnome terminal is doing a double fork, in order to detach itself from the process group of its parent -- so what's actually your subprocess terminates almost immediately, as you observe, and everything is happening in a further descendant that you have no direct access to.
Unfortunately I don't believe gnome terminal offers any way to disable this double fork behavior; so, to find out when the "further descendant" is finished, you'll have to identify that process and monitor it periodically. Interacting directly with it is also a pretty tall order -- no easier than interacting with any "random" process you're not related to:-(.
There are two questions here: what command line to use to launch a Python program in gnome-terminal, and how to use subprocess in a Tkinter app. I only know about the latter.
subprocess.Popen returns immediately, which is why the button is turning red immediately. I think you probably need to make a list of which programs are running. Then write a function poll_processes which calls poll() on each running process, and when the result is not None, removes it from the list and turns the button red.
Then all you have to do is arrange for Tkinter to periodically call that function, which you can do by calling frame.after(msec, poll_processes) to schedule the first call to poll_processes and then having poll_processes do the same thing to schedule the next call.