零Python基础编程密码生成器

本文最后更新于:2024年10月2日 下午

前言

  1. 耗子:主要还是要实现什么功能,你也能看到此篇标题就是关于AI 计算机 编程这三个标签,如果你问为什么有AI这个标签的话,那就是本文的目的,编程小白通过AI也能玩转编程,其实耗子不是第一次进行这种编程,之前的html时钟也是这么做的https://www.hzopo.top/2023/06/27/timeclock/
  2. 利用ai进行编程,虽然我不懂,但是我可以让ai帮我写注释,虽然没什么程序设计的基础,但是我们有ai,这就是现在的时代和以前截然不同的地方,以前是不会可以问老师,问专家等等,现在是不会直接问ai,即使会有错误,但是大体上的效率是比正常人高的,可以说这种编程水平已经超过了很多中等级别的程序员,你想反驳我?你可以一分钟进行328行的格式化输出程序吗,甚至有时候一个bug都没有

密码生成器

实际上这个想法的由来是因为我的朋友做了这个,我的朋友的网站是https://www.dingview.top/
而且生成的密码可以进行github的备份,然后我才去做这个东西,他的软件端是cmd命令行这样的界面

1
2
3
4
5
6
7
请输入密码名字和长度,例如 'abc 53'
是否包含大写字母? (y/n):
是否包含小写字母? (y/n):
是否包含符号? (y/n):
是否包含中文字符? (y/n):
是否包含泰语字符? (y/n):
然后再次回车之后即可生成密码markdown在同文件夹内.

密码生成器演示

语言描述:

  • 可选包含大写字母,小写字母,数字,特殊符号的密码生成。
  • 实现了密码生成的点击复制
  • 实现了密码点击按钮显示隐藏
  • 实现了密码另存为markdown
  • 实现了点击查看密码的历史记录(仅在本次打开时候记录,关闭后清空)
  • 实现了点击清除输入的密码长度和生成的密码
  • 实现了没用的设置界面
  • 实现了多语言的界面语言(实际上那四个选项勾选的忘记汉化了)
    • 简体中文
    • English
    • 日本语
    • 俄语

关于密码生成器源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import random
import string
import tkinter as tk
from tkinter import messagebox, ttk, filedialog
import os
import pyperclip
import json
import sys

# 初始化
password_history = [] # 密码历史记录
default_settings = {
"include_lower": True,
"include_upper": True,
"include_digits": True,
"include_special": True
}


def resource_path(relative_path):
"""获取资源文件的路径"""
try:
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)


def load_settings():
"""加载设置"""
if os.path.exists(resource_path("settings.json")):
with open(resource_path("settings.json"), "r", encoding="utf-8") as file:
return json.load(file)
return default_settings


def save_settings(settings):
"""保存设置"""
with open(resource_path("settings.json"), "w", encoding="utf-8") as file:
json.dump(settings, file, ensure_ascii=False, indent=4)


def load_translations(language):
"""加载翻译文件"""
file_name = f"translations_{language}.json"
file_path = resource_path(file_name)
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return json.load(file)
return {}


def translate(key):
"""根据当前语言返回翻译文本"""
return translations.get(key, key) # 如果找不到翻译,则返回原始文本


def generate_password(length, include_lower, include_upper, include_digits, include_special):
if length < 4:
messagebox.showerror(translate("error_length"), translate("error_length_msg"))
return None

char_set = ""
if include_lower: char_set += string.ascii_lowercase
if include_upper: char_set += string.ascii_uppercase
if include_digits: char_set += string.digits
if include_special: char_set += string.punctuation

if not char_set:
messagebox.showerror(translate("error_no_char_type"), translate("error_no_char_type_msg"))
return None

password = ''.join(random.choice(char_set) for _ in range(length))
password_history.append(password)

return password


def on_generate():
try:
length = int(length_entry.get())
if length <= 0:
messagebox.showerror(translate("error_invalid_length"), translate("error_invalid_length_msg"))
return

password = generate_password(
length,
lower_var.get(),
upper_var.get(),
digits_var.get(),
special_var.get()
)
if password:
password_entry.delete(0, tk.END)
password_entry.insert(0, password)
update_strength_label(password)
except ValueError:
messagebox.showerror(translate("error_invalid_length"), translate("error_invalid_length_msg"))


