用python的thinter写一个球鞋管理app包括登录注册和增删查改的搜索
时间: 2024-03-18 16:39:56 浏览: 31
好的,下面是一个简单的球鞋管理app的示例代码,包括登录注册和增删查改的搜索。注意,这只是一个基本的示例,你可能需要根据自己的需求进行修改和完善。
```python
import tkinter as tk
# 定义一个球鞋类
class Shoe:
def __init__(self, name, brand, color, size):
self.name = name
self.brand = brand
self.color = color
self.size = size
# 定义一个球鞋管理系统类
class ShoeManager:
def __init__(self):
self.shoe_list = []
# 添加球鞋
def add_shoe(self, name, brand, color, size):
shoe = Shoe(name, brand, color, size)
self.shoe_list.append(shoe)
# 删除球鞋
def delete_shoe(self, shoe):
self.shoe_list.remove(shoe)
# 查找球鞋
def search_shoe(self, name):
for shoe in self.shoe_list:
if shoe.name == name:
return shoe
return None
# 修改球鞋信息
def update_shoe(self, shoe, name, brand, color, size):
shoe.name = name
shoe.brand = brand
shoe.color = color
shoe.size = size
# 定义一个登录界面类
class LoginFrame(tk.Frame):
def __init__(self, master, shoe_manager):
super().__init__(master)
self.shoe_manager = shoe_manager
tk.Label(self, text="用户名:").grid(row=0, column=0)
self.username_entry = tk.Entry(self)
self.username_entry.grid(row=0, column=1)
tk.Label(self, text="密码:").grid(row=1, column=0)
self.password_entry = tk.Entry(self, show="*")
self.password_entry.grid(row=1, column=1)
tk.Button(self, text="登录", command=self.login).grid(row=2, column=0)
tk.Button(self, text="注册", command=self.register).grid(row=2, column=1)
# 登录函数
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
# 在此处添加验证用户名和密码的代码
self.master.destroy()
ShoeListFrame(self.shoe_manager)
# 注册函数
def register(self):
username = self.username_entry.get()
password = self.password_entry.get()
# 在此处添加注册新用户的代码
# 定义一个球鞋列表界面类
class ShoeListFrame(tk.Frame):
def __init__(self, master, shoe_manager):
super().__init__(master)
self.shoe_manager = shoe_manager
tk.Label(self, text="球鞋列表").grid(row=0, column=0)
tk.Button(self, text="添加球鞋", command=self.add_shoe).grid(row=1, column=0)
tk.Button(self, text="退出", command=self.quit).grid(row=1, column=1)
self.shoe_listbox = tk.Listbox(self)
self.shoe_listbox.grid(row=2, column=0, columnspan=2)
self.refresh_listbox()
# 刷新球鞋列表框
def refresh_listbox(self):
self.shoe_listbox.delete(0, tk.END)
for shoe in self.shoe_manager.shoe_list:
self.shoe_listbox.insert(tk.END, f"{shoe.name} ({shoe.brand} {shoe.color} {shoe.size})")
# 添加球鞋函数
def add_shoe(self):
AddShoeDialog(self)
# 删除球鞋函数
def delete_shoe(self):
selected_index = self.shoe_listbox.curselection()[0]
selected_shoe = self.shoe_manager.shoe_list[selected_index]
self.shoe_manager.delete_shoe(selected_shoe)
self.refresh_listbox()
# 修改球鞋信息函数
def update_shoe(self):
selected_index = self.shoe_listbox.curselection()[0]
selected_shoe = self.shoe_manager.shoe_list[selected_index]
UpdateShoeDialog(self, selected_shoe)
# 定义一个添加球鞋对话框类
class AddShoeDialog(tk.Toplevel):
def __init__(self, master):
super().__init__(master)
tk.Label(self, text="球鞋名称:").grid(row=0, column=0)
self.name_entry = tk.Entry(self)
self.name_entry.grid(row=0, column=1)
tk.Label(self, text="品牌:").grid(row=1, column=0)
self.brand_entry = tk.Entry(self)
self.brand_entry.grid(row=1, column=1)
tk.Label(self, text="颜色:").grid(row=2, column=0)
self.color_entry = tk.Entry(self)
self.color_entry.grid(row=2, column=1)
tk.Label(self, text="尺码:").grid(row=3, column=0)
self.size_entry = tk.Entry(self)
self.size_entry.grid(row=3, column=1)
tk.Button(self, text="添加", command=self.add_shoe).grid(row=4, column=0)
tk.Button(self, text="取消", command=self.destroy).grid(row=4, column=1)
# 添加球鞋函数
def add_shoe(self):
name = self.name_entry.get()
brand = self.brand_entry.get()
color = self.color_entry.get()
size = self.size_entry.get()
self.master.shoe_manager.add_shoe(name, brand, color, size)
self.master.refresh_listbox()
self.destroy()
# 定义一个修改球鞋信息对话框类
class UpdateShoeDialog(tk.Toplevel):
def __init__(self, master, shoe):
super().__init__(master)
self.shoe = shoe
tk.Label(self, text="球鞋名称:").grid(row=0, column=0)
self.name_entry = tk.Entry(self, textvariable=tk.StringVar(value=shoe.name))
self.name_entry.grid(row=0, column=1)
tk.Label(self, text="品牌:").grid(row=1, column=0)
self.brand_entry = tk.Entry(self, textvariable=tk.StringVar(value=shoe.brand))
self.brand_entry.grid(row=1, column=1)
tk.Label(self, text="颜色:").grid(row=2, column=0)
self.color_entry = tk.Entry(self, textvariable=tk.StringVar(value=shoe.color))
self.color_entry.grid(row=2, column=1)
tk.Label(self, text="尺码:").grid(row=3, column=0)
self.size_entry = tk.Entry(self, textvariable=tk.StringVar(value=shoe.size))
self.size_entry.grid(row=3, column=1)
tk.Button(self, text="保存", command=self.update_shoe).grid(row=4, column=0)
tk.Button(self, text="取消", command=self.destroy).grid(row=4, column=1)
# 修改球鞋信息函数
def update_shoe(self):
name = self.name_entry.get()
brand = self.brand_entry.get()
color = self.color_entry.get()
size = self.size_entry.get()
self.master.shoe_manager.update_shoe(self.shoe, name, brand, color, size)
self.master.refresh_listbox()
self.destroy()
# 创建一个球鞋管理系统对象
shoe_manager = ShoeManager()
# 创建一个登录界面
root = tk.Tk()
root.title("登录")
LoginFrame(root, shoe_manager).pack()
root.mainloop()
```
在这个示例中,我们使用了tkinter库创建了一个简单的GUI应用程序。用户可以在登录界面输入用户名和密码,然后登录到球鞋列表界面。在球鞋列表界面,用户可以添加、删除、修改和查找球鞋。我们使用了一个Shoe类来表示一个球鞋对象,使用了一个ShoeManager类来管理所有的球鞋。我们还定义了一些对话框类来帮助用户进行球鞋信息的添加、删除和修改。
当然,这只是一个简单的示例,你可能需要根据自己的需求进行修改和完善。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)