按下“E”时如何重命名按钮

Posted

技术标签:

【中文标题】按下“E”时如何重命名按钮【英文标题】:How to rename buttons when 'E' is pressed 【发布时间】:2017-02-27 03:49:07 【问题描述】:

从我之前的帖子中偷来的,这就是这篇文章的目的。

具有用于输入密码的触觉数字键盘的银行金库系统很容易被小偷滥用。窃贼可以使用摄像头、他们自己甚至其他人来查看 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()

【问题讨论】:

【参考方案1】:

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

【讨论】:

我实现了你所说的,但 GUI 没有更新按钮上的文本。我认为for key in keys: random.shuffle(key) random.shuffle(keys) print(keys) 部分应该移到elif value == 'E': 下,但是当我按“E”时它仍然不会为我洗牌。 你可以更好地解释你想要做什么 具有用于输入密码的触觉数字键盘的银行保险库系统很容易被小偷滥用。窃贼可以使用摄像头、他们自己甚至其他人来查看 4 位密码输入时的图案;因此他们不需要知道您的引脚的实际值,只需知道允许进入系统的按钮按下顺序即可。为了克服这个致命的缺陷,可以使用带有数字键盘 GUI 的触摸屏显示器,每次输入密码时都会对键进行洗牌,无论密码是否正确。 你应该把这个解释写在你的帖子里 你先生,太棒了!非常感谢,我会在完成后为有兴趣的人发布我的最终项目!

以上是关于按下“E”时如何重命名按钮的主要内容,如果未能解决你的问题,请参考以下文章

win10C盘用户文件夹重命名时遇到问题,该怎么办?

如何批量重命名文件夹下的Excel文件

如何重命名输入类型 = 文件的 HTML“浏览”按钮?

如何重命名输入类型 = 文件的 HTML“浏览”按钮?

linux中如何用rename命令重命名一个文件

如何让文件自动重命名?