平时用到网站太多,账号、密码一大堆,时间一久很容易忘记就做了个简易的账户管理程序
以下是代码
[Python] 纯文本查看 复制代码 from tkinter import *
import csv
def save_info():
account = entry_account.get().strip()
password = entry_password.get().strip()
url = entry_url.get().strip()
name = entry_name.get().strip()
# 确保账号、密码、网址和网页名都不为空
if account and password and url and name:
with open('账号管理.csv', 'a', newline='') as f:
writer = csv.writer(f)
# 检查文件是否为空,若为空则写入抬头行
if f.tell() == 0:
writer.writerow(['账号', '密码', '网址','网页名称'])
writer.writerow([account, password, url,name])
entry_account.delete(0, END)
entry_password.delete(0, END)
entry_url.delete(0, END)
entry_name.delete(0,END)
else:
print('请填写完整的账号、密码、网址和网页名称信息。')
def view_info():
with open('账号管理.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
def search_info():
search_value = entry_search.get()
results = []
with open('账号管理.csv', 'r') as f:
reader = csv.reader(f)
# 跳过抬头行
next(reader)
for row in reader:
if search_value.lower() in row[3].lower():
results.append((row[0], row[1], row[2],row[3]))
# 清空原有的结果
for widget in frame_results.winfo_children():
widget.destroy()
# 显示查找结果
if results:
for result in results:
label_result = Label(frame_results, text=result[2])
label_result.pack()
label_account = Label(frame_results, text="账号:" + result[0])
label_account.pack()
label_password = Label(frame_results, text="密码:" + result[1])
label_password.pack()
else:
label_result = Label(frame_results, text="未找到匹配的网页")
label_result.pack()
root = Tk()
root.geometry("500x400")
label_account = Label(root, text="账号")
label_account.pack()
entry_account = Entry(root)
entry_account.pack()
label_password = Label(root, text="密码")
label_password.pack()
entry_password = Entry(root, show="*")
entry_password.pack()
label_url = Label(root, text="网址")
label_url.pack()
entry_url = Entry(root)
entry_url.pack()
label_name = Label(root, text="网页名称")
label_name.pack()
entry_name = Entry(root)
entry_name.pack()
button_save = Button(root, text="保存", command=save_info)
button_save.pack()
button_view = Button(root, text="查看全部", command=view_info)
button_view.pack()
label_search = Label(root, text="查找网页名")
label_search.pack()
entry_search = Entry(root)
entry_search.pack()
button_search = Button(root, text="查找", command=search_info)
button_search.pack()
frame_results = Frame(root)
frame_results.pack()
root.mainloop()
|