def open_settings_window():
settings_window = tk.Toplevel(root)
settings_window.title(translate("settings"))
settings_window.geometry("400x400")

ttk.Label(settings_window, text=translate("settings"), font=font_medium, background="#E0E0E0",
foreground="black").pack(padx=10, pady=10)

include_lower_var = tk.BooleanVar(value=settings["include_lower"])
include_upper_var = tk.BooleanVar(value=settings["include_upper"])
include_digits_var = tk.BooleanVar(value=settings["include_digits"])
include_special_var = tk.BooleanVar(value=settings["include_special"])

settings_frame = ttk.Frame(settings_window, padding=10)
settings_frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

ttk.Checkbutton(settings_frame, text=translate("include_lower"), variable=include_lower_var,
style='TCheckbutton').grid(row=0, column=0, sticky=tk.W, padx=10, pady=5)
ttk.Checkbutton(settings_frame, text=translate("include_upper"), variable=include_upper_var,
style='TCheckbutton').grid(row=1, column=0, sticky=tk.W, padx=10, pady=5)
ttk.Checkbutton(settings_frame, text=translate("include_digits"), variable=include_digits_var,
style='TCheckbutton').grid(row=2, column=0, sticky=tk.W, padx=10, pady=5)
ttk.Checkbutton(settings_frame, text=translate("include_special"), variable=include_special_var,
style='TCheckbutton').grid(row=3, column=0, sticky=tk.W, padx=10, pady=5)

ttk.Button(settings_frame, text=translate("save_settings"),
command=lambda: save_user_settings(settings_window, include_lower_var, include_upper_var,
include_digits_var, include_special_var)).grid(row=4, column=0,
pady=10)


def save_user_settings(window, lower_var, upper_var, digits_var, special_var):
settings.update({
"include_lower": lower_var.get(),
"include_upper": upper_var.get(),
"include_digits": digits_var.get(),
"include_special": special_var.get()
})
save_settings(settings)
window.destroy()
messagebox.showinfo(translate("settings_saved"))


def update_language(language):
global translations, current_language
current_language = language
translations = load_translations(current_language)
update_ui_text()


def update_ui_text():
length_label.config(text=translate("password_length"))
generate_button.config(text=translate("generate_password"))
password_label.config(text=translate("generated_password"))
toggle_button.config(text=translate("toggle_password"))
copy_button.config(text=translate("copy_password"))
clear_button.config(text=translate("clear_input"))
save_as_button.config(text=translate("save_as"))
history_button.config(text=translate("view_history"))
settings_button.config(text=translate("settings"))
strength_label.config(text=translate("password_strength") + ": ")

# 更新语言下拉菜单
language_menu['menu'].delete(0, 'end')
for lang, lang_name in [("zh", "简体中文"), ("en", "English"), ("ja", "日本語"), ("ru", "Русский")]:
language_menu['menu'].add_command(label=lang_name, command=lambda l=lang: update_language(l))


def save_as():
password = password_entry.get()
if password:
file_path = filedialog.asksaveasfilename(defaultextension=".md",
filetypes=[("Markdown 文件", "*.md"), ("所有文件", "*.*")],
initialdir=os.getcwd(),
title=translate("save_as"))
if file_path:
try:
with open(file_path, "w", encoding="utf-8") as file:
file.write(f"# 生成的密码\n\n生成的密码是: `{password}`")
messagebox.showinfo(translate("success_save") + file_path)
except Exception as e:
messagebox.showerror(translate("error_save"), str(e))
else:
messagebox.showwarning(translate("warning_empty_field"))


def copy_to_clipboard():
password = password_entry.get()
if password:
pyperclip.copy(password)
messagebox.showinfo(translate("success_copy"))
else:
messagebox.showwarning(translate("warning_empty_field"))


def toggle_password_visibility():
if password_entry.cget("show") == "*":
password_entry.config(show="")
toggle_button.config(text=translate("show_password"))
else:
password_entry.config(show="*")
toggle_button.config(text=translate("toggle_password"))


def clear_fields():
length_entry.delete(0, tk.END)
password_entry.delete(0, tk.END)
lower_var.set(settings["include_lower"])
upper_var.set(settings["include_upper"])
digits_var.set(settings["include_digits"])
special_var.set(settings["include_special"])
strength_label.config(text="")


