Python使用选项生成密码

时间:2018-01-19 12:42:48

标签: python python-3.x pyqt pyqt5

我已经为密码生成设置了PyQt5 gui。

Gui Here

它有多个修改密码的选项,但到目前为止我能想出的最好的方法是使用修改器来生成密码

def generate_click(self,value):
    password_length     = self.horizontalSlider.value() # Comes Back as a int
    password_uppercase  = self.checkBox.isChecked()     # Comes Back True/False
    password_lowercase  = self.checkBox_2.isChecked()   # Comes Back True/False
    password_numbers    = self.checkBox_3.isChecked()   # Comes Back True/False
    password_special    = self.checkBox_4.isChecked()   # Comes Back True/False

    print(password_length, password_uppercase, password_lowercase, password_numbers, password_special)



    if (password_uppercase == True and password_lowercase == True and password_numbers == True and password_special == True):

        alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"

    if (password_uppercase == True and password_lowercase == True and password_numbers == True and password_special == True):


    if (password_uppercase == True and password_lowercase == True and password_numbers == True and password_special == True):


    if (password_uppercase == True and password_lowercase == True and password_numbers == True and password_special == True):

有更好的方法吗?谢谢

2 个答案:

答案 0 :(得分:0)

我这样做的方式如下:

alphabet = ''
if password_uppercase:
    alphabet += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if password_lowercase:
    alphabet += "abcdefghijklmnopqrstuvwxyz"
if password_numbers:
    alphabet += "0123456789"
if password_special:
    alphabet += "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"

# if None was selected, add a default
alphabet = alphabet or "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

另请注意,由于password_uppercasepassword_lowercase等变量是布尔值,因此对它们的值进行比较是非常多余的。我建议你使用password_uppercase而不是password_uppercase == Truenot password_uppercase代替password_uppercase == False

答案 1 :(得分:0)

正如@Jon Clements建议您可能想要做的事情:

import string
import random

def generate_click(self,value):
    password_length     = self.horizontalSlider.value() # Comes Back as a int
    password_uppercase  = self.checkBox.isChecked()     # Comes Back True/False
    password_lowercase  = self.checkBox_2.isChecked()   # Comes Back True/False
    password_numbers    = self.checkBox_3.isChecked()   # Comes Back True/False
    password_special    = self.checkBox_4.isChecked()   # Comes Back True/False

    print(password_length, password_uppercase, password_lowercase, password_numbers, password_special)

    # Initialize the set of character to choose from
    l = []

    # Check your different flags and extend the list accordingly
    if password_uppercase:
        l.extend(string.ascii_uppercase)
    if password_lowercase:
        l.extend(string.ascii_lowercase)
    if password_numbers:
        l.extend(string.digits)
    if password_special:
        l.extend(string.punctuation)

    # Build your password
    try:
        password = "".join(random.choices(l, k=password_length))
    except IndexError:  # Necessary if no default set of characters
        password = ""

    # Display your password

使用字符串模块(在python中始终可用),意味着您不必键入可用字符集。 希望这有助于您的努力。