【问题标题】:PyObjC tutorial without Xcode [closed]没有 Xcode 的 PyObjC 教程 [关闭]
【发布时间】:2011-06-27 00:21:44
【问题描述】:

我正在编写一个小型跨平台 wxPython 应用程序,但是在每个平台上我都需要使用一些特定于平台的 API。在 Mac OS 上,它可以使用 PyObjC 完成。

我正在寻找有关如何使用 PyObjC 的教程。然而,到目前为止,我发现的只是 Xcode 教程。我希望我的应用程序能够在 mac/win/lin 上运行,无需更改,并且我不想在 Xcode 中开发它。有什么办法吗?

UPD。更具体地说,我需要从 Mac OS X 访问一些手写板事件,并且我想为此使用 PyObjC(我没有看到任何其他方式)。

【问题讨论】:

  • 我也有同样的问题。我也搜索过,但找不到合适的解决方案。我编写了一些 AppleScript 代码作为包装器,可以在 Mac 上使用我的应用程序。但是能够在没有 XCode 的情况下使用 PyObjC 会很好。

标签: python macos wxpython pyobjc


【解决方案1】:

您可以导入 Foundation 和 AppKit 模块,然后子类化 NSApplication。但是,如果您的 pyobjc 代码不是您的代码的入口点,那么这可能不是您想要的。可以提供更多关于你试图用 pyobjc 做什么的细节吗?

这是一个使用 pyobjc 制作简单状态栏应用程序的快速示例,无需使用 xcode:

import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper

class MyApp(NSApplication):

    def finishLaunching(self):
        # Make statusbar item
        statusbar = NSStatusBar.systemStatusBar()
        self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
        self.icon = NSImage.alloc().initByReferencingFile_('icon.png')
        self.icon.setScalesWhenResized_(True)
        self.icon.setSize_((20, 20))
        self.statusitem.setImage_(self.icon)

        #make the menu
        self.menubarMenu = NSMenu.alloc().init()

        self.menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Click Me', 'clicked:', '')
        self.menubarMenu.addItem_(self.menuItem)

        self.quit = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
        self.menubarMenu.addItem_(self.quit)

        #add menu to statusitem
        self.statusitem.setMenu_(self.menubarMenu)
        self.statusitem.setToolTip_('My App')

    def clicked_(self, notification):
        NSLog('clicked!')

if __name__ == "__main__":
    app = MyApp.sharedApplication()
    AppHelper.runEventLoop()

然后您可以使用 py2app 使其可分发:

from distutils.core import setup
import py2app

NAME = 'myapp'
SCRIPT = 'myapp.py'
VERSION = '0.1'
ID = 'myapp'

plist = dict(
     CFBundleName                = NAME,
     CFBundleShortVersionString  = ' '.join([NAME, VERSION]),
     CFBundleGetInfoString       = NAME,
     CFBundleExecutable          = NAME,
     CFBundleIdentifier          = 'com.yourdn.%s' % ID,
     LSUIElement                 = '1', #makes it not appear in cmd-tab task list etc.
)


app_data = dict(script=SCRIPT, plist=plist)

setup(
   app = [app_data],
   options = {
       'py2app':{
           'resources':[
               ],
           'excludes':[
               ]
           }
       }
)

【讨论】:

  • 哇,感谢这篇精彩的帖子!这正是我所需要的!
【解决方案2】:

您需要 Xcode 做什么?如果您需要它用于 windows/gui(*.nib、*.xib 文件),那么您或许应该搜索“creating *.nib, *xib without Xcode”。 这只是一个搜索提示,没有令人满意的答案。

【讨论】:

  • 我在intro 中找到了 2 种方法,并且没有说明您必须使用 Xcode。在底部有一些 py2appIDE 方法:Xcode。我希望这可以帮助你。怎么说呢,我不是专家。
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 2011-09-25
  • 2015-09-05
  • 2010-10-23
  • 2010-11-22
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多