如果用户在GUI应用程序在后台运行(使用Tkinter)时键入ENTER键,则需要“监听”,如果用户键入ENTER键,则应显示一个消息框,但是一旦运行脚本, GUI不会出现,并且如果按Enter键,控制台中会显示大量错误,并且GUI会显示出来(消息框不会显示)。我想我在“监听”功能中缺少了一些东西:
from pynput.keyboard import Key, Listener
import logging
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("i'm listening...")
window.geometry("400x400")
def on_press(key):
logging.info(str(key))
if str(key) == "Key.enter":
msg = messagebox.showinfo( "simple message", "you clicked the enter button!")
'''
do
other
stuffs
'''
with Listener(on_press=on_press) as listener:
listener.join()
window.mainloop()
控制台:
RuntimeError: main thread is not in main loop
感谢您的帮助/提示!