[PyQt] win32api.LoadKeyboardLayout; any solutions for OSX?

Timothy W. Grove tim_grove at sil.org
Thu Mar 19 17:20:25 GMT 2015


A personal reply to my question sent me to the following link: 
http://stackoverflow.com/questions/23729704/change-osx-keyboard-layoutinput-source-programmatically-via-terminal-or-appl. 
I seem to have found a solution that worked using PyObjC.

I'm currently using Python 3.3 installed via macports, and I installed 
the appropriate ports for my setup (py33-pyobjc, py33-pyobjc-cocoa).

For my import statement:

    from AppKit import NSTextInputContext

To find the current keyboard being used:

    ic = NSTextInputContext.new()
    current_keyboard = ic.selectedKeyboardInputSource()

To list the currently available keyboards:

    keyboards = ic.keyboardInputSources()

To change the current keyboard (to 'Arabic', for example):

    ic.setValue_forKey_('com.apple.keylayout.Arabic',
    'selectedKeyboardInputSource')


Now, that is the good news! This all worked great in my source code, but 
as an app bundle (created with cx_Freeze 4.3.2) it wouldn't run at all. 
I'll include the full error trace at the end of this email, but the 
problems seem to start with the import of AppKit and end with 
objc/_lazyimport.py.

I'm not sure if this points to a 'bug' in objc or something else which 
I've left out of my setup routine, but if anyone has any ideas, I would 
be interested to hear from you.

Best regards,
Timothy Grove



On 05/03/2015 14:51, Timothy W. Grove wrote:
> I was looking for a way to change keyboard layouts from within a 
> Python 3 / PyQt4 application. Win32api.LoadKeyboardLayout has come to 
> my rescue on Windows, but is anyone aware of a cross-platform or OSX 
> specific solution for Apple Mac? Thanks for any suggestions.
>
> Best regards,
> Tim

    Traceback (most recent call last):

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/cx_Freeze/initscripts/Console3.py",
    line 27, in <module>

         exec(code, m.__dict__)

       File "soosl.py", line 6, in <module>

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1565, in _find_and_load

         return _find_and_load_unlocked(name, import_)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1532, in _find_and_load_unlocked

         loader.load_module(name)

       File
    "/Users/timothygrove/Documents/workspace/SooSL/mainwindow.py", line
    19, in <module>

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1565, in _find_and_load

         return _find_and_load_unlocked(name, import_)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1532, in _find_and_load_unlocked

         loader.load_module(name)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/AppKit/__init__.py",
    line 9, in <module>

         import Foundation

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1565, in _find_and_load

         return _find_and_load_unlocked(name, import_)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1532, in _find_and_load_unlocked

         loader.load_module(name)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/Foundation/__init__.py",
    line 9, in <module>

         import CoreFoundation

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1565, in _find_and_load

         return _find_and_load_unlocked(name, import_)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/importlib/_bootstrap.py",
    line 1532, in _find_and_load_unlocked

         loader.load_module(name)

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/CoreFoundation/__init__.py",
    line 20, in <module>

         }, ())

       File
    "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/objc/_lazyimport.py",
    line 82, in __init__

         if nm.startswith(pfx):

    AttributeError: 'NoneType' object has no attribute 'startswith'

    logout


    [Process completed]


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20150319/0e01a2a6/attachment.html>


More information about the PyQt mailing list