标签: pyobjc

如何在Snow Leopard中使用Python捕获iSight帧?

我有以下PyObjC脚本:

from Foundation import NSObject
import QTKit
error = None
capture_session = QTKit.QTCaptureSession.alloc().init()
print 'capture_session', capture_session
device = QTKit.QTCaptureDevice.defaultInputDeviceWithMediaType_(QTKit.QTMediaTypeVideo)
print 'device', device, type(device)
success = device.open_(error)
print 'device open success', success, error
if not success:
    raise Exception(error)
capture_device_input = QTKit.QTCaptureDeviceInput.alloc().initWithDevice_(device)
print 'capture_device_input', capture_device_input, capture_device_input.device()
success = capture_session.addInput_error_(capture_device_input, error)
print 'session add input success', success, error
if not success:
    raise Exception(error)
capture_decompressed_video_output = QTKit.QTCaptureDecompressedVideoOutput.alloc().init()
print 'capture_decompressed_video_output', capture_decompressed_video_output
class Delegate(NSObject):
    def captureOutput_didOutputVideoFrame_withSampleBuffer_fromConnection_(self, captureOutput, videoFrame, sampleBuffer, connection):
        print videoFrame, sampleBuffer, connection
delegate = …
Run Code Online (Sandbox Code Playgroud)

python isight pyobjc qtkit osx-snow-leopard

5
推荐指数
1
解决办法
2665
查看次数

使用PyObjC访问iPhone加速度计

我想通过PyObjC访问我的iPhone的Accelerometer.这是我的代码:

@objc.signature("v@:@@")
def accelerometer_didAccelerate_(self, accelerometer, acceleration):
    msgBox = UIAlertView.alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles_("Acceleration", str(acceleration.x), self, "OK", None)
    msgBox.show()
    msgBox.release()

@objc.signature("v@:@")
def applicationDidFinishLaunching_(self, unused):
    self.accelerometer = UIAccelerometer.sharedAccelerometer()
    self.accelerometer.setUpdateInterval_(1)
    self.accelerometer.setDelegate_(self)
    #...
Run Code Online (Sandbox Code Playgroud)

问题是str(accelleration.x)返回"<native-selector x of <UIAcceleration: 0x.....>>".我能做什么?

iphone pyobjc accelerometer uiaccelerometer

5
推荐指数
1
解决办法
857
查看次数

应用程序启动时显示NSPopover

我正在使用Pyobjc来创建NSStatusItem.单击它时,我显示的是NSPopOver.这工作正常.但是,我的要求是在应用程序启动时立即显示弹出框,而不会由用户执行任何操作.直接在finishLaunching中调用回调不起作用.有没有办法实现这个目标?即使可以模拟NSStatusView上的点击,它也会很好.

class TestApp(NSApplication):

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

        # self.statusItemClicked_(None)

    def statusItemClicked_(self, notification):
        self.viewController = SimpleXibDemoController.alloc().initWithWindowNibName_("Login")

        # Show the window
        self.viewController.showWindow_(self.viewController)
        rect = self.statusitem.valueForKey_('button').frame()
        self.viewController.popover.showRelativeToRect_ofView_preferredEdge_(rect, self.statusitem.valueForKey_('button'), NSMaxYEdge)
Run Code Online (Sandbox Code Playgroud)

pyobjc objective-c nsstatusitem nspopover

5
推荐指数
1
解决办法
462
查看次数

使用PyObjC以正常方式打印python异常

我收到这样的错误:

2010-07-13 20:43:15.131 Python [1527:60f] main:抓到OC_PythonException :: LoginMenuSet实例没有属性'play_sound'

这是代码:

@try {
    [section loop]; //Loop through section
} @catch (NSException *exception) {
    NSLog(@"Caught %@: %@", [exception name], [exception reason]);
}
Run Code Online (Sandbox Code Playgroud)

我想通过回溯和其他一切正常打印python异常.

谢谢.

python pyobjc exception-handling exception objective-c

4
推荐指数
2
解决办法
551
查看次数

什么是PyObjC?

我理解PyObjC的概念,但无法找到有关它究竟是什么或如何开始使用它的任何信息.

它是否像转换器一样,您输入python文件并获得目标c?或者它是一个库,您可以导入到您的目标c文件,让你在其中编写python?

或者它完全是另一回事?

如果有人可以提供如何处理它的提示,概述它是如何工作的,或者只是一些有关它的hello世界的指示,我将非常感激.

python pyobjc language-binding objective-c

4
推荐指数
1
解决办法
558
查看次数

PyObjC 并返回“输出”参数(即 NSError **)

我正在实现一个 ObjC 协议作为 PyObjC 类的混合。

该协议定义了一个“out”参数。

我找不到任何关于实现定义它的 ObjC 协议的 Python 类的行为的好的文档。

我找到了这个邮件列表线程,但那里的建议不起作用。他们说返回一个 Python 列表,其中方法的返回值作为第一项,输出参数作为第二项。

