如何与tkinter.tk()。mainloop()同时运行pynput.listener

发布于 2025-02-04 18:31:33 字数 777 浏览 3 评论 0 原文

我是老师。我教数学,但是由于教育面临人力资源危机,我还有一些其他职责。我教孩子一些编程,他们做得很好。现在,我想和他们一起做一个蛇游戏,但是我在GUI应用程序中实现多线程时遇到了问题。

我发现了类似的情况,但没有解决方案。像这里: 使用键盘将字符移动迷宫 在这里: tkinter.tk()和螺纹

def on_press(key):
    print('{0} pressed'.format(key))

def on_release(key):
    if key == Key.esc:
        return False

with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

root = Tk()
root.mainloop()

我希望我希望窗口与听众同时运行。取而代之的是,我的代码会听到键盘,然后(在我向下拍摄后)弹出窗口。当我打电话给Mainloop后打电话时,它会逆转,然后首先出现窗口,然后将其击落后,听众开始工作。

I am a teacher. I teach math, but since education is facing human resources crisis, I have some additional duties. I teach kids a bit of programming, they do quite well. Now I'd like to make with them a snake game, but I have a problem achieving multithreading in my GUI app.

I found similar cases but no solutions. Like here:
Using the keyboard to move the character in a maze
and here:
Tkinter.Tk() and threading

def on_press(key):
    print('{0} pressed'.format(key))

def on_release(key):
    if key == Key.esc:
        return False

with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

root = Tk()
root.mainloop()

I expected the window to run simultaneously with the listener. instead, my code listens to the keyboard and then (after I shoot the listener down) pops the window up. it reverses when I call the listener after calling the mainloop, then first the window appears and after I shoot it down the listener is starting to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2025-02-11 18:31:33

您不需要侦听器 tkinter 。您可以使用 root.bind 将函数分配给事件。

from tkinter import *

def on_press(event):
    #print('on_press: event:', event)
    #print('on_press: keysym:', event.keysym)
    print('{0} pressed'.format(event.keysym))

def on_release(event):
    #print('on_release: event:', event)
    #print('on_release: keysym:', event.keysym)
    print('{0} release'.format(event.keysym))

    if event.keysym == 'Escape':
         print("exist program")
         root.destroy()

root = Tk()

root.bind('<KeyPress>', on_press)
root.bind('<KeyRelease>', on_release)

root.mainloop()

您还可以

from tkinter import *

def on_escape(event):
    print("exist program")
    root.destroy()

root = Tk()

root.bind('<Escape>', on_escape)
#root.bind('<KeyPress-Escape>', on_press_escape)
#root.bind('<KeyRelease-Escape>', on_release_escape)

root.mainloop()

在tcl/tk文档中的每个键分隔键分配功能:


btw:

如果您想运行 tkinter pynput 同时您必须在 join()

with Listener(on_press=on_press, on_release=on_release) as listener:

    root = Tk()
    root.mainloop()

    #listener.stop()
    listener.join()

listener = Listener(on_press=on_press, on_release=on_release)
listener.start()

root = Tk()
root.mainloop()

#listener.stop()
listener.join()

You don't need Listener in tkinter. You can use root.bind to assign function to events press and release.

from tkinter import *

def on_press(event):
    #print('on_press: event:', event)
    #print('on_press: keysym:', event.keysym)
    print('{0} pressed'.format(event.keysym))

def on_release(event):
    #print('on_release: event:', event)
    #print('on_release: keysym:', event.keysym)
    print('{0} release'.format(event.keysym))

    if event.keysym == 'Escape':
         print("exist program")
         root.destroy()

root = Tk()

root.bind('<KeyPress>', on_press)
root.bind('<KeyRelease>', on_release)

root.mainloop()

You can also assign function to every key separatelly

from tkinter import *

def on_escape(event):
    print("exist program")
    root.destroy()

root = Tk()

root.bind('<Escape>', on_escape)
#root.bind('<KeyPress-Escape>', on_press_escape)
#root.bind('<KeyRelease-Escape>', on_release_escape)

root.mainloop()

Keysyms in Tcl/Tk documentation: https://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm


BTW:

If you want to run tkinter and pynput at the same time then you have to do it before join()

with Listener(on_press=on_press, on_release=on_release) as listener:

    root = Tk()
    root.mainloop()

    #listener.stop()
    listener.join()

or

listener = Listener(on_press=on_press, on_release=on_release)
listener.start()

root = Tk()
root.mainloop()

#listener.stop()
listener.join()
闻呓 2025-02-11 18:31:33

侦听器是一个线程,因此,如果您加入它,主线程将等到其结束直到继续处理。

您只需在没有语句的情况下创建侦听器对象,它将沿主线程运行(直到回调函数将返回 false

Listener is a thread, so if you join it your main thread will wait until its end to continue processing.

You can just create a Listener object without the with statement and it will run along the main thread (until a callback function will return False)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文