{"id":6154,"library":"pyobjc-framework-pencilkit","title":"PencilKit Framework for PyObjC","description":"pyobjc-framework-pencilkit provides Python wrappers for Apple's PencilKit framework on macOS. It is part of the PyObjC project, a bidirectional bridge enabling Python scripts to interact with Objective-C libraries, including macOS Cocoa frameworks. The current version is 12.1 and it maintains an active release cadence, typically aligning with macOS SDK updates and Python version support.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macos","framework","gui","apple","drawing","pencilkit"],"install":[{"cmd":"pip install pyobjc-framework-pencilkit","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core bridge functionality for PyObjC frameworks.","package":"pyobjc-core","optional":false},{"reason":"Provides core macOS GUI frameworks like AppKit and Foundation, necessary for most PyObjC applications and PencilKit UI integration.","package":"pyobjc-framework-Cocoa","optional":false}],"imports":[{"note":"PyObjC framework wrappers expose their classes directly under the framework's name, not the PyPI package name.","wrong":"from pyobjc_framework_pencilkit import PKCanvasView","symbol":"PKCanvasView","correct":"from PencilKit import PKCanvasView"},{"note":"Classes are imported directly from the top-level framework module.","wrong":"import PyObjC.PencilKit.PKToolPicker","symbol":"PKToolPicker","correct":"from PencilKit import PKToolPicker"}],"quickstart":{"code":"import AppKit\nimport Foundation\nimport PencilKit\nfrom PyObjCTools import AppHelper\n\nclass PyPencilKitDelegate(AppKit.NSObject):\n    def applicationDidFinishLaunching_(self, notification):\n        # Create a window\n        self.window = AppKit.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(\n            Foundation.NSRect((100, 100), (600, 400)),\n            AppKit.NSWindowStyleMaskTitled | AppKit.NSWindowStyleMaskClosable | AppKit.NSWindowStyleMaskResizable,\n            AppKit.NSBackingStoreBuffered,\n            False\n        )\n        self.window.setTitle_(\"PyObjC PencilKit Demo\")\n        self.window.setDelegate_(self)\n\n        # Create a PKCanvasView\n        self.canvasView = PencilKit.PKCanvasView.alloc().initWithFrame_(self.window.contentView().bounds())\n        self.canvasView.setAutoresizingMask_(AppKit.NSViewWidthSizable | AppKit.NSViewHeightSizable)\n        self.canvasView.setDrawingPolicy_(PencilKit.PKCanvasViewDrawingPolicyAnyInput) # Allow finger drawing\n\n        # Create and configure a PKToolPicker\n        self.toolPicker = PencilKit.PKToolPicker.alloc().init()\n        self.toolPicker.setVisible_forFirstResponder_(True, self.canvasView) # Make visible for the canvas\n        self.toolPicker.addObserver_(self.canvasView) # Canvas observes tool changes\n\n        self.window.contentView().addSubview_(self.canvasView)\n        self.window.makeKeyAndOrderFront_(None)\n\n        # Make canvas first responder to receive drawing input\n        self.window.makeFirstResponder_(self.canvasView)\n\n    def applicationShouldTerminateAfterLastWindowClosed_(self, sender):\n        return True\n\nif __name__ == \"__main__\":\n    app = AppKit.NSApplication.sharedApplication()\n    delegate = PyPencilKitDelegate.alloc().init()\n    app.setDelegate_(delegate)\n    AppHelper.runEventLoop()","lang":"python","description":"This quickstart demonstrates how to create a basic macOS application using PyObjC that displays a `PKCanvasView` and a `PKToolPicker` for drawing. It sets up a standard Cocoa application window and integrates PencilKit components, allowing for user interaction with an Apple Pencil or trackpad/mouse."},"warnings":[{"fix":"Upgrade your Python environment to Python 3.10 or later for PyObjC 12.x (including this package). Always check `requires_python` on PyPI or the PyObjC changelog for specific version requirements.","message":"PyObjC frequently drops support for older Python versions. PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped Python 3.8. Ensure your Python version is compatible with the PyObjC version you are installing.","severity":"breaking","affected_versions":">=11.0"},{"fix":"Review code that interacts with object initialization, especially custom subclasses or factory methods, to ensure correct reference handling. Explicit memory management is rarely needed in Python, but understanding the underlying Objective-C semantics is crucial for correct behavior.","message":"PyObjC 11.1 changed how initializer methods (`init` family) are modeled, now correctly reflecting that they 'steal' a reference to `self` and return a new one, as per clang's ARC documentation. This affects object lifecycle and reference counting.","severity":"breaking","affected_versions":">=11.1"},{"fix":"Only use `pyobjc-framework-pencilkit` in macOS environments. For cross-platform drawing or GUI development, consider alternative libraries.","message":"PyObjC is a macOS-specific library and will not install or run on other operating systems. The frameworks it wraps (like PencilKit) are Apple-proprietary.","severity":"gotcha","affected_versions":"all"},{"fix":"Always check for `None` before calling methods on PyObjC-wrapped objects that might originate from Objective-C `nil` values. E.g., `if my_obj is not None: my_obj.doSomething_()`.","message":"Unlike Objective-C, where sending a message to `nil` (equivalent to Python `None`) is a no-op, attempting to call a method on a Python `None` object (which PyObjC translates from `nil`) will raise an `AttributeError`.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure custom classes requiring KVO inherit from `AppKit.NSObject` or `Foundation.NSObject`. Use `Foundation.NSMutableArray` and `Foundation.NSMutableDictionary` for collections that will be observed.","message":"Key-Value Observing (KVO) is not supported for pure Python objects. If you need KVO compatibility for properties of Python objects, they must inherit from `NSObject`. For observable sequence or dictionary instance variables, use `NSMutableArray` or `NSMutableDictionary` instead of Python lists or dictionaries.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}