当 pynput 键盘侦听器启动时,Tkinter 应用程序崩溃

问题描述 投票:0回答:1

我有一个一周无法处理的问题...... 当我在 macOS 上从 tkinter 按钮命令启动 pynput 键盘侦听器(如下例所示)时,应用程序仅崩溃,并在控制台中显示“进程已完成,退出代码 133(被信号 5 中断:SIGTRAP)”消息。在 Windows 11 上一切正常。我尝试从新线程启动侦听器,但结果是一样的。

import tkinter
from tkinter import Tk, Button
from pynput import keyboard

logo = b'iVBORw0KGgoAAAANSUhEUgAAACUAAAAlCAYAAADFniADAAAACXBIWXMAAAsSAAALEgHS3X78AAACOElEQVRYhc2Yy23CQBCG/1jcoYO4ATuc7GPcQdIB3H2hgzgdEMm+Ox2ECmKOvhFXAB1ABURjjdGyLHgX/PqlFcKP3Y+Z3WFmno7HI0zlJv4UQMBjAsAG8MzTrPkzA7ApwvzHdH5tKDfxafE5gIUAoKMDAAJbFmG+aQzKTfyIYcYGMCqtaJ4izLd3Q7GbUgAvD8KIOjBYagzlJn7AZn/UOtf0XYT5XBvKTfxq7+wNFnkFsAMgu6Y6BGvFO3QQFhdXCUocTuxNndjbO7EXyPduDSf2jk7sRYr5IrqneteJvcyJvVS+bkkWmvBRbstlKs3YMydZ0kNpx0CVlm7i2xdQvLHfegACGyKqvoyEG5H6eSPZ/ONE2ZoTkBsjimElFMej1wagZjzuFRlmXrlPGS960DuEPSWbvC+Nyf0Wh4Em/0YeVUCWmg4IiGRbnA8NSfaoK0tx+nNhFdWzI9XFlvShO+3IMBOo02cR5tpB2E38TBUfaU9ppahdymrYUk0osziZPwwIalNF9KxnEFFZBWVcm7WkVRHm+xKKK4shuLA0jph5LvtjKbWryi4ZatcfE05VzQmKfNljXrUSew5nhUMR5nQKvzoG2snGuFaMphzpTaL9L1W9XBHpKuIS/mydW2V7+mC+XSc67YGqEyPXfSdxnd+WK/8oZbrWGqptBXHJlBr2pG6pNpMwaZrNeQ/cC0f7LarrTRlBCXBUBtEgC9YBUpOMTnTKIUdLd/U8BUDK71Xp9FbHIkoB+AdGxhvJUKsuTwAAAABJRU5ErkJggg=='

root = Tk()
root.title("Test window")
root.geometry("400x200")
core_logo_icon = tkinter.PhotoImage(data=logo)


def start_listener():
    def on_press(key):
        print("Key ", key, " pressed")

    def on_release(key):
        print("Key ", key, " released")

    keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
    keyboard_listener.start()
    print("Keyboard listener ON")


button = Button(root, command=start_listener, image=core_logo_icon, borderwidth=0)
button.pack()
root.mainloop()

我在 macOS Monterey 12.4 M1 PRO 上使用 pynput 1.7.6、Python 3.9。谢谢您的帮助。

编辑:当我从终端运行应用程序时,错误消息是“zsh:跟踪陷阱路径/到/py/脚本”

python macos tkinter listener pynput
1个回答
0
投票

我也遇到了同样的问题,这对我有用:

keyboard_listener.start()
keyboard_listener.join()

而不是

keyboard_listener.start()
© www.soinside.com 2019 - 2024. All rights reserved.