2017-02-27 73 views
1

從我以前的帖子中被盜,這是本文的目的。如何在按'E'時重命名按鈕

具有用於引腳輸入的觸覺數字鍵盤的銀行金庫系統容易被盜賊濫用。小偷可以使用照相機,他們自己或甚至其他人查看4位數字引腳進入時的模式;因此他們不需要知道引腳的實際值,只需按下一系列按鈕即可進入系統。爲了克服這個致命的缺陷,可以使用具有數字鍵盤GUI的觸摸屏顯示器,每次輸入引腳時都會對鍵進行混洗,而不管它是否正確。

我試圖每次接收到'E'(它工作)時洗牌鍵盤矩陣,但是GUI拒絕更新。我試圖讓數字和字母洗牌,隨時隨地進入他們的引腳。任何幫助表示讚賞。

#!/usr/bin/env python3 

import Tkinter as tk 
import random 

def code(value): 
    # inform function to use external/global variable 
    global pin 

    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) 



# --- main --- 

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



for key in keys: 
    random.shuffle(key) 
    random.shuffle(keys) 
    print(keys) 

# 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, 1): 
    for x, key in enumerate(row): 
     # `lambda` inside `for` have to use `val=key:code(val)` 
     # instead of direct `code(key)` 
     b = tk.Button(root, text=key, command=lambda val=key:code(val)) 
     b.grid(row=y, column=x, ipadx=20, ipady=20) 

# start program 

root.mainloop() 

回答

0

變化enumerate(keys, 1)enumerate(keys)b.grid(row=y, column=x, ipadx=20, ipady=20)b.grid(row=y+1, column=x, ipadx=20, ipady=20)使該條目可以顯示。

完整代碼:

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

我實現你所說的話,但GUI犯規更新按鈕上的文字。我認爲'鑰匙鑰匙: random.shuffle(key) random.shuffle(keys) print(keys)'部分應該在elif value =='E'下移動:'但它仍然會當我按下'E'時,爲我洗牌GUI。 – greatgamer34

+0

你可以更好地解釋你想要做什麼 – eyllanesc

+0

我應該發佈我的更新代碼嗎? – greatgamer34

相關問題