Skip to content

Instantly share code, notes, and snippets.

@orklann
Forked from uchcode/hello_world.py
Created May 22, 2018 09:37
Show Gist options
  • Save orklann/f85afb56c7ec72639e0db775e0951188 to your computer and use it in GitHub Desktop.
Save orklann/f85afb56c7ec72639e0db775e0951188 to your computer and use it in GitHub Desktop.
pyobjc example.
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
NSWindowDelegate = objc.protocolNamed("NSWindowDelegate")
class MainMenu(NSMenu):
def init(self):
self = objc.super(MainMenu, self).init()
if self is None: return None
a = NSMenu.alloc().init()
a.addItemWithTitle_action_keyEquivalent_("Quit", "terminate:", "q")
i = NSMenuItem.alloc().init()
i.setSubmenu_(a)
self.addItem_(i)
return self
class AppWindow(NSWindow, NSWindowDelegate):
def initWithFrame_(self, frame):
r = frame
s = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
b = NSBackingStoreBuffered
d = False
self = objc.super(AppWindow, self).initWithContentRect_styleMask_backing_defer_(r, s, b, d)
if self is None: return None
self.setDelegate_(self)
return self
def windowWillClose_(self, sender):
NSApp.terminate_(sender)
def Window(origin, size):
w = AppWindow.alloc().initWithFrame_((origin, size))
return w
def Button(origin, size):
b = NSButton.alloc().initWithFrame_((origin, size))
b.setBezelStyle_(NSRoundedBezelStyle)
return b
def Label(origin, size):
t = NSTextField.alloc().initWithFrame_((origin, size))
t.setFont_(NSFont.systemFontOfSize_(14))
t.setAlignment_(NSCenterTextAlignment)
t.setDrawsBackground_(False)
t.setBordered_(False)
t.setEditable_(False)
t.setSelectable_(True)
return t
def Field(origin, size):
t = NSTextField.alloc().initWithFrame_((origin, size))
return t
class UserInterface(NSObject):
def init(self):
self = objc.super(UserInterface, self).init()
if self is None: return None
mainMenu = MainMenu.alloc().init()
appDelegate = AppDelegate.alloc().init()
window = Window((0,0), (300,200))
window.center()
window.setTitle_("Hello")
label = Label((10,100), (280,22))
label.setStringValue_("hello python")
button = Button((200,10), (90,26))
button.setTarget_(appDelegate)
button.setAction_("clickme:")
button.setTitle_("Click Me")
window.contentView().addSubview_(label)
window.contentView().addSubview_(button)
window.setDefaultButtonCell_(button.cell())
self.window = window
self.mainMenu = mainMenu
self.appDelegate = appDelegate
self.appDelegate.window = self.window
return self
class AppDelegate(NSObject):
window = objc.IBOutlet()
@objc.IBAction
def clickme_(self, sender):
print("clicked")
def applicationDidFinishLaunching_(self, notification):
self.window.makeKeyAndOrderFront_(None)
def applicationShouldHandleReopen_hasVisibleWindows_(self, app, flag):
self.window.makeKeyAndOrderFront_(None)
return True
app = NSApplication.sharedApplication()
nib = UserInterface.alloc().init()
app.setMainMenu_(nib.mainMenu)
app.setDelegate_(nib.appDelegate)
app.activateIgnoringOtherApps_(True)
AppHelper.runEventLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment