【问题标题】:Generating a random password in python using tkinter UI使用 tkinter UI 在 python 中生成随机密码
【发布时间】:2020-11-03 01:31:15
【问题描述】:

所以我按照教程使用 tkinter 和 python 制作密码生成器。 我按照教程进行了操作,并且效果很好,但我想对其进行扩展。 当我想要一个“复制到剪贴板按钮”让用户复制密码时,这一切都开始了。但它总是给出的不是字符串而是 现在这部分工作了,但由于某种原因,密码生成现在被破坏了。 任何帮助表示赞赏!

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

【问题讨论】:

  • 你能详细说明一下吗?
  • passgenButton = Button(root, text="Generate Password", command=passgen()) 尝试更改为passgenButton = Button(root, text="Generate Password", command=passgen) 删除命令的括号
  • 我删除了括号,但它没有解决我的问题

标签: python python-3.x string function tkinter


【解决方案1】:

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

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)

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

【讨论】:

  • 我将教程中的回调函数添加回代码中(它使用了 Isum,我不知道为什么没有它就无法工作,但现在我的密码又像往常一样生成了。但是输出“添加您的建议以使变量成为全局变量后,复制到剪贴板按钮仍然没有。我在上面发布了更新的代码。
  • @RomanRoth 我确实知道什么是 Isum,但是如果您将上面我发布的代码和 command = passgen 放在按钮事件中,那么它应该可以正常工作,我已经测试过了。我已经添加了完整的代码供您参考。
  • 谢谢你现在复制到剪贴板工作 - 但是在复制按钮下方只有测试。使用 Isum 函数显示密码 - 你能告诉我如何添加密码吗?
  • @RomanRoth 不客气,我已经在上面的帖子中添加了请求的功能,请查看。
  • 你太棒了,非常感谢 - 3 天后,它现在终于按预期工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
  • 2011-08-31
  • 1970-01-01
  • 2018-04-02
相关资源
最近更新 更多