2017-04-09 43 views
0

从我以前的帖子中被盗,这是本文的目的。更改D和E的颜色

具有用于引脚输入的触觉数字键盘的银行金库系统容易被盗贼滥用。小偷可以使用照相机,他们自己或甚至其他人查看4位数字引脚进入时的模式;因此他们不需要知道引脚的实际值,只需按下一系列按钮即可进入系统。为了克服这个致命的缺陷,可以使用具有数字键盘GUI的触摸屏显示器,每次输入引脚时都会对键进行混洗,而不管它是否正确。

我试图让这个用户很友好,所以我想将值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() 
+1

你在哪里设置颜色为红色? – jdigital

+0

我删除了我试图做的那部分代码,因为它弄乱了其他的按钮,我也不认为我是从正确的角度接近它。 – greatgamer34

回答

0

使用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() 
+0

非常感谢你,我没有意识到它可以在这样的洗牌功能里搜索!我把你的答案标记为正确! – greatgamer34