Python 3.x Interaction with other Program GUIs - python

I'm looking for a Python 3.x library that is able to allow interaction with other programs.
For example, I already have some sort of command-line interface which I have developed in python, and I
want to be able to enter, say "1", and have another program open. From here, I wish to hit another
input like "2" and have it manipulate the GUI that opens (for example, for it to "click" the Configurations
dropdown bar and select an option, perhaps modify a few settings, apply, and then possibly also automatically
enter some text). The reason I'm doing this is for test automation.
I've already tried using pywinauto, but I've found it to not be compatible for Python 3! :(
Is there another possible approach to this? Thanks in advance!!!
P.S. I may have forgotten to mention that I'm using Windows 7 but with Python32

You could look into sikuli. It lets you automate clicks and other actions based on region or matched graphic. Fairly smart. Is there a reason you're dead set on using py3?

Py3-compatible pywinauto released! New home page: http://pywinauto.github.io/
P.S. I'm maintainer of pywinauto.

Late answer, but have a look at pyautogui which enables you to move the mouse and press keys. I used it for the following snippet which launches an emulator and presses keys.
import pyautogui as pg
import os
import time
game_filepath = "../games/BalloonFight.zip"
os.system(f"fceux {game_filepath} &")
time.sleep(1)
keys_to_press = ['s', 's', 'enter']
for key_to_press in keys_to_press:
pg.keyDown(key_to_press)
pg.keyUp(key_to_press)
time.sleep(2)
im = pg.screenshot("./test.png", region=(0,0, 300, 400))
print(im)
A more detailed expalanation can be found here: Reinforcement learning to play Nintendo NES games

I created a pywinauto fork on GitHub that's compatible with Python 3:
https://github.com/Usonaki/sendkeys-py-si-python3
I only did basic testing, so there might still be some circular import related problems that I haven't found.

Related

Clicking on NOT-focused window in python

Well the problem is rather easy to understand. When playing a game, sometimes I need to use autoclicker. So in order to be able to watch some videos on youtube I need to use another monitor & autoclicker.
I wanted to create such autoclicker that would let me send any keyboard/mouse actions to specific window or even process (if it's possible). However I have no idea where to start?
There is a python library called pyautogui you can us it to automate stuff.
Installation
pip install pyautogui
your code should look simething like this
import time
import pyautogui
for ctr in range(10):
pyautogui.click()
time.sleep(1000)
Adjust the time based on requirement and refer this for more info about the library
Better to use power automate to make things simple

Can a windows form window be targeted by python?

all.
I'm trying to make a python script that fills 2 fields and clicks a button in a Windows Form (written in C#). What are relevant libraries that i can use to achieve this?
As per James comment, PyAutoGUI is the way to go! Thank you James
Edit: Upon doing further research, there seems to be another library called PyWinAuto. Here is a comparison between the two.

Python simulate media keys

How to make Python (3.7) manipulate any media player currently working on Windows?
I want to get functionality similar to media keys on keyboard, for example:
play_pause.py script which will play or pause music on Spotify or movie in media player (whatever is currently playing).
play_next.py script which will play next song/movie etc.
To clarify: I don't want Python to virtually press actual media keys on keyboard. I would like to get the functionality of such keys so it might work even without keyboard connected to PC.
The easiest solution is to use win32api.keybd_event from pywin32.
For example, install pywin32:
pip install pywin32
And try play/pause - should work without keyboard:
import win32api
from win32con import VK_MEDIA_PLAY_PAUSE, KEYEVENTF_EXTENDEDKEY
win32api.keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENDEDKEY, 0)
Virtual-Key Codes: here and here.
NOTE:
At this link about keybd_event function, you can see message: "Note This function has been superseded. Use SendInput instead".
So if you want/need to use SendInput, you probably need to use ctypes. I suggest you to check the example here. I've tried that code too and it works. If you need any further help, let me know.

better way to automate mouse&keyboard using pyautogui

I wrote a script using pyautogui that should start an program (an IDE) and then start using it.
This is the script so far:
#! python3
# mouseNow.py - Displays the mouse cursor's current position.
import pyautogui, sys, subprocess
from time import sleep
x,y = 1100,550
subprocess.call([r'C:\...exe', arg1, arg2])
pyautogui.click(x,y)
sleep(5) # 2 sec should suffice but this is for safety
pyautogui.typewrite(my_string)
pyautogui.press('enter')
This works well but I want to be portable. The x,y values were determined by where the program prompt appears on screen after I start the program, but this is not portable, I think. Is there a way to point the mouse to the prompt without giving const parameters? something like move_mouse_to_window_of_this_process_after_starting_it()
Also, I use sleep() so I would write the data to the window after it appears, but I guess it's not a good way (some PC will run this much slower, I guess), so is there a way to know when the prompt appeared and then do the pyautogui.typewrite(my_string)?
EDIT: I found a simple solution for the move_mouse_to_window_of_this_process_after_starting_it()
:
>>> pyautogui.hotkey('alt', 'tab')
If you need portable and reliable solution, you have to find a library that supports accessibility technologies to access GUI elements by text. Basic technologies are:
Win32 API, MS UI Automation (Windows)
AT-SPI (Linux)
Apple Accessibility API (MacOS)
There are several open-source GUI automation libraries supporting some of these technologies (usually 1 or 2). Python solutions:
pywinauto on Windows (both Win32 API & MS UIA, see Getting Started Guide)
pyatspi2 on Linux
pyatom on MacOS
There is also a thread on StackOverflow regarding hard sleeps vs flexible waiting.
Enjoy! :)
The way you are interacting with the .exe excludes alternatives to coordinates or blind firing (Tab, Tab, Enter etc..).
If the application has an API, you could interact with it programatically.
If it doesn't you can only try to match the location for x screen resolutions, and this only if the GUI is used in Fullscreen/windowed Fullscreen.

Python interactive CLI application?

I'm not even sure what these would be called? I used to write them in ADA running on VAX-VMS!
I want to make a simple menu-driven application that would let me display menus and use the cursor keys to navigate around them, choose items and navigate around the app. All fully old school text based.
I want to be able to take over the whole terminal window and display stuff in different places.
Is this possible in Python?
Check out the Python HOWTO on Curses Programming with Python and here is the library reference.
Another easy to use library is Urwid - Console User Interface Library.
http://excess.org/urwid/
http://excess.org/urwid/examples.html
Yes, have a look at the different curses implementations.

Categories

Resources