我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用AppKit.NSImage.alloc()。
def __init__(self, dimensions): super(DashedRectangle, self).__init__(dimensions, scale="none") box_width = self.getPosSize()[2] box_height = self.getPosSize()[3] line_image = NSImage.alloc().initWithSize_((box_width, box_height)) line_image.lockFocus() path = NSBezierPath.bezierPathWithRoundedRect_xRadius_yRadius_(NSMakeRect(.5, .5, box_width - 1, box_height - 1), 5, 5) path.setLineWidth_(1.0) path.setLineDash_count_phase_((5, 5), 2, 0) NSColor.colorWithCalibratedWhite_alpha_(0, 0.1).set() path.stroke() line_image.unlockFocus() self.setImage(imageObject=line_image)
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}, is_error=False): icon = NSImage.alloc().initByReferencingFile_( os.path.join(ICON_PATH, 'douban.png')) notification = NSUserNotification.alloc().init() notification.setTitle_(title) notification.setSubtitle_(subtitle) notification.setInformativeText_(info_text) notification.setUserInfo_(userInfo) notification.set_identityImage_(icon) if is_error: error_image = NSImage.alloc().initByReferencingFile_( os.path.join(ICON_PATH, 'error.png')) notification.setContentImage_(error_image) if sound: notification.setSoundName_('NSUserNotificationDefaultSoundName') notification.setDeliveryDate_( Foundation.NSDate.dateWithTimeInterval_sinceDate_( delay, Foundation.NSDate.date())) NSUserNotificationCenter.defaultUserNotificationCenter( ).scheduleNotification_(notification)
def add(self, item, index=None): image_path = os.path.join(self.resource_path, item.icon) image = NSImage.alloc().initByReferencingFile_(image_path) new_item = dict( itemIdentifier = item.identifier, label = item.label, callback = item.dispatch, imageObject = image ) self.items.insert(index, new_item)
def __init__(self, dimensions, alpha=1): super(Background, self).__init__(dimensions, scale="fit") colorTile = NSImage.alloc().initWithSize_((10, 10)) colorTile.lockFocus() color = NSColor.colorWithCalibratedWhite_alpha_(0, alpha) color.set() NSRectFillUsingOperation(((0, 0), (10, 10)), NSCompositeSourceOver) colorTile.unlockFocus() self.setImage(imageObject=colorTile)
def install_notifier(): """Extract `Notify.app` from the workflow to data directory. Changes the bundle ID of the installed app and gives it the workflow's icon. """ archive = os.path.join(os.path.dirname(__file__), 'Notify.tgz') destdir = wf().datadir app_path = os.path.join(destdir, 'Notify.app') n = notifier_program() log().debug("Installing Notify.app to %r ...", destdir) # z = zipfile.ZipFile(archive, 'r') # z.extractall(destdir) tgz = tarfile.open(archive, 'r:gz') tgz.extractall(destdir) assert os.path.exists(n), ( "Notify.app could not be installed in {0!r}.".format(destdir)) # Replace applet icon icon = notifier_icon_path() workflow_icon = wf().workflowfile('icon.png') if os.path.exists(icon): os.unlink(icon) png_to_icns(workflow_icon, icon) # Set file icon # PyObjC isn't available for 2.6, so this is 2.7 only. Actually, # none of this code will "work" on pre-10.8 systems. Let it run # until I figure out a better way of excluding this module # from coverage in py2.6. if sys.version_info >= (2, 7): # pragma: no cover from AppKit import NSWorkspace, NSImage ws = NSWorkspace.sharedWorkspace() img = NSImage.alloc().init() img.initWithContentsOfFile_(icon) ws.setIcon_forFile_options_(img, app_path, 0) # Change bundle ID of installed app ip_path = os.path.join(app_path, 'Contents/Info.plist') bundle_id = '{0}.{1}'.format(wf().bundleid, uuid.uuid4().hex) data = plistlib.readPlist(ip_path) log().debug('Changing bundle ID to {0!r}'.format(bundle_id)) data['CFBundleIdentifier'] = bundle_id plistlib.writePlist(data, ip_path)
def install_notifier(): """Extract ``Notify.app`` from the workflow to data directory. Changes the bundle ID of the installed app and gives it the workflow's icon. """ archive = os.path.join(os.path.dirname(__file__), 'Notify.tgz') destdir = wf().datadir app_path = os.path.join(destdir, 'Notify.app') n = notifier_program() log().debug('installing Notify.app to %r ...', destdir) # z = zipfile.ZipFile(archive, 'r') # z.extractall(destdir) tgz = tarfile.open(archive, 'r:gz') tgz.extractall(destdir) assert os.path.exists(n), \ 'Notify.app could not be installed in %s' % destdir # Replace applet icon icon = notifier_icon_path() workflow_icon = wf().workflowfile('icon.png') if os.path.exists(icon): os.unlink(icon) png_to_icns(workflow_icon, icon) # Set file icon # PyObjC isn't available for 2.6, so this is 2.7 only. Actually, # none of this code will "work" on pre-10.8 systems. Let it run # until I figure out a better way of excluding this module # from coverage in py2.6. if sys.version_info >= (2, 7): # pragma: no cover from AppKit import NSWorkspace, NSImage ws = NSWorkspace.sharedWorkspace() img = NSImage.alloc().init() img.initWithContentsOfFile_(icon) ws.setIcon_forFile_options_(img, app_path, 0) # Change bundle ID of installed app ip_path = os.path.join(app_path, 'Contents/Info.plist') bundle_id = '{0}.{1}'.format(wf().bundleid, uuid.uuid4().hex) data = plistlib.readPlist(ip_path) log().debug('changing bundle ID to %r', bundle_id) data['CFBundleIdentifier'] = bundle_id plistlib.writePlist(data, ip_path)
def install_notifier(): """Extract `Notify.app` from the workflow to data directory. Changes the bundle ID of the installed app and gives it the workflow's icon. """ archive = os.path.join(os.path.dirname(__file__), 'Notify.tgz') destdir = wf().datadir app_path = os.path.join(destdir, 'Notify.app') n = notifier_program() log().debug("Installing Notify.app to %r ...", destdir) # z = zipfile.ZipFile(archive, 'r') # z.extractall(destdir) tgz = tarfile.open(archive, 'r:gz') tgz.extractall(destdir) assert os.path.exists(n), ( "Notify.app could not be installed in {0!r}.".format(destdir)) # Replace applet icon icon = notifier_icon_path() workflow_icon = wf().workflowfile('icon.png') if os.path.exists(icon): os.unlink(icon) png_to_icns(workflow_icon, icon) # Set file icon # PyObjC isn't available for 2.6, so this is 2.7 only. Actually, # none of this code will "work" on pre-10.8 systems. Let it run # until I figure out a better way of excluding this module # from coverage in py2.6. if sys.version_info >= (2, 7): # pragma: nocover from AppKit import NSWorkspace, NSImage ws = NSWorkspace.sharedWorkspace() img = NSImage.alloc().init() img.initWithContentsOfFile_(icon) ws.setIcon_forFile_options_(img, app_path, 0) # Change bundle ID of installed app ip_path = os.path.join(app_path, 'Contents/Info.plist') bundle_id = '{0}.{1}'.format(wf().bundleid, uuid.uuid4().hex) data = plistlib.readPlist(ip_path) log().debug('Changing bundle ID to {0!r}'.format(bundle_id)) data['CFBundleIdentifier'] = bundle_id plistlib.writePlist(data, ip_path)