2016-07-29 214 views
0

我試圖構建一個創建密碼的gui,並且儘可能地生成密碼並使其顯示在標籤中。然而,當多次點擊按鈕時,舊密碼看起來並不會消失,只是重疊在頂部。我也遇到了一個我似乎無法糾正的錯誤,儘管它似乎不影響gui。Python Tkinter標籤刷新

到目前爲止的代碼是:

from tkinter import * 
import random 

myGui = Tk() 
myGui.geometry('300x200+700+250') 
myGui.title('Password Generator') 

def passwordgen(): 
    password = '' 

    for i in range(8): 
     ##----runs the for loop 8 times 
     if (i == 0) or (i == 4): 
      password = password + chr(random.randint(97, 122)) 

     if (i == 1) or (i == 5): 
      password = password + chr(random.randint(65, 90)) 

     if (i == 2) or (i == 6): 
      password = password + chr(random.randint(48, 57)) 

     if (i == 3) or (i == 7): 
      password = password + chr(random.randint(33, 47)) 

    passLabel = Label(myGui, text=password) 
    passLabel.grid(row=0, column=1, sticky=E) 

genPassBtn = Button(myGui, text="Generate Password", command=passwordgen) 
genPassBtn.bind("<Button-1>", passwordgen) 
genPassBtn.grid(row=0, column=0, sticky=W) 

myGui.mainloop() 

我收到的錯誤是:

return self.func(*args) 
TypeError: passwordgen() takes 0 positional arguments but 1 was given 

我希望達到的結果是創建生成密碼的GUI,生成一個散列生成密碼的值,檢查密碼強度,將生成的散列加載到文本文件,然後可以根據存儲的散列驗證密碼。

此外,從收到的建議,我修改了代碼,並增加了額外的檢查力量。現在,該代碼如下所示:

from tkinter import * 
import random 

myGui = Tk() 
myGui.geometry('300x200+700+250') 
myGui.title('Password Generator') 

def passwordgen(): 
    password = '' 

    for i in range(8): 
     ##----runs the for loop 8 times 
     if (i == 0) or (i == 4): 
      password = password + chr(random.randint(97, 122)) 

     if (i == 1) or (i == 5): 
      password = password + chr(random.randint(65, 90)) 

     if (i == 2) or (i == 6): 
      password = password + chr(random.randint(48, 57)) 

     if (i == 3) or (i == 7): 
      password = password + chr(random.randint(33, 47)) 

    strPassword.set(password) 


def checkPassword(): 

    strength = ['Blank', 'Very Weak', 'Weak', 'Medium', 'Strong', 'Very Strong'] 
    score = 1 
    password = strPassword.get() 

    if len(password) < 1: 
     return strength[0] 

    if len(password) < 4: 
     return strength[1] 

    if len(password) >= 8: 
     score += 1 

    if re.search('[0-9]', password): 
     score += 1 

    if re.search('[a-z]', password) and re.search('[A-Z]', password): 
     score += 1 

    if re.search('.', password): 
     score += 1 

    passwordStrength.set(strength[score]) 

genPassBtn = Button(myGui, text="Generate Password", command=passwordgen) 
strPassword = StringVar() 

lblPassword = Label(myGui, textvariable=strPassword) 
lblPassword.grid(row=0, column=1, sticky=W) 
genPassBtn.grid(row=0, column=0, sticky=W) 

passwordStrength = StringVar() 
checkStrBtn = Button(myGui, text="Check Strength", command=checkPassword) 
checkStrBtn.grid(row=1, column=0) 

checkStrLab = Label(myGui, textvariable=passwordStrength) 
checkStrLab.grid(row=1, column=1) 

myGui.mainloop() 

回答

2

試試這個例子。

from tkinter import * 
import random 

myGui = Tk() 
myGui.geometry('300x200+700+250') 
myGui.title('Password Generator') 

def passwordgen(): 
    password = '' 

    for i in range(8): 
     ##----runs the for loop 8 times 
     if (i == 0) or (i == 4): 
      password = password + chr(random.randint(97, 122)) 

     if (i == 1) or (i == 5): 
      password = password + chr(random.randint(65, 90)) 

     if (i == 2) or (i == 6): 
      password = password + chr(random.randint(48, 57)) 

     if (i == 3) or (i == 7): 
      password = password + chr(random.randint(33, 47)) 

    strPassword.set(password) 

genPassBtn = Button(myGui, text="Generate Password", command=passwordgen) 
strPassword = StringVar() 
lblPassword = Label(myGui, textvariable=strPassword) 
lblPassword.grid(row=0,column=1, sticky=W) 
genPassBtn.grid(row=0, column=0, sticky=W) 

myGui.mainloop() 

這裏是我做了什麼

  1. 而不是每次都創建一個新的標籤,我更改使用STRINGVAR一個標籤叫strPassword的文本。
  2. 你不需要將一個按鈕綁定到一個點擊來調用一個函數,使用Button(...,command = myFunction)已經這樣做了。
+0

非常感謝斯科蒂。我現在可以使用strPassword並檢查強度嗎? – JSmith

+0

我不確定你的意思是什麼,但你可以使用strPassword.get()返回字符串的內容以用於其他目的 – scotty3785

+0

我想說這個密碼非常弱,弱,中,強等。 aginst一組參數,如小寫,大寫,數字或特殊字符。 – JSmith