Я хотел создать клавиатуру, клавиши которой были бы цветными, когда я нажимал клавишу на клавиатуре. В коде нет ошибок, но когда кнопка нажата, ничего не происходит: сейчас нет, если возможно сделать эту клавиатуру с этим модулем, или я сделал несколько ошибок. Пожалуйста, помогите мне.
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()