Tkinter 如何与 Listener 一起工作? [英] How can a Tkinter work with a Listener together?

查看:70
本文介绍了Tkinter 如何与 Listener 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个 Tkinter,我希望有一个监听器来监视用户的键盘输入.但是当我使用 mainloop() 启动 Tkinter 时,Listener 无法与之协同工作,直到我退出 Tkinter 才会启动.

I have written a Tkinter and I hope to have a Listener to monitor the Keyboard Input by Users. But when I use mainloop() to start the Tkinter, the Listener cannot work together with it, and will start until I quit Tkinter.

我已经尝试在 Tkinter 子单元中添加这个监听器,但它不起作用.

I have tried to add this Listener in Tkinter sub-unit, but it does not work as the same.

def initialization():
    print("Starting...")
    print("Start listener...")
    with mouse.Listener(on_click=onMouseClick) as listener:
        listener.join()

if __name__ == "__main__" :
    root = tk.Tk()
    root.geometry('800x80')
    root.resizable(height=True, width=True)
    root.overrideredirect(False)
    root.title('vENC Console')

    OneBtn = Button(root, command=initialization, text="One Button", width='30')
    root.mainloop()

我怎样才能让他们一起工作?我需要使用多线程吗?

How can I let them work together? Do I need to use multi-thread?

推荐答案

你可以这样写

listener = mouse.Listener(on_click=onMouseClick)
listener.start() # start thread

root.mainloop()

listener.stop()  # stop thread
listener.join()  # wait till thread really ends its job

并且不要在 onMouseClick 中使用 return False 因为它会结束监听器.

And don't use return False in onMouseClick because it ends listener.

tkinter 有自己的方法来获取键和鼠标事件,所以也许你应该使用它们.Listener 如果您必须在 tkinter 的窗口最小化并且没有从系统获取事件时捕获事件,那么它会很有用.

tkinter has own methods to get keys and mouse events so maybe you should use them. Listener can be useful if you have to catch event when tkinter's window is minimized and it doesn't get events from system.

import tkinter as tk
from pynput import mouse

def onMouseClick(*args):
    print(args)

root = tk.Tk()

listener = mouse.Listener(on_click=onMouseClick)
listener.start() # start thread

root.mainloop()

listener.stop()  # stop thread
listener.join()  # wait till thread really ends its job

<小时>

import tkinter as tk
from pynput import mouse

def onMouseClick(*args):
    print(args)

def initialization():
    global listener

    print("Starting...")
    print("Start listener...")
    listener = mouse.Listener(on_click=onMouseClick)
    listener.start() # start thread

if __name__ == "__main__" :

    listener = None   

    root = tk.Tk()

    btn = tk.Button(root, command=initialization, text="One Button")
    btn.pack()

    root.mainloop()

    # stop listener if it was created
    if listener: # if listener is not None:
        print("Stop listener...")
        listener.stop()  # stop thread
        listener.join()  # wait till thread really ends its job

<小时>

带有停止侦听器的按钮的示例


example with button which stop listener

import tkinter as tk
from pynput import mouse

def onMouseClick(*args):
    print(args)

def on_start():
    global listener

    if not listener:
        print("Start listener...")
        listener = mouse.Listener(on_click=onMouseClick)
        listener.start() # start thread
    else:
        print("listener already running")

def on_stop():
    global listener

    if listener:
        print("Stop listener...")
        listener.stop()  # stop thread
        listener.join()  # wait till thread really ends its job
        listener = None  # to inform that listener doesn't exist
    else:
        print("listener not running")

if __name__ == "__main__" :

    print("Starting...")

    listener = None  # to keep listener

    root = tk.Tk()

    btn = tk.Button(root, command=on_start, text="Star Mouse Listener")
    btn.pack()

    btn = tk.Button(root, command=on_stop, text="Stop Mouse Listener")
    btn.pack()

    root.mainloop()

    # stop listener if it was created
    if listener: # if listener is not None:
        print("Stop listener...")
        listener.stop()  # stop thread
        listener.join()  # wait till thread really ends its job

这篇关于Tkinter 如何与 Listener 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