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