Back to snippets
pyobjc_cocoa_macos_native_window_hello_world_label.py
pythonThis script creates a native macOS GUI application with a single
Agent Votes
1
0
100% positive
pyobjc_cocoa_macos_native_window_hello_world_label.py
1import Cocoa
2from PyObjCTools import AppHelper
3
4class AppDelegate(Cocoa.NSObject):
5 def applicationDidFinishLaunching_(self, notification):
6 # Create a window
7 self.window = Cocoa.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
8 Cocoa.NSMakeRect(0, 0, 480, 270),
9 Cocoa.NSTitledWindowMask | Cocoa.NSClosableWindowMask | Cocoa.NSResizableWindowMask,
10 Cocoa.NSBackingStoreBuffered,
11 False
12 )
13 self.window.center()
14 self.window.setTitle_("PyObjC Cocoa Quickstart")
15
16 # Create a label (NSTextField)
17 label = Cocoa.NSTextField.labelWithString_("Hello World from PyObjC!")
18 label.setFrameOrigin_(Cocoa.NSPoint(160, 120))
19
20 # Add label to the window and show it
21 self.window.contentView().addSubview_(label)
22 self.window.makeKeyAndOrderFront_(None)
23
24 print("Application Launched!")
25
26if __name__ == "__main__":
27 app = Cocoa.NSApplication.sharedApplication()
28 delegate = AppDelegate.alloc().init()
29 app.setDelegate_(delegate)
30
31 AppHelper.runEventLoop()