我试过这个,我得到的只是从 ObjC ( <type 'exceptions.ValueError'>: depythonifying 'char', got 'tuple')调用时的一个例外。

似乎 PyObjC 在 depythonifying 方法参数中严格遵守 ObjC 协议,这很好,但它并没有帮助我尝试修改输出参数。

这是 ObjC 协议:

#import <Foundation/Foundation.h>

@protocol TestProtocol <NSObject>

- (BOOL)testMethod:(NSError **)error;

@end
Run Code Online (Sandbox Code Playgroud)

这是实现此协议的 Python 类:

from Foundation import *
import objc

NSObject = objc.lookUpClass("NSObject")
TestProtocol = objc.protocolNamed("TestProtocol")

class TestClass(NSObject, TestProtocol):

    def testMethod_(self, error):
        return True, None
Run Code Online (Sandbox Code Playgroud)

问题:如何在 Python 中返回 ObjC 输出参数?

cocoa pyobjc

4
推荐指数
1
解决办法
1272
查看次数

Python捕获OS X上的文本文件中的击键值

我正在尝试监视Macbook上的击键,以便构建统计分析器.但是我怎样才能将这些字符与"事件"隔离开来,这更像是:

NSEvent:type = KeyDown loc =(850,248)time = 66551.8 flags = 0x100 win = 0x0 winNum = 0 ctxt = 0x0 chars ="l"unmodchars ="l"repeat = 0 keyCode = 37

所以有人知道如何根据下面发布的脚本,使用chars(来自NSEvent)的值来实现一个.txt doc吗?我需要一个带有按键的文本文件,以便在其上运行我的其他脚本并分析频率等...

提前致谢 ;)

#!/usr/bin/python
# -*- coding: utf-8 -*-


from AppKit import NSApplication, NSApp
from Foundation import NSObject, NSLog
from Cocoa import NSEvent, NSKeyDownMask
from PyObjCTools import AppHelper

class AppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, notification):
        mask = NSKeyDownMask
        NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, handler)

def handler(event):
    try:
        print event
    except KeyboardInterrupt:
        AppHelper.stopEventLoop()

def main():
    app = NSApplication.sharedApplication() …
Run Code Online (Sandbox Code Playgroud)

python macos pyobjc

4
推荐指数
1
解决办法
6184
查看次数

BWSplitView和PyObjc

我正在尝试在Cocoa PyObjc项目中使用来自BWToolkit的Brandon Walkin的BWSplitView.当我运行该项目时,我收到以下错误消息:

NSInvalidUnarchiveOperationException - *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (BWSplitView)
Run Code Online (Sandbox Code Playgroud)

这是否意味着他的工具包与PyObc项目不兼容,所以我应该只使用默认的界面构建器视图?BWToolkit似乎非常适合我的程序,我打算在我的界面的其他地方使用它.

python cocoa pyobjc bwtoolkit

3
推荐指数
1
解决办法
726
查看次数

计算当前周的第一天和最后一天

参考预先回答的问题:获取本周的第一天和最后一天

上面的链接有两个答案.其中一个是理论上的,另一个是PyObjC(Python-Objective C桥接语言),快速谷歌搜索确认PyObjC不适用于iPhone SDK.

因此,关于这个问题,如何将PyObjC代码翻译为与iPhone SDK兼容.

目标:假设今天(星期二)是19日,太阳.是第17次(周开始)和周六.23日是周末.我希望得到一个像19/01 - 23/01这样的字符串[即周的开始(hypen)周结束]

iphone pyobjc date objective-c nsdate

3
推荐指数
1
解决办法
6661
查看次数

PyObjC“此应用程序不允许通知”

我正在尝试测试一个简单的 Python 脚本来发送 macOS 通知:

import UserNotifications

def notif_callback(err):
    print("Error in notification callback:",err)

def auth_callback(granted, err):
    print("Granted: ",granted,)
    print("Error in authorization request: ",err)

content=UserNotifications.UNMutableNotificationContent.alloc().init()
content.setTitle_("Test")
r=UserNotifications.UNNotificationRequest.requestWithIdentifier_content_trigger_('test_notification',content,None)


c=UserNotifications.UNUserNotificationCenter.currentNotificationCenter()

c.requestAuthorizationWithOptions_completionHandler_(0b111111,auth_callback)

c.addNotificationRequest_withCompletionHandler_(r,notif_callback)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行该程序时,出现以下错误

Granted: False
Error in authorization request:  Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}
Error in notification callback: Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}
Run Code Online (Sandbox Code Playgroud)

我没有看到我的系统有任何通知授权,操作系统似乎自动拒绝了该请求。在系统偏好设置中,Python已被授予所有通知权限。我在这里缺少什么?

pyobjc objective-c python-3.7 macos-mojave

3
推荐指数
1
解决办法
865
查看次数