PyobjC:NSPopover上的NSTextField

时间:2014-07-29 10:13:47

标签: python nswindow pyobjc nspopover

我遇到了同样的问题:Not being able to edit NSTextField on NSPopover even though Editable behavior is set。解决方案似乎是覆盖NSWindow的canBecomeKeyWindow。我试图在PyObjC中做同样的事情,但我收到错误Python signature doesn't match implied objective-C signature

在下面的代码中,如果我注释掉canBecomeKeyWindow_(),那么应用程序会按预期运行,但我无法单击和编辑文本字段。

# from Cocoa import *
from AppKit import NSWindowController, NSApplication, NSApp, NSMaxYEdge, NSImage, NSStatusBar, NSMenu, NSMenuItem, NSVariableStatusItemLength, NSRect
from Cocoa import objc

from Foundation import NSUserNotification, NSUserNotificationCenter, NSObject
from PyObjCTools import AppHelper
import webbrowser
import subprocess
import os
global popover


class TestApp(NSApplication):

    def finishLaunching(self):
        # Make statusbar item
        statusbar = NSStatusBar.systemStatusBar()
        self.statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
        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)
                # make the menu
        self.menubarMenu = NSMenu.alloc().init()
        self.menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Login', 'loginCallback:', '')
        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)

    def loginCallback_(self, notification):
        # Initiate the contrller with a XIB
        viewController = SimpleXibDemoController.alloc().initWithWindowNibName_("Login")

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


class SimpleXibDemoController(NSWindowController):
    popover = objc.IBOutlet()
    counterTextField = objc.IBOutlet()
    username_field = objc.IBOutlet()
    password_field = objc.IBOutlet()
    submit_button = objc.IBOutlet()

    def canBecomeKeyWindow_(self):
        return 1

    def windowDidLoad(self):
        NSWindowController.windowDidLoad(self)

    @objc.IBAction
    def submit_(self, sender):
        username = self.username_field.stringValue()
        password = self.password_field.stringValue()
        self.updateDisplay(username + ' ' + password)

    def updateDisplay(self, value):
        self.counterTextField.setStringValue_(value)

if __name__ == "__main__":
    app = TestApp.sharedApplication()
    icon = NSImage.alloc().initByReferencingFile_('app-icon.png')
    app.setApplicationIconImage_(icon)
    AppHelper.runEventLoop()

1 个答案:

答案 0 :(得分:1)

看起来您在不应该添加下划线的位置。 PyObjC桥将其转换为冒号。除此之外,相应的Python布尔值应为True。因此,正确的函数看起来像这样:

def canBecomeKeyWindow(self):
    return True