Mac OS X Pynput问题:`NSInternalInconsistencyException`

问题描述

我正在Mac OS Catalina设备上使用Pynput。我正在以管理员身份运行pynput脚本。

有效的代码

import threading
from skimage.viewer import ImageViewer
from skimage import io

def view(fname):
    """ View a Skimage Image  """
    print(threading.current_thread().name) # make sure it's running in the main thread
    img = io.imread(fname)
    viewer = ImageViewer(img)
    viewer.show()

view('board_shot.png')

以上内容输出将打印:

MainThread,然后使用ImageViewer

显示图像

我的无效代码

import threading
from skimage.viewer import ImageViewer
from skimage import io
from pynput.keyboard import Listener

def main():
    with Listener(on_release=checkkeyrelease) as listener:
        listener.join() # keep on listening
    view('board_shot.png') # view after press

def view(fname):
    """ View a Skimage Image  """
    print(threading.current_thread().name) # make sure it's running in the main thread
    img = io.imread(fname)
    viewer = ImageViewer(img)
    viewer.show()
   
def checkkeyrelease(key):
    if 'char' in dir(key):
        if key.char == 's':
            return False # stop listener

main()

输出

MainThread

2020-08-18 08:10:28.965 python[23022:1182855] pid(23022)/euid(501) is calling TIS/TSM in non-main thread environment,ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2020-08-18 08:10:28.965 python[23022:1182855] pid(23022)/euid(501) is calling TIS/TSM in non-main thread environment,ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!
2020-08-18 08:10:28.965 pyt ...  *** Assertion failure in void assertRunningOnAppKitThread(void)(),/buildroot/Library/Caches/com.apple.xbs/Sources/ViewBridge/ViewBridge-401.1/ViewBridgeUtilities.m:912
2020-08-18 08:10:29.542 python[23022:1182855] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'not running on AppKit (main) thread'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff39847a7d __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff63f19a17 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff39862796 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x00007fff3bb50cf5 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 166
    4   ViewBridge                          0x00007fff61243c37 __26+[NSRemoteView initialize]_block_invoke + 15
    5   libdispatch.dylib                   0x00007fff656995f8 _dispatch_call_block_and_release + 12
    6   libdispatch.dylib                   0x00007fff6569a63d _dispatch_client_callout + 8
    7   libdispatch.dylib                   0x00007fff656a568d _dispatch_main_queue_callback_4CF + 1135
    8   CoreFoundation                      0x00007fff39791a37 __CFRUNLOOP_IS_SERVICING_THE_MAIN_disPATCH_QUEUE__ + 9
    9   CoreFoundation                      0x00007fff39791161 __CFRunLoopRun + 2289
    10  CoreFoundation                      0x00007fff3979061e CFRunLoopRunSpecific + 455
    11  HIToolBox                           0x00007fff389ef1ab runcurrentEventLoopInMode + 292
    12  HIToolBox                           0x00007fff389eeee5 ReceiveNextEventCommon + 603
    13  HIToolBox                           0x00007fff389eec76 _BlockUntilNextEventMatchingListInModeWithFilter + 64
    14  AppKit                              0x00007fff36d8777d _DPSNextEvent + 1135
    15  AppKit                              0x00007fff36d8646b -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
    16  AppKit                              0x00007fff36d80588 -[NSApplication run] + 699
    17  libqcocoa.dylib                     0x0000001a1d49e15b qt_plugin_instance + 99558
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

正如您在上方看到的那样,该图像实际上是在主线程中查看的,所以我不明白问题出在哪里。有人知道吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)