Python 键盘模块没有';t在tkinter Gui中工作
我想创建一个键盘,它的键是 当我按下键盘上的键时,颜色变了。 代码没有错误,但按下按钮时,不会发生任何事情: 我现在不知道是否有可能用那个模块做那个键盘,或者我犯了一些错误。 请帮帮我Python 键盘模块没有';t在tkinter Gui中工作,python,tkinter,keyboard,Python,Tkinter,Keyboard,我想创建一个键盘,它的键是 当我按下键盘上的键时,颜色变了。 代码没有错误,但按下按钮时,不会发生任何事情: 我现在不知道是否有可能用那个模块做那个键盘,或者我犯了一些错误。 请帮帮我 import tkinter as tk import keyboard class SensibleKeyboard: def __init__(self): self.root = tk.Tk() self.root.title("Sensible Keyboar
import tkinter as tk
import keyboard
class SensibleKeyboard:
def __init__(self):
self.root = tk.Tk()
self.root.title("Sensible Keyboard")
self.root.resizable(False,False)
self.root.geometry("520x265")
#Titolo
self.title = tk.Label(text = "SensibleKeyboard",font = ("Arial",26)).place(x = 120 ,y = 10)
#Frame contenitore tastiera
self.frame = tk.Frame(width = 520, height = 165, bg = "#fff")
self.frame.place(x = 0, y = 100)
#Tastiera grafica
#Riga 1
self.q = tk.Button(self.frame,text = "Q", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 0 , y = 0)
self.w = tk.Button(self.frame,text = "W", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 52 , y = 0)
self.e = tk.Button(self.frame,text = "E", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 104 , y = 0)
self.r = tk.Button(self.frame,text = "R", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 156 , y = 0)
self.t = tk.Button(self.frame,text = "T", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 208 , y = 0)
self.y = tk.Button(self.frame,text = "Y", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 260 , y = 0)
self.u = tk.Button(self.frame,text = "U", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 312 , y = 0)
self.i = tk.Button(self.frame,text = "I", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 364 , y = 0)
self.o = tk.Button(self.frame,text = "O", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 416 , y = 0)
self.p = tk.Button(self.frame,text = "P", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 468 , y = 0)
#Riga 2
self.a = tk.Button(self.frame,text = "A", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 17 , y = 56)
self.s = tk.Button(self.frame,text = "S", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 69 , y = 56)
self.d = tk.Button(self.frame,text = "D", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 121 , y = 56)
self.f = tk.Button(self.frame,text = "F", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 173 , y = 56)
self.g = tk.Button(self.frame,text = "G", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 225 , y = 56)
self.h = tk.Button(self.frame,text = "H", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 277 , y = 56)
self.j = tk.Button(self.frame,text = "J", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 329 , y = 56)
self.k = tk.Button(self.frame,text = "K", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 381 , y = 56)
self.l = tk.Button(self.frame,text = "L", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 433 , y = 56)
#Riga 3
self.z = tk.Button(self.frame,text = "Z", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 40 , y = 112)
self.x = tk.Button(self.frame,text = "X", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 92 , y = 112)
self.c = tk.Button(self.frame,text = "C", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 144 , y = 112)
self.v = tk.Button(self.frame,text = "V", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 196 , y = 112)
self.b = tk.Button(self.frame,text = "B", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 248 , y = 112)
self.n = tk.Button(self.frame,text = "N", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 300 , y = 112)
self.m = tk.Button(self.frame,text = "M", height = 3, width = 6, activebackground = "black", activeforeground = "white").place(x = 352 , y = 112)
#Funzione accensione da tastiera
#Funzioni
if keyboard.is_pressed("q"):
self.q.config(bg = "blue")
elif keyboard.is_pressed("w"):
self.w.config(bg = "blue")
elif keyboard.is_pressed("e"):
self.e.config(bg = "blue")
elif keyboard.is_pressed("r"):
self.r.config(bg = "blue")
elif keyboard.is_pressed("t"):
self.t.config(bg = "blue")
elif keyboard.is_pressed("y"):
self.y.config(bg = "blue")
elif keyboard.is_pressed("u"):
self.u.config(bg = "blue")
elif keyboard.is_pressed("i"):
self.i.config(bg = "blue")
elif keyboard.is_pressed("o"):
self.o.config(bg = "blue")
elif keyboard.is_pressed("p"):
self.p.config(bg = "blue")
elif keyboard.is_pressed("a"):
self.a.config(bg = "blue")
elif keyboard.is_pressed("s"):
self.s.config(bg = "blue")
elif keyboard.is_pressed("d"):
self.d.config(bg = "blue")
elif keyboard.is_pressed("f"):
self.f.config(bg = "blue")
elif keyboard.is_pressed("g"):
self.g.config(bg = "blue")
elif keyboard.is_pressed("h"):
self.h.config(bg = "blue")
elif keyboard.is_pressed("j"):
self.j.config(bg = "blue")
elif keyboard.is_pressed("k"):
self.k.config(bg = "blue")
elif keyboard.is_pressed("l"):
self.l.config(bg = "blue")
elif keyboard.is_pressed("z"):
self.z.config(bg = "blue")
elif keyboard.is_pressed("x"):
self.x.config(bg = "blue")
elif keyboard.is_pressed("c"):
self.c.config(bg = "blue")
elif keyboard.is_pressed("v"):
self.v.config(bg = "blue")
elif keyboard.is_pressed("b"):
self.b.config(bg = "blue")
elif keyboard.is_pressed("n"):
self.n.config(bg = "blue")
elif keyboard.is_pressed("m"):
self.m.config(bg = "blue")
else:
pass
def start(self):
self.root.mainloop()
sk = SensibleKeyboard()
sk.start()
您不需要键盘库来处理此问题 首先,让我们修复非常冗余的代码。我们可以根据字符列表以动态的方式来完成,而不是一次只写一个按钮。同时,我们可以将按钮存储在一个列表中,我们可以使用该列表在单击时更新按钮 使用
bind()
我们可以检查其中一个键是否按下,然后更新列表中相应的按钮
通过绑定所有按键和所有按键释放,我们可以使用一种方法来检查这些按键中是否有一个来自我们的列表,如果是,则更新按键和按键释放上的按钮
import tkinter as tk
class SensibleKeyboard(tk.Tk):
def __init__(self):
super().__init__()
self.title("Sensible Keyboard")
self.resizable(False, False)
self.geometry("520x265")
self.title = tk.Label(text="SensibleKeyboard",
font=("Arial", 26)).grid(row=0, column=0, pady=20, sticky='ew')
main_frame = tk.Frame(self)
main_frame.grid(row=1, column=0, sticky='ew')
self.orig_color = self.cget("background")
self.btn_list = []
btn_rows = ['QWERTYUIOP', 'ASDFGHJKL', 'ZXCVBNM']
row_pad = 0
btn_ndex = 0
for ndex, rows in enumerate(btn_rows):
row_frame = tk.Frame(main_frame)
row_frame.grid(row=ndex, column=0, sticky='ew', padx=(row_pad,0))
row_pad += 20
for row_ndex, value in enumerate([char for char in rows]):
self.btn_list.append(tk.Button(row_frame, text=value, height=3, width=6,
activebackground="black", activeforeground="white"))
self.btn_list[-1].grid(row=0, column=row_ndex)
btn_ndex += 1
self.bind('<KeyPress>', lambda e: self.change_bg(e, 'p'))
self.bind('<KeyRelease>', lambda e: self.change_bg(e, 'r'))
def change_bg(self, event, press_type):
print(event)
c = event.char.upper()
for btn_ndex, value in enumerate([char for char in 'QWERTYUIOPASDFGHJKLZXCVBNM']):
if value == c:
if press_type == 'p':
self.btn_list[btn_ndex].config(bg='blue')
else:
self.btn_list[btn_ndex].config(bg=self.orig_color)
break
if __name__ == '__main__':
SensibleKeyboard().mainloop()
将tkinter作为tk导入
等级感应键盘(tk.tk):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.title(“智能键盘”)
自我调整大小(False,False)
自几何(“520x265”)
self.title=tk.Label(text=“SensibleKeyboard”,
font=(“Arial”,26)).grid(行=0,列=0,pady=20,sticky='ew')
主框架=传统框架(自身)
主框架网格(行=1,列=0,粘性=ew')
self.orig_color=self.cget(“背景”)
self.btn_list=[]
btn_行=['QWERTYUIOP','ASDFGHJKL','ZXCVBNM']
行\u pad=0
btn_ndex=0
对于ndex,枚举中的行(btn_行):
行框架=传统框架(主框架)
row\u frame.grid(row=ndex,column=0,sticky='ew',padx=(row\u pad,0))
row_焊盘+=20
对于行ndex,枚举中的值([char for char in rows]):
self.btn\u list.append(tk.Button)(行框,文本=值,高度=3,宽度=6,
activebackground=“黑色”,activeforeground=“白色”))
self.btn\u列表[-1]。网格(行=0,列=row\u ndex)
btn_ndex+=1
self.bind(“”,lambda e:self.change_bg(e,'p'))
self.bind(“”,lambda e:self.change_bg(e,'r'))
def更改背景(自身、事件、按键类型):
打印(事件)
c=event.char.upper()
对于btn_ndex,枚举中的值([QWERTYUIOPASDFHJKLZXCVBNM']中的字符对应字符]):
如果值==c:
如果按_type=='p':
self.btn\u列表[btn\u ndex].config(bg='blue')
其他:
self.btn\u列表[btn\u ndex].config(bg=self.orig\u颜色)
打破
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
SensibleKeyboard().mainloop()
为什么不简单地使用单击事件上的按钮来为按钮着色?您是否希望在不再按下按钮时将颜色更改回默认值?除了整个键盘问题之外,还有什么理由不使用列表和循环来处理所有按钮,而不是重复这么多代码?@AMC I成功地将所有内容构建在一个循环中。我唯一关心的是每行的半制表符间距,但这很容易处理每行的填充。OP可能不知道他们可以在一个循环中构建它。@Poss如果我的答案解决了您的问题,请选中答案旁边的复选标记以显示您的问题已解决。