-1
我正在構建一個簡單的tkinter密碼程序,並且遇到問題。我收到以下錯誤信息:使用for循環將值插入到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
你期待什麼'結束=「」'做什麼?此外,您每次都通過循環顯式刪除內容。你爲什麼這樣做? –
經過一番嘗試,我知道我的問題是什麼。不知道如何解決它。我正在使用end =''希望它將密碼中的單個字符連接到選定的長度。 –