改变 D 和 E 的颜色

Changing the Color of D and E

从我之前的post那里偷来的,这就是这个post的目的。

具有用于输入密码的触觉数字键盘的银行金库系统很容易被小偷滥用。窃贼可以使用摄像头、他们自己甚至其他人来查看输入时 4 位密码的图案;因此他们不需要知道您的密码的实际值,只需要知道允许进入系统的按钮按下顺序。为了克服这个致命缺陷,可以使用具有数字键盘 GUI 的触摸屏显示器,每次输入 PIN 码时,无论输入正确与否,都会随机排列按键。

我试图让这个用户友好,所以我希望将值 D 和 E 涂成红色以便于找到它们,但是当我尝试调整代码时,它会改变所有值的颜色.有谁知道解决方法?任何和所有的帮助表示赞赏。 以下是我的代码:

import tkinter as tk
import random

def code(position):
    global pin
    b = buttons[position]
    value = b['text']

    if value == 'D':
        # remove last element from `pin`
        pin = pin[:-1]
        # remove all from `entry` and put new `pin`
        e.delete('0', 'end')
        e.insert('end', pin)

    elif value == 'E':
        # check pin
        if pin == "3529":
            print("PIN OK")
        else:
            print("PIN ERROR!")
            # clear pin
            pin = ''
            e.delete('0', 'end')
    else:
        # add number to `pin`
        pin += value
        # add number to `entry`
        e.insert('end', value)

    print("Current:", pin)

    shuffle_buttons()

def shuffle_buttons():
    for key in keys:
        random.shuffle(key)
    random.shuffle(keys)
    for y, row in enumerate(keys):
        for x, key in enumerate(row):
            b = buttons[(x, y)]
            b['text'] = key                

# --- main ---

# keypad description

keys = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['D', '0', 'E'],
]

buttons = {}

# create global variable 
pin = '' # empty string

# init
root = tk.Tk()

# create `entry` to display `pin`
e = tk.Entry(root, justify='right')
e.grid(row=0, column=0, columnspan=3, ipady=5)

# create `buttons` using `keys
for y, row in enumerate(keys):
    for x, key in enumerate(row):
        position  = (x, y)
        b = tk.Button(root, text= key, command= lambda val=position: code(val))
        b.grid(row=y+1, column=x, ipadx=20, ipady=20)

        buttons[position] = b

shuffle_buttons()

root.mainloop()

随时使用 config 更改按钮上值的颜色 shuffle_buttons() 被调用:

import tkinter as tk
import random

def code(position):
    global pin
    b = buttons[position]
    value = b['text']

    if value == 'D':
        # remove last element from `pin`
        pin = pin[:-1]
        # remove all from `entry` and put new `pin`
        e.delete('0', 'end')
        e.insert('end', pin)

    elif value == 'E':
        # check pin
        if pin == "3529":
            print("PIN OK")
        else:
            print("PIN ERROR!")
            # clear pin
            pin = ''
            e.delete('0', 'end')
    else:
        # add number to `pin`
        pin += value
        # add number to `entry`
        e.insert('end', value)

    print("Current:", pin)

    shuffle_buttons()

def shuffle_buttons():
    for key in keys:
        random.shuffle(key)
    random.shuffle(keys)
    for y, row in enumerate(keys):
        for x, key in enumerate(row):
            b = buttons[(x, y)]
            b['text'] = key
            if key in ["D", "E"]:
                b.config(fg="red")
            else:
                b.config(fg="black")               

# --- main ---

# keypad description

keys = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['D', '0', 'E'],
]

buttons = {}

# create global variable 
pin = '' # empty string

# init
root = tk.Tk()

# create `entry` to display `pin`
e = tk.Entry(root, justify='right')
e.grid(row=0, column=0, columnspan=3, ipady=5)

# create `buttons` using `keys
for y, row in enumerate(keys):
    for x, key in enumerate(row):
        position  = (x, y)
        b = tk.Button(root, text= key, command= lambda val=position: code(val))
        b.grid(row=y+1, column=x, ipadx=20, ipady=20)

        buttons[position] = b

shuffle_buttons()

root.mainloop()