0

On Windows there is a WinAPI: FindWindow that you can use to get window handle of a existing window and use this handle to send message to it. Is there a python module can do that too? Find a window & communicate with it?

If this module do exist, could the same mechainsm be able applied on Ubuntu too? Thanks a lot!

maple
  • 15
  • 5
  • I think that you should use the win api from python. Search for "call win api from python" – Oscar Vicente Perez Jul 14 '16 at 07:08
  • Thanks! But isn't there a module can do this? I want my python program can both apply to Windows & Ubuntu without modification..... – maple Jul 14 '16 at 07:10
  • Is that window from your program, another program made by you or a third party one? – Oscar Vicente Perez Jul 14 '16 at 07:18
  • I hope there is a solution for the latter case(thrid party, already existing window,...) but if there isn't I would like to try to open the Window(program) within my python program & try to to communicate with it..... – maple Jul 14 '16 at 07:23
  • What do you want to send to the window? But I think that you must use the Win API and QT or whatever the Ubuntu one uses. More info about the program? – Oscar Vicente Perez Jul 14 '16 at 07:28
  • Actually the demand comes from the needs that daily open three terminals to execute specific command on Ubuntu. Becuase I had few expereience on WinAPI & python(it's really easy to use), so I just wonder if I can do this with them. I plan to use my python program to sleep 24 hrs, then send message to those three terminals alreay opned one day before to proceed new commands. Becuse the terminal GUIs are needed, I can't use crontab to do that.............. Any suggestions? – maple Jul 14 '16 at 07:34
  • You want to execute shell commands? You can use calle method to do it – Oscar Vicente Perez Jul 14 '16 at 07:36
  • I can open new terminals but how about the old terminals? Is there a way I can close them?(So it first comes to me if I can get the terminal handles & send quit singal to them...) Otherwise there are more & more consoles as time goes by....... – maple Jul 14 '16 at 07:41
  • Can you describe al your problem? Maybe with that we can see if there's an easy solution – Oscar Vicente Perez Jul 14 '16 at 07:58
  • What kind of commands? What if you close the older ones and open a new one? I think if you open the process from python you can send multiple commands to the terminal. Maybe this help: http://stackoverflow.com/questions/3730964/python-script-execute-commands-in-terminal If this is the way you want to go i can make an answer for you – Oscar Vicente Perez Jul 14 '16 at 08:06
  • My needs are daily open 3 terminals to excute three different commands(they are keeping running....). My problem is how to close old terminals? Otherwise there are more more terminals exist! – maple Jul 14 '16 at 08:08
  • Well, give me some minutes and I will answer this question with some examples. – Oscar Vicente Perez Jul 14 '16 at 08:10
  • Can I use the retrun object of function subprocess.Popen to close the old terminals? Thanks a lot~ – maple Jul 14 '16 at 08:10
  • See my answer and if it helps you – Oscar Vicente Perez Jul 14 '16 at 08:43

1 Answers1

0

You can execute your commands with a subprocess:

import subprocess
import time

process = subprocess.Popen("echo 'start' & sleep 60 & echo 'stop'", shell=True)
time.sleep(60) # Maybe you want a timer...

The you have two options of closing, use terminate or kill methods in the Popen returned object or simulate a Ctrl. + C

import subprocess
import time

process = subprocess.Popen(cmd, shell=True)
time.sleep(5)
process.terminate() # Or kill

Simulate de ctrl + c:

import subprocess
import time
import os
import signal

process = subprocess.Popen(cmd, shell=True)
time.sleep(5)
os.kill(process.pid, signal.SIGINT) # Ctrl. + C

If you want to get the output you can use:

process.communicate()[0].strip()

Which gives you a string.

If you want a console GUI, you can use a command like:

gnome-terminal -x sh yourCommand

Or the equivalent for the terminal you have installed.