How to make a figure of a keyboard where each key has its own color

91 Views Asked by At

I would like to prepare images of keyboards where each key can be assigned a different color. I have seen the Python keyboardlayout package but it is not clear how to control the color of the keys. I would prefer to use existing Python packages.

1

There are 1 best solutions below

1
Tranbi On

You can use update_key. Changing the color of the A key for instance:

import tkinter as tk
import tkinter.font as tkf

import keyboardlayout as kl
import keyboardlayout.tkinter as klt

key_size = 60

window = tk.Tk()
window.resizable(False, False)

key_info_dict = {
    "margin": 5,
    #"color": "grey", letting color out so that it can be specified for each new key info profile
    "txt_color": "black",
    "txt_font": tkf.Font(family='Arial', size=key_size//4),
    "txt_padding": (key_size//6, key_size//10)
}
key_info = kl.KeyInfo(**key_info_dict, color="grey")

keyboard_layout = klt.KeyboardLayout(
    kl.LayoutName.QWERTY,
    kl.KeyboardInfo(position=(0,0), padding=2),
    (key_size, key_size),  # width, height,
    key_info,
    master=window
)

key_info_red = kl.KeyInfo(**key_info_dict, color="red")
keyboard_layout.update_key(key=kl.Key.A, key_info=key_info_red)

window.mainloop()

Output: enter image description here