2017-04-09 44 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