随机密码生成器(去重复字符,可自定义长度和字符集)(GUI非原创)
本帖最后由 hrh123 于 2023-5-3 21:27 编辑一个Python的随机字符生成器,因为不会写GUI所以GUI部分是AI代写的{:1_924:},第一次发帖大佬轻喷
import random
import string
import tkinter as tk
def generate_random_string():
try:
length = int(entry_length.get())
if length <= 0:
label_result.config(text="错误:长度必须是正整数")
return ""
except ValueError:
label_result.config(text="错误:请输入一个整数")
return ""
allowed_chars = ""
if var_upper.get():
allowed_chars += string.ascii_uppercase
if var_lower.get():
allowed_chars += string.ascii_lowercase
if var_digit.get():
allowed_chars += string.digits
if var_punct.get():
allowed_chars += string.punctuation
if not allowed_chars:
label_result.config(text="错误:请至少选择一种字符集")
return ""
random_string = "".join(random.choice(allowed_chars)
for _ in range(length))
def has_duplicates(s):
return len(s) != len(set(s))
def replace_duplicates(s):
new_s = ""
for ch in s:
if s.count(ch) > 1:
new_ch = random.choice(allowed_chars)
new_s += new_ch
else:
new_s += ch
return new_s
def remove_duplicates(s):
while has_duplicates(s):
s = replace_duplicates(s)
return s
rs = remove_duplicates(random_string)
return rs
def show_and_copy_random_string():
random_string = generate_random_string()
if random_string:
label_result.config(text=random_string)
window.clipboard_clear()
window.clipboard_append(random_string)
window = tk.Tk()
window.title("随机字符串生成器")
window.geometry("400x300")
label_welcome = tk.Label(window, text="欢迎使用随机字符串生成器!")
label_welcome.pack()
label_length = tk.Label(window, text="请输入你想要生成的字符串长度(正整数):")
label_length.pack()
entry_length = tk.Entry(window)
entry_length.pack()
var_upper = tk.BooleanVar()
var_lower = tk.BooleanVar()
var_digit = tk.BooleanVar()
var_punct = tk.BooleanVar()
check_upper = tk.Checkbutton(window, text="大写字母", variable=var_upper)
check_upper.pack()
check_lower = tk.Checkbutton(window, text="小写字母", variable=var_lower)
check_lower.pack()
check_digit = tk.Checkbutton(window, text="数字", variable=var_digit)
check_digit.pack()
check_punct = tk.Checkbutton(window, text="特殊字符", variable=var_punct)
check_punct.pack()
button_generate = tk.Button(window, text="一键生成并复制",
command=show_and_copy_random_string)
button_generate.pack()
label_result = tk.Label(window, text="")
label_result.pack()
window.mainloop()
本帖最后由 hrh123 于 2023-5-4 00:23 编辑
既然有人需要,那我提供个完整的包含pyc和exe的吧
https://1drv.ms/u/s!Al_XmUrVmOqAhAF4hTOxDPKpzlQA?e=2WrBAG
https://drive.google.com/file/d/1pRQdgZI0txMhoQBbYslbYeiIesindkND/view?usp=share_link
(OneDrive和Google云盘国内访问情况不佳,如果有不嫌麻烦的可以帮忙分流) 这几天自己鼓捣了下,觉得需要增加一个是否去重复字符的选项以满足实际需求,于是小改了一下代码(其他很多生成器都有的前后缀和批量生成等功能我认为会降低安全性,不符合我的初衷,包括生成和复制按钮在一起也是为了有人获取剪切板信息后起混淆作用).
新的代码如下:import random
import string
import tkinter as tk
def generate_random_string():
try:
length = int(entry_length.get())
if length <= 0:
label_result.config(text="错误:长度必须是正整数")
return ""
except ValueError:
label_result.config(text="错误:请输入一个整数")
return ""
allowed_chars = ""
if var_upper.get():
allowed_chars += string.ascii_uppercase
if var_lower.get():
allowed_chars += string.ascii_lowercase
if var_digit.get():
allowed_chars += string.digits
if var_punct.get():
allowed_chars += string.punctuation
if not allowed_chars:
label_result.config(text="错误:请至少选择一种字符集")
return ""
random_string = "".join(random.choice(allowed_chars)
for _ in range(length))
def has_duplicates(s):
return len(s) != len(set(s))
def replace_duplicates(s):
new_s = ""
for ch in s:
if s.count(ch) > 1:
new_ch = random.choice(allowed_chars)
new_s += new_ch
else:
new_s += ch
return new_s
def remove_duplicates(s):
while has_duplicates(s):
s = replace_duplicates(s)
return s
if var_remove.get():
rs = remove_duplicates(random_string)
else:
rs = random_string
return rs
def show_and_copy_random_string():
random_string = generate_random_string()
if random_string:
label_result.config(text=random_string)
window.clipboard_clear()
window.clipboard_append(random_string)
window = tk.Tk()
window.title("随机字符串生成器")
window.geometry("400x300")
label_welcome = tk.Label(window, text="欢迎使用随机字符串生成器!")
label_welcome.pack()
label_length = tk.Label(window, text="请输入你想要生成的字符串长度(正整数):")
label_length.pack()
entry_length = tk.Entry(window)
entry_length.pack()
var_upper = tk.BooleanVar()
var_lower = tk.BooleanVar()
var_digit = tk.BooleanVar()
var_punct = tk.BooleanVar()
check_upper = tk.Checkbutton(window, text="大写字母", variable=var_upper)
check_upper.pack()
check_lower = tk.Checkbutton(window, text="小写字母", variable=var_lower)
check_lower.pack()
check_digit = tk.Checkbutton(window, text="数字", variable=var_digit)
check_digit.pack()
check_punct = tk.Checkbutton(window, text="特殊字符", variable=var_punct)
check_punct.pack()
var_remove = tk.BooleanVar()
check_remove = tk.Checkbutton(window, text="去重复字符", variable=var_remove)
check_remove.pack()
button_generate = tk.Button(window, text="一键生成并复制",
command=show_and_copy_random_string)
button_generate.pack()
label_result = tk.Label(window, text="")
label_result.pack()
window.mainloop()
本帖最后由 hrh123 于 2023-5-4 20:49 编辑
waxxy 发表于 2023-5-4 20:41
感谢楼主回复!这样一来我就明白,原来有这么高的性能。同时,还学到了GitHub上还有私有库这玩意儿,请问 ...
如果您想在GitHub上创建一个私有库,仅仅只需要一个账号(需要邮箱注册),在创建仓库时选择"Private"即可.当然,如果想流畅使用Github您可能需要有英文和Git的基础,不过您若是只想来储存密码的话,那么简单了解下GitHub的功能及使用即可,您可以参考GitHub Docs(https://docs.github.com/zh)来学习 试了一下,还不错。打包成单独文件8700K,真大 有成品吗? 本帖最后由 hrh123 于 2023-5-3 20:05 编辑
zgmn001 发表于 2023-5-3 18:38
有成品吗?
这不就是成品?如果您要exe的话,看9楼 谢谢@Thanks!{:1_921:} homehome 发表于 2023-5-3 16:36
试了一下,还不错。打包成单独文件8700K,真大
python适合作为脚本用,一打包很大,用虚拟环境打包能好点
生成一个GUI版本
https://wwi.lanzoup.com/iOOPu0ul8gxe
密码:52pj 学习一下,感谢分享