WinRT Windows Foundation Collections

3.2.1 · active · verified Thu Apr 16

This package provides Python projections for the Windows Runtime (WinRT) APIs specifically within the `Windows.Foundation.Collections` namespace. It enables Python developers to interact with WinRT collection types like maps, vectors, and property sets. The current version is 3.2.1, with releases occurring frequently, often tied to Windows SDK updates and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating and interacting with a `PropertySet`, a common mutable WinRT map-like collection. It shows how to insert items, retrieve them, and iterate over the collection using both WinRT-specific methods and Python's dictionary-like interface.

from winrt.windows.foundation.collections import PropertySet
from winrt.windows.foundation import Uri

# PropertySet is a concrete WinRT collection type (IMap<str, object>)
properties = PropertySet()
properties.Insert("myString", "Hello WinRT")
properties.Insert("myNumber", 123)
properties.Insert("myUri", Uri("http://example.com/test"))

print(f"PropertySet size: {properties.Size}")
print(f"Value for 'myString': {properties.Lookup('myString')}")
print(f"Value for 'myUri': {properties.Lookup('myUri')}")

# PropertySet also supports Python dictionary-like access and iteration
properties["anotherKey"] = "anotherValue"

print("\nIterating through PropertySet:")
for key, value in properties:
    print(f"  {key}: {value}")

# Check if a key exists
if properties.HasKey("myNumber"):
    print(f"'myNumber' exists with value: {properties['myNumber']}")

view raw JSON →