I have my own application on python 2.7. I want to control an outside .exe application.
I'm able to launch such an application, klm.exe as:
from win32com.client import *
ExtApp = Dispatch("Wscript.Shell")
ExtApp.Run("E:\XYZ\ABC\klm")
But I want to have full control of this outside .exe application as it has tabs, radio buttons, push buttons, etc.
Is there a way to do so?
But I want to have full control of this outside .exe application as it has tabs, radio buttons, push buttons, etc.
Is there a way to do so?
Yes, multiple ways, depending on the application.
Since you're already using COM (although I'm not sure why you're using it just to launch apps)… does the app have a COM automation (IDispatch) interface? If so, there will probably be documentation showing how to use it from VB# (or VBScript or C# or …), which you can easily adapt to Python and win32com. (For an example of such an application, see the Outlook automation docs.)
If there's no COM automation interface, there may still be a lower-level COM interface, which is almost as easy to use via win32com, but it usually won't provide any access to the GUI controls; instead, you'll be talking to the same lower-level functionality that the GUI uses. (For a good example, see Apple's iTunes COM Interface.)
If there's no COM support at all, the simplest thing to do is to automate it via Windows WM_* events. There are some examples of doing that in the pywin32 documentation, but there are also a lot of higher-level wrappers, like AutoPy and pywinauto/swapy, and so on that will make things a whole lot easier. There are dozens of these, free and commercial, and even more if you're willing to step outside of Python and use a different scripting system, and SO is not a good place to discuss the pros and cons of each.
Finally, you can always ignore the app's windows and just automate the system mouse… but this is almost always a silly thing to do.
Related
Is it possible to create a user interface without the help of python framework (like tinker or pygame) and use only vanilla python code? If yes, how?
Can you briefly explain how python framework works?
Is the code of different python framework different?
If the computer did not have the framework installed, will the program still runnable if the program uses a framework?
Thanks very much
Yes, after all tinker and pygame are just python classes packaged as modules.
Python frameworks are a bunch of pre-tested and reusable modules that allow you to use and extend upon so you don't have to reinvent the wheel.
Yes, frameworks will have differences in usability and code.
The computer will always need the dependencies, though you can package these in various ways aka create a package that has all your dependencies for the program to run.
If you want as few external dependencies as possible (but still a GUI) I would strongly suggest using a Web-Microframework like bottle (single file) and utilize the user's browser for rendering.
You can make a GUI without any external framework with HTML by setting up a webserver and using the user's browser to render it.
For a browser-GUI without an external Framework: Depending on whether you know JavaScript you can either use XML-RPC (xmlrpc.server+http.server with JS in the browser) or WSGI (wsgiref) (example on that page)
Yes, totally.
Of course the if you do not prepare for this case you cannot run a program without an integral part of it like a Framework - but you can distribute your program with the Framework included.
For XML-RPC
import xmlrpc.server
import http.server
class MyHandler(xmlrpc.server.SimpleXMLRPCRequestHandler,http.server.SimpleHTTPRequestHandler):
pass
This handler will serve files from the current working directory (your actual HTML-UI and JS for communication (there are several XMP-RPC libraries for JS)) but it can also be used like in the XML-RPC-Server example to glue your code and the UI together.
I have the application installed on my windows PC, I want to launch that application using python and select dropdown options and do some other activities in that application.
I was able to launch the application using the os.system command, but I am not able to proceed further.
I want my program to do things like:
* select from a dropdown menu
* click on a button
How can my application control the user interface of another application?
Normally, an application exposes a user interface (UI) for users, and an application programming interface (API) for programming.
A human being uses keyboard and mouse to work with the user interface (UI)
An application uses programming to work with the application programming interface (API)
The UI is designed for humans, and the API is designed for computers.
It is sometimes possible to use programming to control the user interface of another program -- so your program acts as if it were using the keyboard and mouse. This technique is often called "UI automation", and programs that do it are sometimes called "robots".
It's a big topic, and it can be quite complex. It's almost always better to use an API instead if you can: it's faster, simpler, more reliable.
If you do need to use UI automation, there are a few different tools that can help.
You are asking about Python, so here are a few UI automation tools that work with Python:
AutoIT is a standalone product, but you can use Python to script it.
PyWinAuto is designed for use from Python.
Sikuli uses computer vision to find parts of the screen. I believe it comes with a recording tool as well.
Just to repeat: UI automation is weird and hard. If you can possibly use an API instead, your life will be much easier.
You need to install pywinauto package
Try the following code to run the .exe file
from pywinauto import application
app = application.Application()
app.start("Notepad.exe")
here you are:
(with os ^_-)
import os
os.startfile('your exe file address')
I learned about them for GUIs, and they are indeed my favourite Python option for such purposes. However, they are so much more than that. They implement network sockets, media management, regular expressions, basic geometry, stuff like QVariant is basically a dynamic attribute handler...
I mean, it seems weird to just call them "GUI packages". It can be as little as a GUI, but it can also be pretty much an entire mask on top Python where you never use a naked function and rely on QObjects for everything.
Is there a name for this concept? Are there others that I should be aware about if I'm doing, say, scientific programming?
Frameworks, platforms, toolkits. PyQT calls itself a toolkit. DirectX is a full featured framework like you described, and it is intended to run on platforms like Windows and Xbox. I use the ExpressJS framework for web development, but most of it's core functionality is either in the NodeJS server, which is not officially referred to as a framework AFAIK; it exposes pretty much the whole OS through a javascript interface for server side interweb code. For scientific programming, Matlab and Octave both just call themselves languages, but I would call them platforms because they have a graphical interface and GUI building tools, and a whole ecosystem of modules, and because your code lives inside of their system. OpenGL, scipy and numpy are libraries, by contrast, because they are used as a component inside of your project. And then there are content management systems (CMS) like Wordpress and Drupal, enterprise resource planning systems (ERP) like Tryton, and probably a bunch of other sub-categories of the "framework" and "platform" categories of software.
All in all, it's not easy to categorize software. We need to standardise a taxonomy, and then make new standardized taxonomies to make up for deficiencies in the first one ;)
lately I have been trying to find a way to control applications on my ubuntu using python. What i want to achieve is something like what we can do with applescript in mac.... Control chrome send queries to it etc.
can someone please point me in the right direction as to how one can control applications using python, esp on Ubuntu...or a code snippet maybe?
Read about D-Bus: http://en.wikipedia.org/wiki/D-Bus
Python dbus: http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
In order to control a process, it must be connected using dbus in the first place and exporting services. This is the same idea of AppleScript. On a mac, an application has to be exporting services that can be controlled over applescript.
PyQt/PySide has a DBus module as well: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtdbus.html
Have you taken a look at Project Sikuli?
So I'm trying to create an automatic installer using Python. How would I go about interacting with native Windows applications that I launch? I am currently launching them using subprocess.call. Would I need to use Win32com? IronPython? Call a VB script from Python?
I've been trying to look into COM objects, but all I see are default Microsoft applications.
If the application supports COM, use COM using win32com. This tutorial helped me a lot. If it does not, you can use something like pywinauto or similar software. It is usually described as module for GUI testing. SendKeys comes handy for non-standard GUIs as a last resort.
Ranorex Spy is very helpful tool for identifying GUI elements.
Some of my colleagues also use compiled AutoHotkey scripts.