Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interact with a window's GUI with Python?

Let's say you want to open myapp.exe, open the 3rd menu, then choose the 2nd menu item (i.e. like a user would do with the keyboard or mouse), and then in the dialog window, choose the 2nd button.

pyahk and pyautogui seem to offer this, but in a rather "low-level" way, by simulating clicks:

pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')

How to do interact with a Windows GUI in a higher-level way with Python?

Example:

window = gui.open('myapp.exe')
window.menu_open(3).choose_item(2)
child_window = window.wait_for_dialog()
child_window.buttons[1].click()
like image 828
Basj Avatar asked Feb 22 '19 18:02

Basj


People also ask

Can Python interact with GUI?

There are many graphical user interface (GUI) toolkits that you can use with the Python programming language. The big three are Tkinter, wxPython, and PyQt. Each of these toolkits will work with Windows, macOS, and Linux, with PyQt having the additional capability of working on mobile.

How does Python connect to GUI code?

Tkinter ProgrammingImport the Tkinter module. Create the GUI application main window. Add one or more of the above-mentioned widgets to the GUI application. Enter the main event loop to take action against each event triggered by the user.

Is Python good for GUIs?

While being incredibly useful for the fields of data science and machine learning, Python is also great for developing graphical user interfaces! In fact, it has many frameworks that even beginners can use to easily get started with developing a GUI.

How do I open one GUI from another GUI in Python?

You can't "call" another GUI. If this other GUI creates its own root window and calls mainloop() , your only reasonable option is to spawn a new process. That's a simple solution that requires little work. The two GUIs will be completely independent of each other.


Video Answer


1 Answers

pywinauto seems to be much more in-line with what you want - it utilizes the Win32 API and MS UI Automation among other things.

Here is an example of automation of the notepad application:

from pywinauto.application import Application
app = Application().start("notepad.exe")

app.UntitledNotepad.menu_select("Help->About Notepad")
app.AboutNotepad.OK.click()
app.UntitledNotepad.Edit.type_keys("pywinauto Works!", with_spaces = True)
like image 63
Random Davis Avatar answered Sep 30 '22 04:09

Random Davis