我的代碼:
from tkinter import *
from tkinter.ttk import Combobox
import random
screen = Tk()
screen.title("Password Generator")
screen.geometry('600x400')
screen.configure(background ="gray")
def Password_Gen():
global sc1
sc1.set("")
passw=""
length=int(c1.get())
lowercase='abcdefghijklmnopqrstuvwxyz'
uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ' lowercase
mixs='0123456789' lowercase uppercase '@#$%&*'
if c2.get()=='Low Strength':
for i in range(0,length):
passw=passw random.choice(lowercase)
sc1.set(passw)
elif c2.get()=='Medium Strength':
for i in range(0,length):
passw=passw random.choice(uppercase)
sc1.set(passw)
elif c2.get()=='High Strength':
for i in range(0,length):
passw=passw random.choice(mixs)
sc1.set(passw)
sc1=StringVar('')
t1=Label(screen,text='Password Generator',font=('Arial',25),fg='green',background ="gray")
t1.place(x=60,y=0)
t2=Label(screen,text='Password:',font=('Arial',14),background ="gray")
t2.place(x=145,y=90)
il=Entry(screen,font=('Arial',14),textvariable=sc1)
il.place(x=270,y=90)
t3=Label(screen,text='Length: ',font=('Arial',14),background ="gray")
t3.place(x=145,y=120)
t4=Label(screen,text='Strength:',font=('Arial',14),background ="gray")
t4.place(x=145,y=155)
c1=Entry(screen,font=('Arial',14),width=10)
c1.place(x=230,y=120)
c2=Combobox(screen,font=('Arial',14),width=15)
c2['values']=('Low Strength','Medium Strength','High Strength')
c2.current(1)
c2.place(x=237,y=155)
b=Button(screen,text='Generate!',font=('Arial',14),fg='green',background ="gray",command=gen)
b.place(x=230,y=195)
screen.mainloop()
出于某種原因,我不斷收到這些錯誤:
Traceback (most recent call last):
File "C:\Users\z\3D Objects\birthday\passwordgeneratorgui.py", line 32, in <module>
sc1=StringVar('')
File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 540, in __init__
Variable.__init__(self, master, value, name)
File "C:\Users\z\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 372, in __init__
self._root = master._root()
AttributeError: 'str' object has no attribute '_root'
我該如何解決這些錯誤?
uj5u.com熱心網友回復:
這是因為 StringVar 的第一個引數應該是“容器”。您正在傳遞一個空字串而不是那個。sc1=StringVar('')用替換該行sc1=StringVar()。您還可以閱讀這樣的 StringVars 指南:https ://www.pythontutorial.net/tkinter/tkinter-stringvar/
您的代碼中的另一個錯誤是在這一行:
b=Button(screen,text='Generate!',font=('Arial',14),fg='green',background ="gray",command=gen)
問題是您傳入了一個gen不存在的函式。我猜你想Password_Gen用這個按鈕呼叫這個函式。因此,將其替換為以下行:
b=Button(screen,text='Generate!',font=('Arial',14),fg='green',background ="gray",command=Password_Gen)
編輯:
我還注意到您的密碼生成器有一個小問題。
lowercase='abcdefghijklmnopqrstuvwxyz'
uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ' lowercase
mixs='0123456789' lowercase uppercase '@#$%&*'
在mixs變數中,會有兩次小寫字母,我不認為你想要這個。我想你想要的是這個(因為小寫字母已經包含在你的uppercase變數中):
mixs='0123456789' uppercase '@#$%&*'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435640.html
