Skip to main content

XWindows

Linux python client for Xserver to access windows information and automate keypresses

Table of contents


About the package

I created this package mainly to solve some problems I faced using opencv on linux.

Problems


  • Obtaining Window screen in fast and efficient way
    • Every time we want to capture screen we have to take screen shot with 3rd party packages like pyautogui and then convert into opencv image which is both slow and inefficient.
  • No modules like pywin32 for linux to access internal apis

Solutions


  • This package provides native Xserver interaction from python using python-xlib package.
  • This package provides the following features
    • Get active windows (active means all the visual application windows)
    • Get geometry of a particular window (height, width, x, y coordinates)
    • Get screenshot of a particular window in raw bytes or PIL image format
    • Automate keypresses

Since all these operations are done using native apis you will experience increase in performance

Resources

  • native xlib documentation
    • This documentation is written for C / C++ language and you need to have a bit of knowledge to understand this documentation.
    • No availability of lot of examples makes it even harder for beginners to understand the documentation.
  • python-xlib documentation
    • The documentation is still not completed but the devs are updating it continuosly.
  • My Notes
    • I have noted down some important topics to understand XServer and xlib library documentation. Hope it helps :)

Documentation

Installation

pip install XWindows

If you use python3, then use following command

pip3 install XWindows

Importing

import XWindows

# If you want to import seperate components

from XWindows import windows
from XWindows import keys

# If you want to import seperate components and functionality
# This way of importing is preffered cause you don't have to add whole namespace again and again
# This will be used to explain the features further

from XWindows.windows import Windows
from XWindows.keys import Keys

Initialising

# intialising windows object
windows = Windows()

# dispose windows object
windows.dispose()

Methods

Get Active windows

  • Here active indicates the visual application windows
activeWindows = windows.getActiveWindows()
print(activeWindows)

# The result will be list of tuples of (window_id, window_title)
# [
#   (29360135, 'firstPy'), 
#   (33554442, 'python3'), 
#   (39845895, b'windows.py - firstPy - Visual Studio Code'), 
#   (39845920, b'test.py - XWindows - Visual Studio Code'), 
#   (52428803, b'Linux get Window (python) \xe2\x80\x94 Mozilla Firefox')
# ]

# These window_id's change everytime you close and open a particular window

Get Window geometry

# Syntax windows.getWindowGeometry(window_id)

geometry = windows.getWindowGeometry(29360135)
print(geometry)

# returns a tuple of (x, y, width, height)
# (302, 141, 1375, 691)

Get Window Screen

# syntax: windows.getWindowScreen(window_id, bytes_format=False)
# syntax: windows.getWindowScreen(window_id, bytes_format=True) for raw data

# returns image in PIL Image format
screen = windows.getWindowScreen(29360135)

# returns tuple of (image in bytes, image width, image height)
screen_bytes_format = windows.getWindowScreen(29360135, bytes_format=True)

Get Window Slice

  • Use this to get certain portion of a screen
# syntax: windows.getWindowSlice(window_id, x, y, width, height)
# syntax: windows.getWindowSlice(window_id, x, y, width, height, bytes_format=True)

window_id = 29360135
(x, y, width, height) = windows.getWindowGeometry(window_id)

# returns data in PIL Image format
window_slice = windows.getWindowSlice(window_id, x, y, width, height)

# return image data in bytes format
window_slice_bytes_format = windows.getWindowSlice(window_id, x, y, width, height)

Press Key

# syntax: windows.pressKey([keysyms,], window_id)
# all keysyms are accessible using windows.keys

# this below sends A keys pressed signal to window with id window_id
windows.pressKey([windows.keys.A], window_id)
  • [keysyms,] - list of keys you want to simulate
  • send the keys in the order you would press in keyboard
  • for example [windows.keys.A,] = [windows.keys.Shift_L, windows.keys.a]

Author

sleepingsaint @github

Contributions or PR's of any kind are appreciated

Features Planned to add

[ ] Automate mouse movements
[ ] Get windows with title rather than window_id

Metadata

Release files for XWindows 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for XWindows 0.0.7
File Size Uploaded
XWindows-0.0.7.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for XWindows 0.0.7
File Interpreter ABI Platform
XWindows-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / XWindows-0.0.7.tar.gz

Download URL XWindows-0.0.7.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
58c6e15a581f2cb665eb8984dc9894e8448f4416cde04d6d8d159603aeea6f8f
BLAKE2b-256 checksum
How to use checksums
f5fc2e153036ef876c5f91528cac4e40df95035731beb4d7ee7228bb0ff9ca28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.5

Release files / XWindows-0.0.7-py3-none-any.whl

Download URL XWindows-0.0.7-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7307f138fce21305eb937d1b00488905c84a057def7daaa8c8558f0a554bab2a
BLAKE2b-256 checksum
How to use checksums
3be98b552ca156a1f2d408b0b6f27428e98ec94ae39088444f0461ed79c90f44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page