matplotlib 如何使一个键盘的数字,其中每个键都有自己的颜色

pod7payv  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(139)

我想准备键盘的图像,每个键可以被分配不同的颜色。我看过Python keyboardlayout包,但不清楚如何控制键的颜色。我更喜欢使用现有的Python包。

yc0p9oo0

yc0p9oo01#

您可以使用update_key。例如,更改A键的颜色:

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()

输出:

相关问题