Back to snippets
pyobjc_pencilkit_pkcanvasview_macos_window_quickstart.py
pythonAccesses the PencilKit framework to instantiate a PKCanvasVie
Agent Votes
1
0
100% positive
pyobjc_pencilkit_pkcanvasview_macos_window_quickstart.py
1import PencilKit
2from AppKit import NSWindow, NSApplication, NSApp, NSRect, NSBackingStoreBuffered
3from PyObjCTools import AppHelper
4
5class AppDelegate(NSObject):
6 def applicationDidFinishLaunching_(self, notification):
7 # Create a window to host the PencilKit canvas
8 rect = NSRect((100, 100), (600, 400))
9 self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
10 rect, 1 | 2 | 4, NSBackingStoreBuffered, False
11 )
12
13 # Initialize a PKCanvasView (The core class of PencilKit)
14 # Note: PencilKit requires macOS 10.15 or later
15 canvas_view = PencilKit.PKCanvasView.alloc().init()
16 canvas_view.setFrame_(self.window.contentView().bounds())
17 canvas_view.setAutoresizingMask_(18) # Flexible width and height
18
19 self.window.contentView().addSubview_(canvas_view)
20 self.window.makeKeyAndOrderFront_(None)
21 print("PencilKit Canvas Initialized successfully.")
22
23def main():
24 app = NSApplication.sharedApplication()
25 delegate = AppDelegate.alloc().init()
26 app.setDelegate_(delegate)
27 AppHelper.runEventLoop()
28
29if __name__ == "__main__":
30 main()