0

我目前正在开发密码生成器的 v1。我一直在研究视觉元素,现在开始研究实际的密码生成。我一直在使用 pydroid 3 和 tkinter 在我的 android 设备上对此进行编码。

我让程序有三个复选框,分别代表大写、数字和特殊字符;以及所需密码长度的输入字段。我创建了第一个在选中所有复选框时执行的 if 语句,但是在点击生成按钮时对其进行测试会导致“PY_VAR4”结果,每次按下按钮时 4 都会增加(PY_VAR4、PY_VAR5 等)。

你们对我如何解决这个问题有什么建议吗?我尝试使用不同的方法来使用变量或label["text"] = "text" 代码更改结果标签的文本,但这似乎并不能解决问题。我还测试了密码长度变量(将其直接插入结果标签)和复选框变量,它似乎有效。我得出的结论是,它一定是 if 语句或生成代码中的某些东西,但我无法弄清楚它可能是什么。在这一点上,我不确定还有什么要测试和玩弄的。

这是代码:

import tkinter as tk
import random
import string


#randomized password based on ticked boxes
def final(event):
    password = tk.StringVar(value="")
    password_length = size.get()    
    if uppercase_on == True and special_chars_on == True and numbers_on == True:
        characters = string.ascii_letters + string.digits + string.punctuation
        password = "".join(secret.choice(characters) for i in range(password_length))
    result["textvariable"] = password

        
        
# start of gui
window = tk.Tk()

# some variables.
no_result = tk.StringVar(value="")

# title
title_text = tk.Label(
text="Aero's Pawsword Generator",
fg="brown",
bg="grey",
height=5
)

# Action prompt text
action_descriptor = tk.Label(
text="Select the appropriate option and tap Generate!",
height=5
)

#size input field + text
size_text = tk.Label(text="Size")
size = tk.Entry(
width=3,
fg="grey"
)

# uppercase tick box + variable
uppercase_on = tk.BooleanVar(value=False)
upper = tk.Checkbutton(
text="Uppercase Included",
variable=uppercase_on,
onvalue=True,
offvalue=False,
height=3
)

# special characters tick box + variable
special_chars_on = tk.BooleanVar(value=False)
special_chars = tk.Checkbutton(
text = "Special Characters Included",
variable=special_chars_on,
onvalue=True,
offvalue=False,
height=3
)

# numbers tick box + variable
numbers_on = tk.BooleanVar(value=False)
numbers = tk.Checkbutton(
text="Numbers Included",
variable=numbers_on,
onvalue=True,
offvalue=False,
height=3
)

# generate button
generate = tk.Button(
text="Generate",
width=5,
height=2,
bg="grey",
fg="brown"
)

# results label, empty until something has been generated
result = tk.Label(
textvariable=no_result,
height=5
)

# pack all elements
title_text.pack(fill=tk.X)
action_descriptor.pack(fill=tk.X)
size_text.pack(fill=tk.X)
size.pack()
upper.pack()
special_chars.pack()
numbers.pack()
generate.pack()
result.pack(fill=tk.X)

generate.bind("<Button-1>", final)

window.mainloop()
4

2 回答 2

0

你错过了一些...

def final():
    password = tk.StringVar(value="")
    password_length = size.get()
    if uppercase_on.get() == True and special_chars_on.get() == True and numbers_on.get() == True:
        characters = string.ascii_letters + string.digits + string.punctuation
    for i in range(int(password_length)) :
        password.set(str(password.get())+random.choice(characters))
    no_result.set(str(password.get()))

# generate button
generate = tk.Button(
    text="Generate",
    width=5,
    height=2,
    bg="grey",
    fg="brown",
    command=final
)

最后移除绑定

upper.pack()
special_chars.pack()
numbers.pack()
generate.pack()
result.pack()

window.mainloop()
于 2021-11-28T15:29:44.833 回答
0

You do have issues with your final(). Here is a version that works:

#randomized password based on ticked boxes
def final(event):
    password = "" # tk.StringVar(value="")
    password_length = size.get()
    password_length = int(password_length) if password_length else 8
    characters = string.ascii_lowercase
    if uppercase_on.get():
        characters += string.ascii_uppercase
    if special_chars_on.get():
        characters += string.punctuation
    if numbers_on.get():
        characters += string.digits
    password = "".join(random.choice(characters) for i in range(password_length))
    no_result.set(password)
    # result["textvariable"] = password

A few changes to note:

  1. You were changing the type of password in your function. You started it as a StringVar and then changed it to a string inside the if. Be careful about using the same variable for more than one purpose. It makes the code harder to follow and debug. And it leads to errors.

  2. Your if was always satisfied because you have to access the value of a StringVar (or other Tkinter variable) using the .get() method. Otherwise you're getting the actual Tkinter variable which Python treats as a logical True.

  3. You needed to separate out the different components of your password. So, I assumed the default password would be 8 characters, all lower case. Then, you can override that with the checkboxes and Entry widget (for password length).

于 2021-11-28T15:31:41.587 回答