用python代码写,使用tkinter实现界面UI设计,用文件保存数据,有登录和注册功能,以及数据的增删改查的衣物管理
时间: 2024-03-15 12:44:04 浏览: 21
好的,下面是一个简单的代码示例,实现了使用Tkinter实现UI界面设计,使用文件保存数据,包括登录和注册功能,以及数据的增删改查的衣物管理。
```python
import tkinter as tk
import tkinter.messagebox as msgbox
import json
# 定义文件名和字段名
filename = "clothes.json"
fields = ["name", "type", "color", "size", "quantity"]
# 定义窗口类
class ClothesManager:
def __init__(self, master):
self.master = master
self.master.title("衣物管理系统")
self.master.geometry("400x300")
# 创建控件
self.username_label = tk.Label(self.master, text="用户名:")
self.username_label.grid(row=0, column=0, sticky=tk.W)
self.username_entry = tk.Entry(self.master)
self.username_entry.grid(row=0, column=1)
self.password_label = tk.Label(self.master, text="密码:")
self.password_label.grid(row=1, column=0, sticky=tk.W)
self.password_entry = tk.Entry(self.master, show="*")
self.password_entry.grid(row=1, column=1)
self.login_btn = tk.Button(self.master, text="登录", command=self.login)
self.login_btn.grid(row=2, column=0)
self.register_btn = tk.Button(self.master, text="注册", command=self.register)
self.register_btn.grid(row=2, column=1)
self.clothes_listbox = tk.Listbox(self.master)
self.clothes_listbox.grid(row=3, column=0, columnspan=2)
self.add_btn = tk.Button(self.master, text="添加", command=self.add_clothes)
self.add_btn.grid(row=4, column=0)
self.delete_btn = tk.Button(self.master, text="删除", command=self.delete_clothes)
self.delete_btn.grid(row=4, column=1)
self.update_btn = tk.Button(self.master, text="更新", command=self.update_clothes)
self.update_btn.grid(row=5, column=0)
self.search_btn = tk.Button(self.master, text="查询", command=self.search_clothes)
self.search_btn.grid(row=5, column=1)
# 初始化数据
self.load_data()
# 登录方法
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
# 从文件中读取用户信息
with open(filename, "r") as f:
users = json.load(f)
# 验证用户名和密码是否正确
if username in users and users[username] == password:
msgbox.showinfo("提示", "登录成功")
else:
msgbox.showerror("错误", "用户名或密码错误")
# 注册方法
def register(self):
username = self.username_entry.get()
password = self.password_entry.get()
# 从文件中读取用户信息
try:
with open(filename, "r") as f:
users = json.load(f)
except:
users = {}
# 注册用户
if username in users:
msgbox.showerror("错误", "用户名已存在")
else:
users[username] = password
with open(filename, "w") as f:
json.dump(users, f)
msgbox.showinfo("提示", "注册成功")
# 加载数据方法
def load_data(self):
try:
with open(filename, "r") as f:
clothes = json.load(f)
except:
clothes = {}
# 将数据显示在列表框中
self.clothes_listbox.delete(0, tk.END)
for name, info in clothes.items():
self.clothes_listbox.insert(tk.END, f"{name}, {info['type']}, {info['color']}, {info['size']}, {info['quantity']}")
# 添加衣物方法
def add_clothes(self):
# 弹出输入框
values = []
for field in fields:
value = tk.StringVar()
tk.Label(self.master, text=f"{field}:").grid(row=len(values)+6, column=0)
tk.Entry(self.master, textvariable=value).grid(row=len(values)+6, column=1)
values.append(value)
tk.Button(self.master, text="确定", command=lambda:self.save_clothes(values)).grid(row=len(values)+7, column=0)
# 保存衣物方法
def save_clothes(self, values):
name = values[0].get()
info = {}
for i in range(1, len(values)):
info[fields[i]] = values[i].get()
try:
with open(filename, "r") as f:
clothes = json.load(f)
except:
clothes = {}
clothes[name] = info
with open(filename, "w") as f:
json.dump(clothes, f)
self.load_data()
# 删除衣物方法
def delete_clothes(self):
index = self.clothes_listbox.curselection()
if index:
name = self.clothes_listbox.get(index)
name = name.split(",")[0]
try:
with open(filename, "r") as f:
clothes = json.load(f)
except:
clothes = {}
if name in clothes:
del clothes[name]
with open(filename, "w") as f:
json.dump(clothes, f)
self.load_data()
# 更新衣物方法
def update_clothes(self):
index = self.clothes_listbox.curselection()
if index:
name = self.clothes_listbox.get(index)
name = name.split(",")[0]
try:
with open(filename, "r") as f:
clothes = json.load(f)
except:
clothes = {}
if name in clothes:
# 弹出输入框
values = []
for field in fields[1:]:
value = tk.StringVar(value=clothes[name][field])
tk.Label(self.master, text=f"{field}:").grid(row=len(values)+6, column=0)
tk.Entry(self.master, textvariable=value).grid(row=len(values)+6, column=1)
values.append(value)
tk.Button(self.master, text="确定", command=lambda:self.save_update(name, values)).grid(row=len(values)+7, column=0)
# 保存更新的衣物信息
def save_update(self, name, values):
info = {}
for i in range(len(values)):
info[fields[i+1]] = values[i].get()
try:
with open(filename, "r") as f:
clothes = json.load(f)
except:
clothes = {}
clothes[name] = info
with open(filename, "w") as f:
json.dump(clothes, f)
self.load_data()
# 查询衣物方法
def search_clothes(self):
# 弹出输入框
values = []
for field in fields:
value = tk.StringVar()
tk.Label(self.master, text=f"{field}:").grid(row=len(values)+6, column=0)
tk.Entry(self.master, textvariable=value).grid(row=len(values)+6, column=1)
values.append(value)
tk.Button(self.master, text="确定", command=lambda:self.do_search(values)).grid(row=len(values)+7, column=0)
# 执行查询衣物操作
def do_search(self, values):
try:
with open(filename, "r") as f:
clothes = json.load(f)
except:
clothes = {}
for name, info in clothes.items():
match = True
for i in range(len(values)):
if values[i].get() != "" and values[i].get() != info[fields[i]]:
match = False
break
if match:
self.clothes_listbox.selection_clear(0, tk.END)
index = self.clothes_listbox.get(0, tk.END).index(f"{name}, {info['type']}, {info['color']}, {info['size']}, {info['quantity']}")
self.clothes_listbox.selection_set(index)
self.clothes_listbox.activate(index)
break
# 启动程序
if __name__ == "__main__":
root = tk.Tk()
app = ClothesManager(root)
root.mainloop()
```
运行代码后,可以看到一个简单的UI界面,包含了登录和注册功能,以及数据的增删改查的衣物管理。用户信息和衣物信息都保存在文件中,可以进行持久化存储。