我正在制作一个简单的tkinter密码程序,而且我遇到了问题。我收到以下错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "password.py", line 23, in pass_gen
PassField.insert(0, random.choice(string.digits + string.ascii_letters), end='')
TypeError: insert() got an unexpected keyword argument 'end'
如果我删除结束='' ,错误消息将消失,但代码无法按预期运行。我最终得到一个数字密码而不是所需的长度。
import random
import string
from tkinter import *
from tkinter import ttk
root = Tk()
strength = IntVar()
length = DoubleVar()
root.title('Password Generator')
def pass_gen(event):
lengthy = int(PassLength.get())
print(lengthy)
if strength.get() == 1:
for i in range(0, lengthy):
PassField.delete(0, 'end')
PassField.insert(0, random.choice(string.ascii_letters), end='')
elif strength.get() == 2:
for i in range(0, lengthy):
PassField.delete(0, 'end')
PassField.insert(0, random.choice(string.digits + string.ascii_letters), end='')
elif strength == 'x':
for i in range(0, lengthy):
PassField.delete(0, 'end')
PassField.insert(0, random.choice(string.punctuation + string.digits + string.ascii_letters), end='')
Radiobutton(root, text="Weak", variable=strength, value=1).grid(row=0, column=0, sticky=N)
Radiobutton(root, text="Strong", variable=strength, value=2).grid(row=0, column=1, sticky=N)
Radiobutton(root, text="Extra-strong", variable=strength, value=3).grid(row=0, column=2, sticky=N)
Label(root, text='Length').grid(row=1, column=0, sticky=W, padx=4)
PassLength = Scale(root, variable=length, from_=0, to=30, orient=HORIZONTAL)
PassLength.grid(row=1, column=1)
Label(root, text='Password').grid(row=2, sticky=W, padx=4)
PassField = Entry(root)
PassField.grid(row=2, column=1, sticky=E, pady=4)
generateButton = Button(root, text='Generate')
generateButton.bind("<Button-1>", pass_gen)
generateButton.grid(row=2, column=2,sticky=E)
root.mainloop()
我很感激您的意见。 TIA
答案 0 :(得分:1)
您可能需要insert('end', ...)
import tkinter as tk
root = tk.Tk()
p = tk.Entry(root)
p.pack()
p.insert(0, 'a')
p.insert('end', 'b')
root.mainloop()
但请删除delete(0, 'end')
,因为您删除了可疑数据。
答案 1 :(得分:0)
找到连接密码的正确方法,这是我更新的代码:
import random
import string
from tkinter import *
from tkinter import ttk
root = Tk()
strength = IntVar()
length = DoubleVar()
root.title('Password Generator')
def pass_gen(event):
lengthy = int(PassLength.get())
if strength.get() == 1:
list = []
for i in range(0, lengthy):
list.append(random.choice(string.ascii_letters))
password = ''.join(list)
PassField.delete(0, 'end')
PassField.insert(0, password)
elif strength.get() == 2:
list = []
for i in range(0, lengthy):
list.append(random.choice(string.digits + string.ascii_letters))
password = ''.join(list)
PassField.delete(0, 'end')
PassField.insert(0, password)
elif strength.get() == 3:
list = []
for i in range(0, lengthy):
list.append(random.choice(string.punctuation + string.digits + string.ascii_letters))
password = ''.join(list)
PassField.delete(0, 'end')
PassField.insert(0, password)
Radiobutton(root, text="Weak", variable=strength, value=1).grid(row=0, column=0, sticky=N)
Radiobutton(root, text="Strong", variable=strength, value=2).grid(row=0, column=1, sticky=N)
Radiobutton(root, text="Extra-strong", variable=strength, value=3).grid(row=0, column=2, sticky=N)
Label(root, text='Length').grid(row=1, column=0, sticky=W, padx=4)
PassLength = Scale(root, variable=length, from_=0, to=20, orient=HORIZONTAL)
PassLength.grid(row=1, column=1)
Label(root, text='Password').grid(row=2, sticky=W, padx=4)
PassField = Entry(root)
PassField.grid(row=2, column=1, sticky=E, pady=4)
generateButton = Button(root, text='Generate')
generateButton.bind("<Button-1>", pass_gen)
generateButton.grid(row=2, column=2,sticky=E)
root.mainloop()