pynput 不让 tkinter 生成 window
pynput not letting tkinter make window
如果按下 Esc 键,我有 pynput 读取按键,tkinter 在 window 中显示它们。两者都工作正常,但是当我将它们放入一个代码中时,什么也没有发生。这是我的代码:
import tkinter as tk
from pynput import keyboard
listen = False
def onpress(key):
global listen
if str(key) == 'Key.esc':
listen = not listen
if listen:
if str(key) == 'Key.1':
labelval.set(labelval.get()+'1')
print('1')
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=entryval)
lab.grid(column=0,row=0)
with keyboard.Listener(
on_press = onpress) as listener:
listener.join()
当我 运行 它时没有任何反应,tk window 没有出现,并且 1 没有打印到屏幕上。有什么建议么?当我取出 listener.join()
时,一切正常,但它不能 keylog
您必须 运行 在 with
和 join()
之间编码 - 并使用 mainloop()
显示 tkinter window
with keyboard.Listener(on_press=onpress) as listener:
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
win.mainloop()
listener.join()
或至少 mainloop()
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
with keyboard.Listener(on_press=onpress) as listener:
win.mainloop()
listener.join()
不写也可以with()
listener = keyboard.Listener(on_press=onpress)
listener.start()
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
win.mainloop()
listener.join()
顺便说一句:您创建了 labelval
但您使用了 textvariable=entryval
在 Linux 我不得不使用不同的方法来识别 1
import tkinter as tk
from pynput import keyboard
listen = False
def onpress(key):
global listen
#if key == keyboard.Key.esc:
if str(key) == 'Key.esc':
listen = not listen
if listen:
if hasattr(key, 'char') and key.char == '1':
labelval.set(labelval.get()+'1')
print('1!')
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
with keyboard.Listener(on_press=onpress) as listener:
win.mainloop()
listener.join()
如果按下 Esc 键,我有 pynput 读取按键,tkinter 在 window 中显示它们。两者都工作正常,但是当我将它们放入一个代码中时,什么也没有发生。这是我的代码:
import tkinter as tk
from pynput import keyboard
listen = False
def onpress(key):
global listen
if str(key) == 'Key.esc':
listen = not listen
if listen:
if str(key) == 'Key.1':
labelval.set(labelval.get()+'1')
print('1')
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=entryval)
lab.grid(column=0,row=0)
with keyboard.Listener(
on_press = onpress) as listener:
listener.join()
当我 运行 它时没有任何反应,tk window 没有出现,并且 1 没有打印到屏幕上。有什么建议么?当我取出 listener.join()
时,一切正常,但它不能 keylog
您必须 运行 在 with
和 join()
之间编码 - 并使用 mainloop()
显示 tkinter window
with keyboard.Listener(on_press=onpress) as listener:
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
win.mainloop()
listener.join()
或至少 mainloop()
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
with keyboard.Listener(on_press=onpress) as listener:
win.mainloop()
listener.join()
不写也可以with()
listener = keyboard.Listener(on_press=onpress)
listener.start()
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
win.mainloop()
listener.join()
顺便说一句:您创建了 labelval
但您使用了 textvariable=entryval
在 Linux 我不得不使用不同的方法来识别 1
import tkinter as tk
from pynput import keyboard
listen = False
def onpress(key):
global listen
#if key == keyboard.Key.esc:
if str(key) == 'Key.esc':
listen = not listen
if listen:
if hasattr(key, 'char') and key.char == '1':
labelval.set(labelval.get()+'1')
print('1!')
win = tk.Tk()
labelval = tk.StringVar()
lab = tk.Label(win, textvariable=labelval)
lab.grid(column=0,row=0)
with keyboard.Listener(on_press=onpress) as listener:
win.mainloop()
listener.join()