程序设计思路:
该程序框架由我之前的客户管理系统与商品管理系统改制而来,核心是利用python中的tkinter制作GUI,用pymysql将python连接MySQL数据库进行数据支持,用Frame框架进行页面的滚动与切换,是具备一定功能性的二手交易平台程序
该程序具备会员的注册,客户的买卖,购物车与结账功能
会员的注册实现基于pymsql连接的MySQL数据库中的会员表的匹配
客户的买卖 卖是基本的增加商品功能 买则是将查询与购物车、结账三者结合形成的操作
主程序
import pymysql.cursors from tkinter import ttk import tkinter as tk import tkinter.font as tkFont from tkinter import * import tkinter.messagebox as messagebox import time import subprocess class logincheck: def __init__(self,parent_window): parent_window.update() parent_window.destroy() self.window=tk.Tk() self.window.title("会员登录界面") self.window.geometry('700x600+70+50') def open_other_python_program(): # 指定要启动的Python程序的路径 program_path = 'C:\\Users\\16226\\Desktop\\易物会员注册系统.py' # 使用subprocess模块启动程序 subprocess.Popen(['python', program_path]) Button(self.window, text="注册", font=tkFont.Font(size=16), command=open_other_python_program, width=10,height=2, fg='white', bg='pink').place(x=70, y=300) def getTime(): timeStr=time.strftime('%H:%M:%S') Rtime.configure(text=timeStr) self.window.after(1000, getTime) Rtime= Label(self.window,text='') Rtime.pack(pady=25) getTime() label = Label(self.window, text="易物二手交易登录", font=("楷体", 30)) label.config(fg="purple") label2 = Label(self.window, text="\n潘安俊、伍宇成、张宇飞、张雪", font=("楷体",15)) label.pack(pady=10) label2.pack(pady=10) self.var_username=StringVar() self.var_pwd=StringVar() self.right_top_username_label = Label(text="用户名:", font=('楷体', 15)).pack(pady=15) self.right_top_username_entry = Entry(textvariable=self.var_username, font=('楷体', 15)).pack() self.right_top_pwd_label = Label(text="密码:", font=('楷体', 15)).pack(pady=15) self.right_top_pwd_entry = Entry(textvariable=self.var_pwd, font=('楷体', 15)).pack() self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) self.username=[] self.pwd=[] self.window.mainloop() db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 会员表 WHERE 用户名='%s'"%(self.var_username.get()) try: cursor.execute(sql) results = cursor.fetchall() for row in results: self.username.append(row[0]) self.pwd.append(row[1]) except: print("Error: unable to fetch data") messagebox.showinfo('警告!', '数据库连接失败!') db.close() def back(self): self.window.destroy() def new_row(self): if self.var_username.get() != '' and self.var_pwd.get() != '' or self.var_username.get()=='test' and self.var_pwd.get()=='123': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor= db.cursor() sql = "SELECT * FROM 会员表 WHERE 用户名='%s'"%(self.var_username.get()) try: cursor.execute(sql) startpage(self.window) except: db.rollback() messagebox.showinfo('警告!', '数据库连接失败!') db.close() else: messagebox.showinfo('提示!', '请填写正确的用户信息') class startpage: def __init__(self,parent_window): parent_window.update() parent_window.destroy() self.window=tk.Tk() self.window.title("易物二手奢侈品交易") self.window.geometry('1200x600+70+50') def switch_to_another_program(): subprocess.run(['python','查询.py'],shell=True) def open_other_python_program1(): # 指定要启动的Python程序的路径 program_path = 'C:\\Users\\16226\\Desktop\\购物车系统.py' # 使用subprocess模块启动程序 subprocess.Popen(['python', program_path]) def open_other_python_program2(): # 指定要启动的Python程序的路径 program_path = 'C:\\Users\\16226\\Desktop\\收货人系统.py' # 使用subprocess模块启动程序 subprocess.Popen(['python', program_path]) def getTime(): timeStr=time.strftime('%H:%M:%S') Rtime.configure(text=timeStr) self.window.after(1000, getTime) Rtime= Label(self.window,text='') Rtime.pack(pady=25) getTime() label= Label(self.window,text="易物二手奢侈品主页",font=("楷体",30)) label.pack(pady=10) Button(self.window,text="我要卖商品",font=tkFont.Font(size=16),command=lambda: xinjian(self.window),width=20,height=2,fg='white',bg='pink').place(x=100,y=300) Button(self.window,text="依据编号查询商品",font=tkFont.Font(size=16), command=lambda: cangkucha(self.window), width=20,height=2, fg='white', bg='gray').place(x=400, y=300) Button(self.window, text="删除购物车商品", font=tkFont.Font(size=16), command=lambda:shanchu(self.window), width=20,height = 2, fg = 'white', bg = 'gray').place(x=700, y=400) Button(self.window, text="我要买商品", font = tkFont.Font(size=16), command = lambda: kehudan(self.window), width = 20, height = 2, fg = 'white', bg = 'purple').place(x=100, y=400) Button(self.window, text="退出系统", font=tkFont.Font(size=16), command=self.window.destroy, width=20,height=2, fg='white', bg='gray').place(x=700, y=700) Button(self.window, text="修改商品信息", font=tkFont.Font(size=16), command=lambda:xiugai(self.window),width=20,height=2,fg='white',bg='gray').place(x=700,y=300) Button(self.window, text="依据商品名查询", font=tkFont.Font(size=16), command=lambda:cangkucha2(self.window), width=20,height=2, fg='white', bg='gray').place(x=400, y=400) Button(self.window, text="收货人管理", font=tkFont.Font(size=16), command=open_other_python_program2,width=20, height=2, fg='white', bg='gray').place(x=700, y=500) Button(self.window, text="购物车查看", font=tkFont.Font(size=16), command=lambda: kehudan0(self.window), width=20,height=2, fg='white', bg='green').place(x=100, y=500) Button(self.window, text="购物车结账", font=tkFont.Font(size=16), command=open_other_python_program1, width=20,height=2, fg='white', bg='green').place(x=400, y=500) self.window.mainloop() class xinjian: def __init__(self,parent_window): parent_window.destroy() self.window=tk.Tk() self.window.title("我要卖商品") self.window.geometry('700x600+70+50') self.top_title=Label(self.window,text="添加待售商品",bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = StringVar() self.var_name = StringVar() self.var_type = StringVar() self.var_p = StringVar() self.var_b = StringVar() self.var_new = StringVar() self.var_op = StringVar() self.var_num = StringVar() self.right_top_id_label = Label(text="商品编号:", font=('楷体', 15)).place(x=100,y=100) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).place(x=200,y=100) self.right_top_name_label =Label(text="商品名称:", font=('楷体', 15)).place(x=300,y=100) self.right_top_name_entry = Entry(textvariable=self.var_name, font=('楷体', 15)).place(x=400,y=100) self.right_top_type_label = Label(text="商品品类:", font=('楷体', 15)).place(x=500,y=100) self.right_top_type_entry = Entry(textvariable=self.var_type, font=('楷体', 15)).place(x=600,y=100) self.right_top_p_label = Label(text="商品价格:", font=('楷体', 15)).place(x=100,y=200) self.right_top_p_entry = Entry(textvariable=self.var_p, font=('楷体', 15)).place(x=200,y=200) self.right_top_b_label = Label(text="商品品牌:",font=('楷体',15)).place(x=100,y=300) self.right_top_b_entry = Entry(textvariable=self.var_b, font=('楷体', 15)).place(x=200,y=300) self.right_top_num_label = Label(text="商品年份:", font=('楷体', 15)).place(x=100,y=400) self.right_top_num_entry = Entry(textvariable=self.var_num, font=('楷体', 15)).place(x=200,y=400) self.right_top_new_label = Label(text="商品成色:", font=('楷体', 15)).place(x=300,y=200) self.right_top_new_entry = Entry(textvariable=self.var_new, font=('楷体', 15)).place(x=400,y=200) self.right_top_op_label = Label(text="商品公价:", font=('楷体', 15)).place(x=300,y=400) self.right_top_op_entry = Entry(textvariable=self.var_op, font=('楷体', 15)).place(x=400,y=400) self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).place(x=500,y=500) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).place(x=500,y=550) self.window.protocol("WM_DELETE_WINDOW", self.back) self.id=[] self.name=[] self.type=[] self.p=[] self.b = [] self.new=[] self.op=[] self.num=[] db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表" try: cursor.execute(sql) results=cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.type.append(row[2]) self.p.append(row[3]) self.b.append(row[4]) self.new.append(row[5]) self.op.append(row[6]) self.num.append(row[7]) except: print("Error: unable to fetch data") messagebox.showinfo('警告!', '数据库连接失败!') db.close() def back(self): startpage(self.window) def new_row(self): if self.var_id.get() != '' and self.var_name.get() != '' and self.var_type.get() != '' and self.var_p.get() != '': db=pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql= "INSERT INTO 商品表(商品编号,商品名称,商品品类,商品价格,商品品牌,商品成色,商品公价,商品年份) VALUES('%s','%s','%s','%s','%s','%s','%s','%s')"%(self.var_id.get(), self.var_name.get(),self.var_type.get(), self.var_p.get(), self.var_b.get(), self.var_new.get(), self.var_op.get(), self.var_num.get()) try: cursor.execute(sql) db.commit() messagebox.showinfo('提示!', '添加成功!') except: db.rollback() messagebox.showinfo('警告!', '数据库连接失败!') db.close() else: messagebox.showinfo('提示!', '请填写客户信息') class kehudan: def __init__(self,parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('可购商品清单') self.window.geometry('1200x600+70+50') canvas = tk.Canvas(self.window, width=500, height=100) canvas.pack(side='top', fill='both', expand=True) # 创建水平滚动条 scrollbar = tk.Scrollbar(self.window, orient='horizontal', command=canvas.xview) scrollbar.pack(side='bottom', fill='x') # 配置Canvas的滚动命令 canvas.config(xscrollcommand=scrollbar.set) vsb = tk.Scrollbar(self.window, orient='vertical', command=canvas.yview) vsb.pack(side='right', fill='y') # 配置Canvas的滚动命令 canvas.config(yscrollcommand=vsb.set) # 创建一个Frame作为滚动内容的容器 scrollable_frame = tk.Frame(canvas) canvas.create_window((0, 0), window=scrollable_frame, anchor='nw') # 在Frame中添加多个Label # 使Frame可滚动 def on_configure(event): # 当Frame大小改变时,更新Canvas的滚动区域 canvas.configure(scrollregion=canvas.bbox('all')) # 绑定Frame的大小改变事件 scrollable_frame.bind('<Configure>', on_configure) db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 商品表" try: cursor.execute(sql) # 执行sql语句 results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] db.commit() Label(scrollable_frame, text=self.id + "\t" + self.name + "\t" + self.type + "\t" + self.p + "\t" + self.b + "\t" + self.new + "\t" +self.op + "\t" +self.num, font=('楷体', 18)).pack(pady=5) except: db.rollback() # 发生错误时回滚 messagebox.showinfo('警告!', '数据库连接失败!') db.close() # 关闭数据库连接 self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) def back(self): cangkucha(self.window) class cangkucha: def __init__(self, parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('依据商品编号查询') self.window.geometry('700x600+70+50') self.student_id = StringVar() self.id = '商品编号: ' + ' ' self.name = '商品名称: ' +' ' self.type = '商品品类: ' + ' ' self.p = '商品价格 : ' + ' ' self.b = '商品品牌: ' + ' ' self.new = '商品成色: ' + ' ' self.op = '商品公价: ' + ' ' self.num = '商品年份:' + ' ' Button(self.window, text="商品清单", font=tkFont.Font(size=12), command=lambda: kehudan(self.window), width=20, height=2, fg='white', bg='gray').place(x=20, y=70) self.right_top_name_label = Label(text="商品编号查询", font=('楷体', 15)).pack(pady=15) self.right_top_name_entry = Entry(textvariable=self.student_id, font=('楷体', 15)).pack(pady=30) self.right_top_button3 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 打开数据库连接 db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表 WHERE 商品编号='%s'" % (self.student_id.get()) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] except: print("Error: unable to fetch data") db.close() def back(self): startpage(self.window) def new_row(self): if self.student_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表 where 商品编号 = '%s'" % (self.student_id.get()) # SQL 插入语句 try: cursor.execute(sql) results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] db.commit() label = tk.Label(self.window, text='商品信息查看', bg='SkyBlue', font=('楷体', 20), width=70, height=2) label.pack(pady=20) Label(self.window, text=self.id, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.name, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.type, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.p, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.b, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.new, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.op, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.num, font=('楷体', 18)).pack(pady=5) Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack(pady=150) self.window.protocol("WM_DELETE_WINDOW", self.back_1) self.window.mainloop() except: db.rollback() messagebox.showinfo('提示', '数据库连接失败!') db.close() else: messagebox.showinfo('提示', '请填写商品信息!') def back_1(self): cangkucha(self.window) class cangkucha2: def __init__(self, parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('依据商品名查询') self.window.geometry('700x600+70+50') self.student_id = StringVar() self.student_name = StringVar() self.id = '商品编号: ' + ' ' self.name = '商品名称: ' + ' ' self.type = '商品品类: ' + ' ' self.p = '商品价格 : ' + ' ' self.b = '商品品牌: ' + ' ' self.new = '商品成色: ' + ' ' self.op = '商品公价: ' + ' ' self.num = '商品年份:' + ' ' Button(self.window, text="商品清单", font=tkFont.Font(size=12), command=lambda: kehudan(self.window), width=20, height=2, fg='white', bg='gray').place(x=20, y=70) self.right_top_name_label = Label(text="商品名查询", font=('楷体', 15)).pack(pady=15) self.right_top_name_entry = Entry(text="商品编号", textvariable=self.student_name, font=('楷体', 15)).pack(pady=30) self.right_top_button3 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 打开数据库连接 db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表 WHERE 商品名='%s'" % (self.student_name.get()) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] except: print("Error: unable to fetch data") db.close() def back(self): startpage(self.window) def new_row(self): if self.student_name.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表 where 商品名 = '%s'" % (self.student_name.get()) # SQL 插入语句 try: cursor.execute(sql) results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] db.commit() label = tk.Label(self.window, text='商品信息查看', bg='SkyBlue', font=('楷体', 20), width=70, height=2) label.pack(pady=20) Label(self.window, text=self.id, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.name, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.type, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.p, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.b, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.new, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.op, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.num, font=('楷体', 18)).pack(pady=5) Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack(pady=150) self.window.protocol("WM_DELETE_WINDOW", self.back_1) self.window.mainloop() except: db.rollback() messagebox.showinfo('提示', '数据库连接失败!') db.close() else: messagebox.showinfo('提示', '请填写商品信息!') def back_1(self): cangkucha2(self.window) class shanchu: def __init__(self,parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('删除购物车商品') self.window.geometry('700x600+70+50') self.top_title = Label(self.window, text='删除', bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = '商品编号: ' + ' ' self.var_name = '商品名称: ' + ' ' self.var_id = StringVar() self.var_name = StringVar() self.right_top_id_label = Label(text="商品编号", font=('楷体', 15)).pack(pady=15) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack() self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) self.id = [] self.name = [] self.type = [] self.p = [] self.b = [] self.new = [] self.op = [] self.num = [] # 打开数据库连接 db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 购物车表" # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.type.append(row[2]) self.p.append(row[3]) self.b.append(row[4]) self.new.append(row[5]) self.op.append(row[6]) self.num.append(row[7]) except: print("Error: unable to fetch data") messagebox.showinfo('提示', '数据库连接失败!') db.close() def back(self): startpage(self.window) def new_row(self): if self.var_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "DELETE FROM 购物车表 WHERE 商品编号 = '%s'" % (self.var_id.get()) try: cursor.execute(sql) db.commit() messagebox.showinfo('提示!', '删除成功!') except: db.rollback() messagebox.showinfo('警告!', '数据库连接失败!') db.close() else: messagebox.showinfo('警告!', '填写删除信息') class xiugai: def __init__(self,parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('修改信息(依据商品编号进行修改,请核对好商品编号后进行此项操作)') self.window.geometry('700x600+70+50') self.top_title = Label(self.window, text='修改信息', bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = StringVar() self.var_name = StringVar() self.var_type = StringVar() self.var_p = StringVar() self.var_num = StringVar() self.var_b = StringVar() self.var_new = StringVar() self.var_op = StringVar() self.right_top_id_label = Label(text="商品编号:", font=('楷体', 15)).place(x=100, y=100) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).place(x=200, y=100) self.right_top_name_label = Label(text="商品名称:", font=('楷体', 15)).place(x=300, y=100) self.right_top_name_entry = Entry(textvariable=self.var_name, font=('楷体', 15)).place(x=400, y=100) self.right_top_type_label = Label(text="商品品类:", font=('楷体', 15)).place(x=500, y=100) self.right_top_type_entry = Entry(textvariable=self.var_type, font=('楷体', 15)).place(x=600, y=100) self.right_top_p_label = Label(text="商品价格:", font=('楷体', 15)).place(x=100, y=200) self.right_top_p_entry = Entry(textvariable=self.var_p, font=('楷体', 15)).place(x=200, y=200) self.right_top_b_label = Label(text="商品品牌:", font=('楷体', 15)).place(x=100, y=300) self.right_top_b_entry = Entry(textvariable=self.var_b, font=('楷体', 15)).place(x=200, y=300) self.right_top_num_label = Label(text="商品年份:", font=('楷体', 15)).place(x=100, y=400) self.right_top_num_entry = Entry(textvariable=self.var_num, font=('楷体', 15)).place(x=200, y=400) self.right_top_new_label = Label(text="商品成色:", font=('楷体', 15)).place(x=300, y=200) self.right_top_new_entry = Entry(textvariable=self.var_new, font=('楷体', 15)).place(x=400, y=200) self.right_top_op_label = Label(text="商品公价:", font=('楷体', 15)).place(x=300, y=400) self.right_top_op_entry = Entry(textvariable=self.var_op, font=('楷体', 15)).place(x=400, y=400) self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).place(x=500, y=500) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).place(x=500, y=550) self.window.protocol("WM_DELETE_WINDOW", self.back) self.id = [] self.name = [] self.type = [] self.p = [] self.b = [] self.new = [] self.op = [] self.num = [] db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表" try: cursor.execute(sql) results = cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.type.append(row[2]) self.p.append(row[3]) self.b.append(row[4]) self.new.append(row[5]) self.op.append(row[6]) self.num.append(row[7]) except: print("Error: unable to fetch data") messagebox.showinfo('提示', '数据库连接失败!') db.close() def back(self): startpage(self.window) def new_row(self): if self.var_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "UPDATE 商品表 SET 商品编号='%s',商品名称='%s',商品品类='%s',商品价格='%s',商品品牌='%s',商品成色='%s',商品公价='%s',商品年份='%s' WHERE 商品编号='%s'" % (self.var_id.get(),self.var_name.get(),self.var_type.get(),self.var_p.get(),self.var_b.get(),self.var_new.get(),self.var_op.get(),self.var_num.get(),self.var_id.get()) try: cursor.execute(sql) db.commit() messagebox.showinfo('提示!', '修改成功!') except: db.rollback() messagebox.showinfo('警告!', '数据库连接失败!') db.close() else: messagebox.showinfo('警告!', '填写修改信息') class xiugai1: def __init__(self,parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('修改信息(依据商品编号进行入库,请核对好商品编号后进行此项操作)') self.window.geometry('700x600+70+50') self.top_title = Label(self.window, text='入库操作', bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = StringVar() self.var_name = StringVar() self.var_gender = StringVar() self.var_age = StringVar() self.var_num = StringVar() self.right_top_id_label = Label(text="商品编号", font=('楷体', 15)).pack(pady=15) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack() self.right_top_age_label = Label(text="商品数量", font=('楷体', 15)).pack(pady=15) self.right_top_age_entry = Entry(textvariable=self.var_age, font=('楷体', 15)).pack() self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) self.id = [] self.name = [] self.gender = [] self.age = [] self.num = [] # 打开数据库连接 db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 商品表" # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.gender.append(row[2]) self.age.append(row[3]) self.num.append(row[4]) except: print("Error: unable to fetch data") messagebox.showinfo('提示', '数据库连接失败!') db.close() def back(self): startpage(self.window) def new_row(self): if self.var_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "UPDATE 商品表 SET 商品数量=商品数量+'%s' WHERE 商品编号='%s'" % (self.var_age.get(),self.var_id.get()) try: cursor.execute(sql) db.commit() messagebox.showinfo('提示!', '入库成功!') except: db.rollback() messagebox.showinfo('警告!', '数据库连接失败!') db.close() else: messagebox.showinfo('警告!', '填写入库信息') class xiugai2: def __init__(self,parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('我的购物车') self.window.geometry('700x600+70+50') self.top_title = Label(self.window, text='购物车', bg='SkyBlue', font=('楷体', 20), width=70, height=2) self.top_title.pack() self.var_id = StringVar() self.var_name = StringVar() self.var_type = StringVar() self.var_p = StringVar() self.var_num = StringVar() self.var_b = StringVar() self.var_new = StringVar() self.var_op = StringVar() self.right_top_id_label = Label(text="商品编号", font=('楷体', 15)).pack(pady=15) self.right_top_id_entry = Entry(textvariable=self.var_id, font=('楷体', 15)).pack() self.right_top_age_label = Label(text="商品数量", font=('楷体', 15)).pack(pady=15) self.right_top_age_entry = Entry(textvariable=self.var_age, font=('楷体', 15)).pack() self.right_top_button1 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button2 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) self.id = [] self.name = [] self.type = [] self.p = [] self.b = [] self.new = [] self.op = [] self.num = [] # 打开数据库连接 db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 商品表" # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id.append(row[0]) self.name.append(row[1]) self.type.append(row[2]) self.p.append(row[3]) self.b.append(row[4]) self.new.append(row[5]) self.op.append(row[6]) self.num.append(row[7]) except: print("Error: unable to fetch data") messagebox.showinfo('提示', '数据库连接失败!') db.close() def back(self): startpage(self.window) def new_row(self): if self.var_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 商品表 WHERE 商品编号='%s'" % (self.var_id.get()) try: cursor.execute(sql) db.commit() messagebox.showinfo('提示!', '出库成功!') except: db.rollback() messagebox.showinfo('警告!', '数据库连接失败!') db.close() else: messagebox.showinfo('警告!', '填写出库信息') class kehudan0: def __init__(self, parent_window): parent_window.destroy() # 销毁子界面 self.window = tk.Tk() self.window.title('购物车商品清单') self.window.geometry('1200x600+70+50') canvas = tk.Canvas(self.window, width=500, height=100) canvas.pack(side='top', fill='both', expand=True) # 创建水平滚动条 scrollbar = tk.Scrollbar(self.window, orient='horizontal', command=canvas.xview) scrollbar.pack(side='bottom', fill='x') # 配置Canvas的滚动命令 canvas.config(xscrollcommand=scrollbar.set) vsb = tk.Scrollbar(self.window, orient='vertical', command=canvas.yview) vsb.pack(side='right', fill='y') # 配置Canvas的滚动命令 canvas.config(yscrollcommand=vsb.set) # 创建一个Frame作为滚动内容的容器 scrollable_frame = tk.Frame(canvas) canvas.create_window((0, 0), window=scrollable_frame, anchor='nw') self.total_price = 0 # 在Frame中添加多个Label # 使Frame可滚动 def on_configure(event): # 当Frame大小改变时,更新Canvas的滚动区域 canvas.configure(scrollregion=canvas.bbox('all')) # 绑定Frame的大小改变事件 scrollable_frame.bind('<Configure>', on_configure) db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() # 使用cursor()方法获取操作游标 sql = "SELECT * FROM 购物车表" try: cursor.execute(sql) # 执行sql语句 results = cursor.fetchall() for row in results: price = float(row[3]) self.total_price += price self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] db.commit() Label(scrollable_frame,text=self.id + "\t" + self.name + "\t" + self.type + "\t" + self.p + "\t" + self.b + "\t" + self.new + "\t" + self.op + "\t" + self.num,font=('楷体', 18)).pack(pady=5) total_price_label = tk.Label(self.window, text=f"总价: {self.total_price:.2f}", font=('楷体', 18)) total_price_label.pack(pady=10) except: db.rollback() # 发生错误时回滚 messagebox.showinfo('警告!', '数据库连接失败!') db.close() # 关闭数据库连接 self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) def back(self): cangkucha0(self.window) class cangkucha0: def __init__(self, parent_window): parent_window.destroy() self.window = tk.Tk() self.window.title('依据商品编号查询') self.window.geometry('700x600+70+50') self.student_id = StringVar() self.id = '商品编号: ' + ' ' self.name = '商品名称: ' + ' ' self.type = '商品品类: ' + ' ' self.p = '商品价格 : ' + ' ' self.b = '商品品牌: ' + ' ' self.new = '商品成色: ' + ' ' self.op = '商品公价: ' + ' ' self.num = '商品年份:' + ' ' Button(self.window, text="商品清单", font=tkFont.Font(size=12), command=lambda: kehudan(self.window), width=20, height=2, fg='white', bg='gray').place(x=20, y=70) self.right_top_name_label = Label(text="商品编号查询", font=('楷体', 15)).pack(pady=15) self.right_top_name_entry = Entry(textvariable=self.student_id, font=('楷体', 15)).pack(pady=30) self.right_top_button3 = ttk.Button(text='确定', width=20, command=self.new_row).pack(pady=30) self.right_top_button4 = ttk.Button(text='返回', width=20, command=self.back).pack() self.window.protocol("WM_DELETE_WINDOW", self.back) # 打开数据库连接 db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 购物车表 WHERE 商品编号='%s'" % (self.student_id.get()) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] except: print("Error: unable to fetch data") db.close() def back(self): startpage(self.window) def new_row(self): if self.student_id.get() != '': db = pymysql.connect(host="localhost", user="root", passwd="pananjun", db="cus") cursor = db.cursor() sql = "SELECT * FROM 购物车表 where 商品编号 = '%s'" % (self.student_id.get()) # SQL 插入语句 try: cursor.execute(sql) results = cursor.fetchall() for row in results: self.id = '商品编号: ' + row[0] self.name = '商品名称: ' + row[1] self.type = '商品品类: ' + row[2] self.p = '商品价格 : ' + row[3] self.b = '商品品牌: ' + row[4] self.new = '商品成色: ' + row[5] self.op = '商品公价: ' + row[6] self.num = '商品年份:' + row[7] db.commit() label = tk.Label(self.window, text='购物车商品信息查看', bg='SkyBlue', font=('楷体', 20), width=70, height=2) label.pack(pady=20) Label(self.window, text=self.id, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.name, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.type, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.p, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.b, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.new, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.op, font=('楷体', 18)).pack(pady=5) Label(self.window, text=self.num, font=('楷体', 18)).pack(pady=5) Button(self.window, text="返回首页", width=8, font=tkFont.Font(size=12), command=self.back_1).pack(pady=150) self.window.protocol("WM_DELETE_WINDOW", self.back_1) self.window.mainloop() except: db.rollback() messagebox.showinfo('提示', '数据库连接失败!') db.close() else: messagebox.showinfo('提示', '请填写商品信息!') def back_1(self): cangkucha(self.window) if __name__ == '__main__': window = tk.Tk() logincheck(window)
易物会员注册系统
import tkinter as tk from tkinter import messagebox import mysql.connector # 数据库连接配置 db_config = { 'user': 'root', 'password': 'pananjun', 'host': 'localhost', 'database': 'cus', 'charset': 'utf8mb4', 'collation': 'utf8mb4_general_ci' } # 连接数据库 def connect_db(): try: return mysql.connector.connect(**db_config) except mysql.connector.Error as err: messagebox.showerror("数据库连接失败", f"错误: {err}") return None # 注册会员 def register_member(): # 获取输入的会员信息 name = entry_name.get() password = entry_password.get() # 连接数据库 db_conn = connect_db() if db_conn is None: return # 插入数据到数据库 cursor = db_conn.cursor() try: cursor.execute("INSERT INTO 会员表 (用户名,密码) VALUES (%s, %s)", (name, password)) db_conn.commit() messagebox.showinfo("注册成功", f"欢迎 {name} 加入会员!") except mysql.connector.Error as err: messagebox.showerror("注册失败", f"错误: {err}") finally: cursor.close() db_conn.close() # 创建主窗口 root = tk.Tk() root.title("易物会员注册系统") # 创建输入框和标签 label_name = tk.Label(root, text="用户名:") label_name.pack() entry_name = tk.Entry(root) entry_name.pack() label_password = tk.Label(root, text="密码:") label_password.pack() entry_password = tk.Entry(root, show="*") entry_password.pack() # 创建注册按钮 button_register = tk.Button(root, text="注册", command=register_member) button_register.pack() # 运行主循环 root.mainloop()g购物车
import tkinter as tk from tkinter import messagebox import mysql.connector from mysql.connector import Error # 数据库连接配置 db_config = { 'user': 'root', 'password': 'pananjun', 'host': 'localhost', 'database': 'cus', 'charset': 'utf8mb4', 'collation': 'utf8mb4_general_ci' } def create_db_connection(): try: return mysql.connector.connect(**db_config) except Error as e: messagebox.showerror("数据库连接失败", f"错误: {e}") return None # 从商品表中读取数据 def fetch_product_data(product_id): try: db_conn = create_db_connection() if db_conn is None: return None cursor = db_conn.cursor() cursor.execute("SELECT * FROM 商品表 WHERE 商品编号 = %s", (product_id,)) product = cursor.fetchone() cursor.close() db_conn.close() return product except Error as e: messagebox.showerror("读取商品数据失败", f"错误: {e}") return None # 将商品数据插入到购物车表中,并删除原表中的相应数据 def insert_product_to_cart(product_data): try: db_conn = create_db_connection() if db_conn is None: return cursor = db_conn.cursor() # 插入到购物车表 cursor.execute("INSERT INTO 购物车表 (商品编号, 商品名称, 商品品类, 商品价格, 商品品牌, 商品成色, 商品公价, 商品年份) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", (product_data[0], product_data[1], product_data[2], product_data[3], product_data[4], product_data[5], product_data[6], product_data[7])) db_conn.commit() # 从商品表中删除相应数据 cursor.execute("DELETE FROM 商品表 WHERE 商品编号 = %s", (product_data[0],)) db_conn.commit() cursor.close() db_conn.close() except Error as e: messagebox.showerror("插入购物车数据失败", f"错误: {e}") # 计算商品总价 def calculate_total_price(product_data): try: price = float(product_data[3]) # 假设商品价格在第4个位置 return price except ValueError: messagebox.showerror("计算总价失败", "商品价格格式不正确") return None # GUI部分 root = tk.Tk() root.title("商品结账") # 商品编号输入框 entry_product_id = tk.Entry(root) entry_product_id.pack() # 添加到购物车按钮 def add_to_cart_action(): product_id = entry_product_id.get() product_data = fetch_product_data(product_id) if product_data: insert_product_to_cart(product_data) total_price = calculate_total_price(product_data) if total_price is not None: messagebox.showinfo("操作成功", f"商品已结账并从商品表中删除。总价: {total_price}") else: messagebox.showerror("操作失败", "计算总价失败") else: messagebox.showerror("操作失败", "未找到商品。") button_add_to_cart = tk.Button(root, text="添加到购物车", command=add_to_cart_action) button_add_to_cart.pack() # 运行主循环 root.mainloop()