[Python] 纯文本查看 复制代码
import random
import string
import PySimpleGUI as sg
from pyperclip import copy
def get_passwords(xiaoxie=True,
daxie=True,
shuzi=True,
biaodian=True,
zidingyi='',
qianzhui='',
zhongjian='',
houzhui='',
paichu='',
pwd_len=11,
pwd_count=1):
pwd_list = []
if not any([xiaoxie, daxie, shuzi, biaodian]):
return '至少要勾选一种密码类型!'
words = zidingyi
if xiaoxie:
words += string.ascii_lowercase # 小写字母
if daxie:
words += string.ascii_uppercase # 大写字母
if shuzi:
words += string.digits # 数字
if biaodian:
words += string.punctuation # 标点
# 规避样本数量 < 密码长度的问题
if len(words) <= pwd_len:
words = words * (pwd_len // len(words) + 1)
zhui_len = len(f'{qianzhui}{zhongjian}{houzhui}')
if pwd_len < zhui_len:
return '密码长度<前后缀的长度之和!'.split() * 3
for _ in range(pwd_count):
chosen = ''.join(random.sample(words, pwd_len)) # 生产随机列表
chosen = f'{qianzhui}{chosen}{houzhui}' # 加前后缀
half_chosen = len(chosen) // 2
chosen = f'{chosen[:half_chosen]}{zhongjian}{chosen[half_chosen:]}'
if len(paichu) > 0: # 排除字符
exclude_chars = set(paichu)
exclude_table = str.maketrans('', '', ''.join(exclude_chars))
chosen = chosen.translate(exclude_table)
pwd_list.append(chosen.strip()) # 组成字符串
return [
f'{str(n).zfill(len(str(pwd_count)))}. {i}'
for n, i in enumerate(pwd_list, 1)
]
def gui():
sg.theme('GrayGrayGray') # 设置当前主题
xuan_frame = [
sg.Checkbox(
'小写a-z',
default=True,
key='小写',
),
sg.Checkbox(
'大写A-Z',
default=True,
key='大写',
),
sg.Checkbox(
'数字0-9',
default=True,
key='数字',
),
sg.Checkbox(
'标点',
default=True,
key='标点',
),
],
zidingyi_frame = [
sg.Text('自定义字:'),
sg.InputText(
key='自定义',
size=(40, 11),
),
],
zhui_frame = [
sg.Text(' 前缀:'),
sg.InputText(
key='前缀',
size=(5, 11),
),
sg.Text(' 中间字符:'),
sg.InputText(
key='中间字符',
size=(5, 11),
),
sg.Text(' 后缀:'),
sg.InputText(
key='后缀',
size=(5, 11),
),
],
paichu_frame = [
sg.Text('排除字符(不要的):'),
sg.InputText(
key='排除',
size=(40, 11),
),
],
chang_frame = [
sg.Text('密码长度'),
sg.Slider(
range=(5, 80),
default_value=6,
orientation='horizontal', # 滑块方向
tick_interval=20, # 显示的步长
enable_events=True,
key='长度',
size=(41, 20))
],
shu_frame = [
sg.Text('密码个数'),
sg.Slider(
range=(5, 650),
default_value=5,
orientation='horizontal', # 滑块方向
tick_interval=80, # 显示的步长
enable_events=True,
key='个数',
size=(41, 20))
],
layout = [[
sg.Frame(
'',
xuan_frame,
size=(400, 40),
pad=(5),
),
], [
sg.Frame(
'',
zidingyi_frame,
size=(400, 40),
pad=(5),
),
], [
sg.Frame(
'',
zhui_frame,
size=(400, 40),
pad=(5),
),
], [sg.Frame(
'',
paichu_frame,
size=(400, 40),
pad=(5),
)], [sg.Frame(
'',
chang_frame,
size=(400, 80),
pad=(5),
)], [sg.Frame(
'',
shu_frame,
size=(400, 80),
pad=(5),
)],
[
sg.Listbox([],
key='结果',
size=(57, 10),
horizontal_scroll=True,
highlight_background_color='gray',
font=('宋体', 10),
enable_events=True,
pad=(5, 10))
],
[
sg.Button('清空', key='清空', pad=(10, 10), enable_events=True),
sg.Button(
'复制所有密码',
key='复制所有密码',
button_color='#118890',
enable_events=True,
),
],
[
sg.Text(
'取消或勾选密码类型后,要拖动滑块,才能重新生成',
text_color='#0000ff',
key='提示',
font=('宋体', 10),
justification='center',
)
]]
# 创建展示窗口8
window = sg.Window('随机密码 - v1.9',
layout,
font=('微软雅黑', 10),
size=(400, 610),
element_justification='center')
# 事件循环并获取输入值
while True:
event, values = window.read()
if not event:
break
elif event != '复制所有密码': # 任何事件都会生成密码
pwds = get_passwords(values['小写'], values['大写'], values['数字'],
values['标点'], values['自定义'], values['前缀'], values['中间字符'],
values['后缀'], values['排除'], int(values['长度']),
int(values['个数']))
# print(event, values)
if event in ['个数', '长度']:
window['结果'].update(pwds)
window['提示'].update('点击单条密码,可复制到剪贴板')
if event == '结果':
try:
pwd = values['结果'][0].split('.', 1)[1].strip()
copy(pwd)
window['提示'].update(f'已复制:\n{pwd}')
except:
window['提示'].update('取消或勾选密码类型后,要拖动滑块,才能重新生成')
if event == '清空':
window['长度'].update(0)
window['个数'].update(0)
window['结果'].update('')
window['提示'].update('')
window['自定义'].update('')
window['前缀'].update('')
window['中间字符'].update('')
window['后缀'].update('')
window['排除'].update('')
if event == '复制所有密码':
try:
passwords = [i.split('.', 1)[1].strip() for i in pwds]
copy('\n'.join(passwords))
window['提示'].update(f'【所有】密码({len(pwds)}条)已复制到剪贴板')
except:
window['提示'].update('取消或勾选密码类型后,要拖动滑块,才能重新生成')
window.close()
if __name__ == '__main__':
gui()