Python---密码生成器

Python—密码生成器

有一批服务器需要定时更新密码,密码要求最小14位,带大小写,数字,字符.由于这些机器需要我这边给密码,每次想密码又很麻烦,且这个环境与外网绝对隔离,也用不了一些其他工具.只能手写一个py用来创建

1. 代码部分

import tkinter

# 随机密码生成器
# 密码类
class Password:
    def __init__(self, length):
        self.length = length
        self.passwordlist = []
        self.num = 0
        self.az = 0
        self.AZ = 0
        self.other = 0
        self.prefix = ""

    def generate(self):
        import random
        temp = ''
        self.passwordlist = []
        pwsd = ''
        if self.num == 1:
            pwsd += '0123456789'
        if self.az == 1:
            pwsd += 'abcdefghijklmnopqrstuvwxyz'
        if self.AZ == 1:
            pwsd += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        if self.other == 1:
            pwsd += '~!@#$%^&*()_+-=,./<>?;:[]{}|'
        # 生成5个随机密码
        for i in range(5):
            for j in range(self.length - len(self.prefix)):
                temp += random.choice(pwsd)
            self.passwordlist.append(self.prefix+temp)
            temp = ''

# 窗口类
class Window:
    def __init__(self):
        self.window = tkinter.Tk()
        self.window.title('随机密码生成器--Infra 仇沁')
        self.window.geometry('400x340')
        self.window.resizable(0, 0)
        # 生成5格密码 5个复制按钮
        self.passwordlist = []
        self.copybuttonlist = []
        for i in range(5):
            self.passwordlist.append(tkinter.StringVar())
            self.passwordlist[i].set('密码' + str(i + 1))
            tkinter.Label(self.window, textvariable=self.passwordlist[i]).place(x=20, y=20 + i * 40, width=260, height=30)

        # 生成复制按钮,每个按钮对应一个密码
        for i in range(5):
            self.copybuttonlist.append(tkinter.Button(self.window, text='复制', command=lambda i=i: self.copy(i)))
            self.copybuttonlist[i].place(x=300, y=20 + i * 40, width=80, height=30)
        # 生成密码按钮
        self.generatebutton = tkinter.Button(self.window, text='生成密码', command=self.generate)
        self.generatebutton.place(x=20, y=300, width=360, height=30)
        # 密码长度
        tkinter.Label(self.window, text='密码长度').place(x=20, y=260, width=80, height=30)
        self.length = tkinter.StringVar()
        self.length.set('14')
        tkinter.Entry(self.window, textvariable=self.length).place(x=100, y=260, width=20, height=30)
        # 密码类型

        self.num = tkinter.IntVar()
        self.num.set(1)
        tkinter.Checkbutton(self.window, text='数字', variable=self.num).place(
            x=120, y=260, width=80, height=30)
        self.az = tkinter.IntVar()
        self.az.set(1)
        tkinter.Checkbutton(self.window, text='小写', variable=self.az).place(
            x=180, y=260, width=80, height=30)
        self.AZ = tkinter.IntVar()
        self.AZ.set(1)
        tkinter.Checkbutton(self.window, text='大写', variable=self.AZ).place(
            x=240, y=260, width=80, height=30)
        self.other = tkinter.IntVar()
        self.other.set(1)
        tkinter.Checkbutton(self.window, text='特殊字符', variable=self.other).place(
            x=300, y=260, width=80, height=30)
        # 密码前缀
        tkinter.Label(self.window, text='密码前缀').place(x=20, y=220, width=80, height=30)
        self.prefix = tkinter.StringVar()
        self.prefix.set('Pana#')
        tkinter.Entry(self.window, textvariable=self.prefix).place(x=100, y=220, width=80, height=30)

    def generate(self):
        # 实例化密码类
        pwsd1 = Password(int(self.length.get()))
        pwsd1.num = self.num.get()
        pwsd1.az = self.az.get()
        pwsd1.AZ = self.AZ.get()
        pwsd1.other = self.other.get()
        pwsd1.prefix = self.prefix.get()
        pwsd1.generate()
        for i in range(5):
            self.passwordlist[i].set(pwsd1.passwordlist[i])

    def copy(self,i):
        print(i)
        pass
        #print(i)
        self.window.clipboard_clear()
        self.window.clipboard_append(self.passwordlist[i].get())

if __name__ == "__main__":
    window = Window()
    window.window.mainloop()

2. 效果

Python---密码生成器_第1张图片
Python---密码生成器_第2张图片

你可能感兴趣的:(python,python,开发语言)