Python X Library

0.33 · active · verified Fri Apr 10

python-xlib is a Python interface to the X11 Protocol, allowing Python programs to interact with the X Window System. It provides low-level access to Xlib functions and protocol extensions for X client programming. The current version is 0.33, and the library is actively maintained with releases addressing bugs and adding support for various X extensions.

Warnings

Install

Imports

Quickstart

Establishes a connection to the default X server, retrieves the root window's geometry, and queries the current mouse pointer position. This basic example demonstrates how to connect to the X server and perform a simple X request. Ensure the `DISPLAY` environment variable is correctly configured and an X server is running.

import Xlib.display

try:
    # Connect to the default X server
    # The DISPLAY environment variable typically specifies the X server.
    # For remote connections, ensure X forwarding is set up or xhost permissions are granted.
    display = Xlib.display.Display()

    # Get the root window of the default screen
    root = display.screen().root

    # Print its geometry (x, y, width, height, border_width, depth)
    geometry = root.get_geometry()
    print(f"Root window geometry: {geometry}")

    # Example: Query the current mouse pointer position
    pointer_info = root.query_pointer()
    print(f"Mouse pointer at: ({pointer_info.root_x}, {pointer_info.root_y})")

finally:
    # Always close the display connection to release resources
    if 'display' in locals() and display:
        display.close()

view raw JSON →