def show_history():
if password_history:
history_window = tk.Toplevel(root)
history_window.title(translate("view_history"))
history_listbox = tk.Listbox(history_window, width=50, height=10, foreground='black', background='#F0F0F0')
history_listbox.pack(padx=10, pady=10)
for pwd in password_history:
history_listbox.insert(tk.END, pwd)
else:
messagebox.showinfo(translate("history_empty"))


# 加载设置和翻译
settings = load_settings()
current_language = "zh" # 默认为中文
translations = load_translations(current_language)

# 创建主窗口
root = tk.Tk()
root.title(translate("password_generator"))

# 创建字体
font_medium = ('Helvetica', 12)

# 创建样式
style = ttk.Style()
style.configure('TLabel', background='#F0F0F0', foreground='black')
style.configure('TButton', background='#4CAF50', foreground='black', padding=6)
style.configure('TCheckbutton', background='#F0F0F0', foreground='black')

# 创建界面组件
length_label = ttk.Label(root, text=translate("password_length"), font=font_medium)
length_label.grid(row=0, column=0, padx=10, pady=10, sticky="w")

length_entry = ttk.Entry(root, font=font_medium)
length_entry.grid(row=0, column=1, padx=10, pady=10)

lower_var = tk.BooleanVar(value=settings["include_lower"])
upper_var = tk.BooleanVar(value=settings["include_upper"])
digits_var = tk.BooleanVar(value=settings["include_digits"])
special_var = tk.BooleanVar(value=settings["include_special"])

lower_check = ttk.Checkbutton(root, text=translate("include_lower"), variable=lower_var)
lower_check.grid(row=1, column=0, padx=10, pady=5, sticky="w")

upper_check = ttk.Checkbutton(root, text=translate("include_upper"), variable=upper_var)
upper_check.grid(row=1, column=1, padx=10, pady=5, sticky="w")

digits_check = ttk.Checkbutton(root, text=translate("include_digits"), variable=digits_var)
digits_check.grid(row=2, column=0, padx=10, pady=5, sticky="w")

special_check = ttk.Checkbutton(root, text=translate("include_special"), variable=special_var)
special_check.grid(row=2, column=1, padx=10, pady=5, sticky="w")

generate_button = ttk.Button(root, text=translate("generate_password"), command=on_generate)
generate_button.grid(row=3, column=0, columnspan=2, pady=10)

password_label = ttk.Label(root, text=translate("generated_password"), font=font_medium)
password_label.grid(row=4, column=0, padx=10, pady=10, sticky="w")

password_entry = ttk.Entry(root, font=font_medium, show="*")
password_entry.grid(row=4, column=1, padx=10, pady=10)

toggle_button = ttk.Button(root, text=translate("toggle_password"), command=toggle_password_visibility)
toggle_button.grid(row=5, column=0, padx=10, pady=10)

button_frame = ttk.Frame(root)
button_frame.grid(row=6, column=0, columnspan=2, pady=10)

copy_button = ttk.Button(button_frame, text=translate("copy_password"), command=copy_to_clipboard)
copy_button.grid(row=0, column=0, padx=5, pady=5)

clear_button = ttk.Button(button_frame, text=translate("clear_input"), command=clear_fields)
clear_button.grid(row=0, column=1, padx=5, pady=5)

save_as_button = ttk.Button(button_frame, text=translate("save_as"), command=save_as)
save_as_button.grid(row=0, column=2, padx=5, pady=5)

history_button = ttk.Button(button_frame, text=translate("view_history"), command=show_history)
history_button.grid(row=0, column=3, padx=5, pady=5)

settings_button = ttk.Button(root, text=translate("settings"), command=open_settings_window)
settings_button.grid(row=7, column=0, columnspan=2, pady=10)

strength_label = ttk.Label(root, text=translate("password_strength") + ": ", font=font_medium)
strength_label.grid(row=8, column=0, columnspan=2, pady=10)

# 创建语言下拉菜单
language_var = tk.StringVar(value=current_language)
language_menu = ttk.OptionMenu(root, language_var, "简体中文", "English", "日本語", "Русский", command=update_language)
language_menu.grid(row=9, column=0, columnspan=2, pady=10)

# 启动应用
root.mainloop()


零Python基础编程密码生成器
http://haozi520.github.io/2024/09/19/python/
作者
HaoziOpO Studio Offical
发布于
2024年9月19日
更新于
2024年10月2日
许可协议