python pyobjc的例子。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pyobjc的例子。相关的知识,希望对你有一定的参考价值。

#!/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()

以上是关于python pyobjc的例子。的主要内容,如果未能解决你的问题,请参考以下文章

python macOS PyObjc Computername

python PyObjc系统Icon.py

python 在OS X上强制使用pyobjc自动发现时区

使用 Esky 冻结/打包 Cocoa PyObjC Python 应用程序

python 使用PyObjC和NSWorkspace设置桌面图片。我是个伪君子!

PyOBJC MLMediaLibrary