使用tkinter UI在python中生成随机密码

2024-09-24 02:13:46 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我遵循了使用tkinter和python制作密码生成器的教程。 我遵循了教程,它起了作用,但我想扩展它。 这一切都始于我想要一个“复制到剪贴板按钮”,让用户复制密码。但它总是不给一根弦,而是<;0x处的函数[函数名称]…>; 现在该部分工作了,但由于某种原因,密码生成现在中断。 感谢您的帮助

from tkinter import *
import random
import string

##### PASSWORD GENERATOR BY ROMAN ROTH

root = Tk()
root.geometry("400x280")
root.title("Password Generator")

##### INITIAL VARIABLES
title = StringVar()
choice = IntVar()
lengthlabel = StringVar()
passlength = IntVar()
symbols = "!§$%&/()=?{[]}*+'#~,;.:-_'<>"
poor = string.ascii_uppercase + string.ascii_lowercase
average = string.ascii_uppercase + string.ascii_lowercase + string.digits
advanced = poor + average + symbols


##### FUNCTIONS
def selection():
    choice.get()


def callback():
    Isum.config(text=passgen())


Isum = Label(root, text="")
Isum.pack(side=BOTTOM)

password = str(callback)


# Password generation script - joins a random symbol to the string for how many times set in the spinbox
def passgen():
    global password
    password = ""
    if choice.get() == 1:
        return password.join(random.sample(poor, passlength.get()))
    elif choice.get() == 2:
        return password.join(random.sample(average, passlength.get()))
    elif choice.get() == 3:
        return password.join(random.sample(advanced, passlength.get()))


# Copies the current password to the clipboard
def copytoclipboard():
    global password
    print(password)
    Isum.clipboard_clear()
    Isum.clipboard_append(password)
    Isum.update()


##### USER INTERFACE
label = Label(root, textvariable=title).pack()
title.set("The strength of the password:")

R1 = Radiobutton(root, text="Uppercase and Lowercase", variable=choice, value=1, command=selection).pack(anchor=CENTER)
R2 = Radiobutton(root, text="Uppercase, Lowercase, Digits", variable=choice, value=2, command=selection).pack(
    anchor=CENTER)
R3 = Radiobutton(root, text="Uppercase, Lowercase, Digits, Symbols", variable=choice, value=3, command=selection).pack(
    anchor=CENTER)

lengthlabel.set("Password length: (8 to 24)")
lengthtitle = Label(root, textvariable=lengthlabel).pack()

spinboxlength = Spinbox(root, from_=8, to_=24, textvariable=passlength, width=13).pack()

passgenButton = Button(root, text="Generate Password", command=callback)
passgenButton.pack()

copyButton = Button(root, text="Copy Password to Clipboard", command=copytoclipboard)
copyButton.pack(side=BOTTOM)

root.mainloop()

Tags: thetotextgetstringtitlerandomroot
1条回答
网友
1楼 · 发布于 2024-09-24 02:13:46

您需要使这两个函数都可以访问密码变量

passgen()
copytoclipboard()

因为它们之间是共享的

解决此问题的方法之一是将password变量设置为全局变量,如下所示:

# Password generation script - joins a random symbol to the string for how many times set in the spinbox
def passgen():
    global password
    password = ""
    if choice.get() == 1:
        password = password.join(random.sample(poor, passlength.get()))
    elif choice.get() == 2:
        password = password.join(random.sample(average, passlength.get()))
    elif choice.get() == 3:
        password = password.join(random.sample(advanced, passlength.get()))

# Copies the current password to the clipboard
def copytoclipboard():
    global password
    print(password)
    root.clipboard_clear()
    root.clipboard_append(password)
    root.update()

现在应该可以了

完整代码:


from tkinter import *
import random
import string

##### PASSWORD GENERATOR

root = Tk()
root.geometry("400x280")
root.title("Password Generator")

##### INITIAL VARIABLES
title = StringVar()
choice = IntVar()
lengthlabel = StringVar()
passlength = IntVar()
symbols = "!§$%&/()=?{[]}*+'#~,;.:-_'<>"
poor = string.ascii_uppercase + string.ascii_lowercase
average = string.ascii_uppercase + string.ascii_lowercase + string.digits
advanced = poor + average + symbols
password = "test"


##### FUNCTIONS
def selection():
    choice.get()


# Password generation script - joins a random symbol to the string for how many times set in the spinbox
def passgen():
    global password
    password = ""
    if choice.get() == 1:
        password = password.join(random.sample(poor, passlength.get()))
    elif choice.get() == 2:
        password = password.join(random.sample(average, passlength.get()))
    elif choice.get() == 3:
        password = password.join(random.sample(advanced, passlength.get()))



# passtext = passgen()


# Copies the current password to the clipboard
def copytoclipboard():
    global password
    print(password)
    root.clipboard_clear()
    root.clipboard_append(password)
    root.update()


##### USER INTERFACE
label = Label(root, textvariable=title).pack()
title.set("The strength of the password:")

R1 = Radiobutton(root, text="Uppercase and Lowercase", variable=choice, value=1, command=selection).pack(anchor=CENTER)
R2 = Radiobutton(root, text="Uppercase, Lowercase, Digits", variable=choice, value=2, command=selection).pack(
    anchor=CENTER)
R3 = Radiobutton(root, text="Uppercase, Lowercase, Digits, Symbols", variable=choice, value=3, command=selection).pack(
    anchor=CENTER)

lengthlabel.set("Password length: (8 to 24)")
lengthtitle = Label(root, textvariable=lengthlabel).pack()

spinboxlength = Spinbox(root, from_=8, to_=24, textvariable=passlength, width=13).pack()

passgenButton = Button(root, text="Generate Password", command=passgen)
passgenButton.pack()

passwordlabel = Label(root, text=password).pack(side=BOTTOM)

copyButton = Button(root, text="Copy Password to Clipboard", command=copytoclipboard)
copyButton.pack(side=BOTTOM)

root.mainloop()


要修复测试标签,请添加:

view_pass = StringVar()


def passgen():
    global password
    password = ""
    if choice.get() == 1:
        password = password.join(random.sample(poor, passlength.get()))
    elif choice.get() == 2:
        password = password.join(random.sample(average, passlength.get()))
    elif choice.get() == 3:
        password = password.join(random.sample(advanced, passlength.get()))
    view_pass.set(password)


和变化:

passwordlabel = Label(root, text=password).pack(side=BOTTOM)

passwordlabel = Label(root, textvariable=view_pass).pack(side=BOTTOM)

因为它是一个变量而不是静态文本

相关问题 更多 >