我想先显示一个文本,然后再删除它,并在这段时间内保持软件运行(即防止循环),并获取文本并重复显示。
我希望它采用函数形式
我想用每个键显示文本,并且文本操作在一个函数中
import keyboard
import pynput
import tkinter, win32api, win32con, pywintypes
def printer(text):
label = tkinter.Label(text=text, font=('Times New Roman','16'), fg='white', bg='blue')
label.master.overrideredirect(True)
label.master.geometry("+0+0")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "blue")
hWindow = pywintypes.HANDLE(int(label.master.frame(), 16))
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)
label.pack()
label.after(1000, lambda:label.config(text=''))
label.mainloop()
pressedList = ''
def on_press(key):
global pressedList
try:
pressedKey = key.char # single-char keys
except:
pressedKey = key.name # other keys
pressedList += pressedKey
if pressedList.rfind('a')+len('a') == len(pressedList) and pressedList[pressedList.rfind('a'):] == 'a':
printer('a')
elif pressedList.rfind('f1')+len('f1') == len(pressedList) and pressedList[pressedList.rfind('f1'):] == 'f1' and pressedKey == 'f1':
printer('f1')
else:
printer('pass')
pass
lis = pynput.keyboard.Listener(on_press=on_press)
lis.start()
lis.join()
答案 0 :(得分:0)
您可能不需要将tkinter
与其他事件监听器混合;将keypress
绑定到tkinter
窗口将满足您的要求:
import tkinter as tk
def display_key(e=None):
if e is not None:
tv.set(e.keysym)
root.after(1000, display_key)
else:
tv.set('-')
root = tk.Tk()
root.geometry('300x200')
tv = tk.StringVar()
tv.set('-')
label = tk.Label(root, textvariable=tv)
label.config(font=("Courier", 144))
label.pack(expand=True, fill='both')
root.bind('<Key>', display_key)
root.mainloop()