基于python,MySQL,tkinter
import pymysql #导入数据库模块 import tkinter #导入相关的窗体模块 import os #利用os模块可以方便定义完整路径 import tkinter.messagebox#实现提示框的组件 import tkinter.simpledialog#简单对话框 from wordcloud import WordCloud import matplotlib.pyplot as plt import threading import pandas as pd #对数据进行访问 import sys #与操作系统交互 def get_resources_path(relative_path):#利用此函数实现资源路径的定位 if getattr(sys,"frozen",False): base_path=sys._MEIPASS #获取临时资源 else: base_path=os.path.abspath(".") #获取当前路径 return os.path.join(base_path,relative_path) #获得绝对路径 LOGO_PATH=get_resources_path(os.path.join("resources",'shop_logo.ico')) #'resources'+os.sep + 'shop_logo.ico'#logo文件路径 IMAGES_PATH=get_resources_path(os.path.join("resources",'background.png'))#'resources' +os.sep+ 'background.png'#图像文件路径 Warehouse_PATH=get_resources_path(os.path.join("resources",'店铺.png'))#'resources' +os.sep+ '店铺.png'#图像文件路径 COMMODITY_DELETE_PATH=get_resources_path(os.path.join("resources",'店铺修改背景图.png'))#'resources' +os.sep+ '店铺修改背景图.png'#图像文件路径 WORDCLOUD_PATH='词云图.png' EXCEL_COMMODITY=get_resources_path(os.path.join("resources",'商品数据.xlsx'))#'resources'+ os.sep + '商品数据.xlsx'#商品数据的路径 EXCEL_STORE=get_resources_path(os.path.join("resources",'店铺.xlsx'))#'resources'+ os.sep + '店铺.xlsx'#店铺数据的路径 EXCEL_WAREHOUSE=get_resources_path(os.path.join("resources",'库房.xlsx'))#'resources'+ os.sep + '库房.xlsx'#库房数据的路径 EXCEL_CUSTOMER=get_resources_path(os.path.join("resources",'顾客数据.xlsx'))#'resources'+ os.sep + '顾客数据.xlsx'#顾客数据的路径 class MainForm: #定义窗体类,父类 def __init__(self):#构造窗体 self.root=tkinter.Tk()#创建一个窗体 self.root.title("露露小卖店")#设置标题 self.root.iconbitmap(LOGO_PATH)#设置logo资源 self.root.geometry('680x500+150+50')#初始化窗口大小 self.root.resizable(False,False) #固定窗口大小 def worcloud_s(self,text): #生产词云图片 wc= WordCloud( collocations=False, font_path=r"C:/Windows/Fonts/simfang.ttf", background_color="black", width=600, height=300, max_words=50).generate(text) wc.to_image() wc.to_file("词云图.png") class The_login(MainForm): #登录窗口 def __init__(self): super().__init__() photo = tkinter.PhotoImage(file=IMAGES_PATH) # 设置图像资源 tkinter.Label(self.root, image=photo,width=680,height=500).place(x=0,y=0) tkinter.Label(self.root, text="用户名", font=("黑体", 20)).place(x=150,y=200) self.entry1 = tkinter.Entry(self.root,font=("黑体", 20)) self.entry1.place(x=300,y=200) tkinter.Label(self.root, text="密码", font=("黑体", 20)).place(x=150,y=250) self.entry2 = tkinter.Entry(self.root,font=("黑体", 20),show="*") self.entry2.place(x=300,y=250) tkinter.Button(self.root, text="店铺登录",command=self.bidui, width=15, font=("微软雅黑", 10)).place(x=150,y=300) tkinter.Button(self.root, text="退出", command=self.root.quit, width=15, font=("微软雅黑", 10)).place(x=450, y=300) tkinter.Button(self.root, text="导入店铺信息", command=self.store_in, width=10, font=("微软雅黑", 10)).place(x=550, y=450) tkinter.Button(self.root, text="店铺注册", command=self.Registered, width=10, font=("微软雅黑", 10)).place(x=0, y=450) self.root1=self.root.mainloop() #对用户账号,密码进行查询,比对 def bidui(self): global global_id def find_store():#查找店铺账号和密码数据 cursor=conn.cursor() sql='select * from store where store_id="%s" and store_password="%s"'%(self.entry1.get(),self.entry2.get()) cursor.execute(sql) conn.commit() cursor.close() return cursor.fetchone() try : if bool(find_store())==1: print("登录成功") global_id=self.entry1.get() self.root.destroy() The_store() else: labell = tkinter.Label(self.root, text='登陆失败!', font=("黑体", 10)) ## labell.place(x=300, y=0) except: pass def store_in(self): def remove_store_all(): # 清除店铺中的数据表数据 cursor = conn.cursor() # 创建游标 cursor.execute('delete from store') conn.commit() # 提交 cursor.close() # 关闭游标 def store_Add(): # 向店铺的表中导入数据 cursor = conn.cursor() # 创建游标 for co in range(len(df["店铺账号"])-1): print(co) try: sql = 'insert into store values (%s,%s,%s,%s,%s,%s,%s);' # 添加数据 cursor.execute(sql,( df["店铺账号"][co], df["店铺密码"][co], df["店铺名称"][co], df["店铺地址"][co], df["店铺电话"][co], df["店主"][co], df["盈亏"][co])) # 执行添加数据 conn.commit() # 提交 except: print(df["店铺名称"][co]) cursor.close() # 关闭游 df = pd.read_excel(EXCEL_STORE) remove_store_all() store_Add() #——————————————————————-——————————店铺注册------------------------------ def Registered(self): #店铺注册信息 def remove_prompt(): # 清空提示信息 Entry_prompt.delete(0.0, tkinter.END) Entry_prompt.update() Entry_prompt.insert(tkinter.INSERT,"———————————店铺注册——————————") def store_add():#添加店铺信息 if Entry_user.get()=='': #用户名不能为空 remove_prompt() Entry_prompt.insert(tkinter.INSERT, "用户名输入为空n") elif Entry_password.get()=='': #密码不能为空 remove_prompt() Entry_prompt.insert(tkinter.INSERT, "密码输入为空n") elif Entry_name.get() == '': # 名称不能为空 remove_prompt() Entry_prompt.insert(tkinter.INSERT, "店铺名称输入为空n") elif Entry_address.get() == '': # 地址不能为空 remove_prompt() Entry_prompt.insert(tkinter.INSERT, "店铺地址输入为空n") elif Entry_phon.get() == '': # 电话不能为空 remove_prompt() Entry_prompt.insert(tkinter.INSERT, "店铺电话输入为空n") elif Entry_master.get() == '': # 店主不能为空 remove_prompt() Entry_prompt.insert(tkinter.INSERT, "店主输入为空n") else: remove_prompt() cursor = conn.cursor() # 创建游标 try: sql = 'insert into store values (%s,%s,%s,%s,%s,%s,%s);' # 添加数据 cursor.execute(sql, ( Entry_user.get(), #账号 Entry_password.get(),#密码 Entry_name.get(),#名称 Entry_address.get(),#地址 Entry_phon.get(),#电话 Entry_master.get(),#店主 0 )#盈亏 ) # 执行添加数据 conn.commit() # 提交 cursor.execute('select * from store where store_id="%s";'%(Entry_user.get())) conn.commit() Entry_prompt.insert(tkinter.INSERT, "店铺注册成功n") Entry_prompt.insert(tkinter.INSERT, cursor.fetchall()) except: Entry_prompt.insert(tkinter.INSERT, "店铺账号已存在,请从新输入:n") cursor.close() # 关闭游 root=tkinter.Toplevel() #创建一个附属窗口 root.title("露露小卖店") #标题 root.iconbitmap(LOGO_PATH) # 设置logo资源 root.geometry('380x400+150+50') # 初始化窗口大小 root.resizable(False, False) # 固定窗口大小 tkinter.Button(root,text="确认添加",command=store_add,font=("微软雅黑", 10)).place(x=30,y=360) tkinter.Button(root, text="退出", command=root.destroy, font=("微软雅黑", 10)).place(x=320, y=360) #店铺账号 店铺密码 店铺名称 店铺地址 店铺电话 店主 tkinter.Label(root,text="店铺账号",font=("微软雅黑", 10)).place(x=60,y=120) tkinter.Label(root, text="店铺密码", font=("微软雅黑", 10)).place(x=60, y=160) tkinter.Label(root, text="店铺名称", font=("微软雅黑", 10)).place(x=60, y=200) tkinter.Label(root, text="店铺地址", font=("微软雅黑", 10)).place(x=60, y=240) tkinter.Label(root, text="店铺电话", font=("微软雅黑", 10)).place(x=60, y=280) tkinter.Label(root, text="店 主", font=("微软雅黑", 10)).place(x=60, y=320) Entry_prompt = tkinter.Text(root, width=44,height=5,font=("微软雅黑", 10)) Entry_prompt.place(x=12, y=10) Entry_user=tkinter.Entry(root,font=("微软雅黑", 10)) Entry_user.place(x=120,y=120) Entry_password = tkinter.Entry(root, font=("微软雅黑", 10)) Entry_password.place(x=120, y=160) Entry_name = tkinter.Entry(root, font=("微软雅黑", 10)) Entry_name.place(x=120, y=200) Entry_address = tkinter.Entry(root, font=("微软雅黑", 10)) Entry_address.place(x=120, y=240) Entry_phon = tkinter.Entry(root, font=("微软雅黑", 10)) Entry_phon.place(x=120, y=280) Entry_master = tkinter.Entry(root, font=("微软雅黑", 10)) Entry_master.place(x=120, y=320) remove_prompt() class The_store(MainForm):#主页面设置 def __init__(self): super().__init__() self.Label=tkinter.Label(self.root,text="欢迎光临露露小卖店",bg='#233233',width=32,height=10, fg='#ffffff',font=("黑体",30)) self.Label.place(x=20,y=0) self.button1 = tkinter.Button(self.root, text="初始化", command=self.Ininia, height=3, width=10).place(x=0,y=430) self.button2 = tkinter.Button(self.root, text="商品", command=self.Commodity_switch, height=3, width=10).place(x=150,y=430) self.button3 = tkinter.Button(self.root, text="店铺", command=self.Shop_switch, height=3, width=10).place(x=300,y=430) self.button4 = tkinter.Button(self.root, text="库房", command=self.Warehouse_switch, height=3, width=10).place(x=450,y=430) self.button1 = tkinter.Button(self.root, text="退出", command=quit, height=3, width=10).place(x=600,y=430) self.root.mainloop() def Commodity_switch(self): #商品 self.root.destroy() #关闭主界面 Commodity_From() #进入商品界面 def Shop_switch(self): #店铺 self.root.destroy() # 关闭主界面 Shop_From() def Warehouse_switch(self): #库房 self.root.destroy() # 关闭主界面 Warehouse() def Ininia(self): #具体初始化 self.Label.place_forget() text=tkinter.Text(self.root,width=80,height=20,font=('微软雅黑', 10)) scroll = tkinter.Scrollbar() scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y) text.place(x=20, y=20) scroll.config(command=text.yview) text.config(yscrollcommand=scroll.set) text.insert(tkinter.INSERT,"----------数据加载开始---------n") INn=Initialize() def After_xx(): for i in INn.Accord(): text.insert(tkinter.INSERT,i) text.insert(tkinter.INSERT, 'n') text.insert(tkinter.END,"数据加载完毕") self.root.after(100,After_xx()) class Initialize: #对数据库进行初始化 def __init__(self): self.df_commodity=pd.read_excel(EXCEL_COMMODITY) self.df_warehouse = pd.read_excel(EXCEL_WAREHOUSE) self.df_customer = pd.read_excel(EXCEL_CUSTOMER) self.remove_all() self.all_refresh() def remove_all(self): #清除数据表数据 cursor = conn.cursor() #创建游标 cursor.execute('delete from commodity;') #清除商品数据 cursor.execute('delete from warehouse;') #清除库房信息 cursor.execute('delete from customer') #清除顾客信息 conn.commit() #提交 cursor.close() #关闭游标 def all_refresh(self): def commodity_Add():#向商品commodity的表中导入数据'DW-1201090311','其它蔬菜',4,2,20210101,'2年',1,8 global global_id cursor = conn.cursor() #创建游标 for co in range(len(self.df_commodity["商品编码"])): try: sql = 'insert into commodity values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'#添加数据 cursor.execute(sql, ( self.df_commodity["商品编码"][co], self.df_commodity["商品名称"][co], self.df_commodity["商品售价"][co], self.df_commodity["商品进价"][co], self.df_commodity["生产日期"][co], self.df_commodity["保质期"][co], self.df_commodity["商品规格"][co], self.df_commodity["销售数量"][co], global_id, self.df_commodity["库房编号"][co], None) ) # 执行添加数据 except: print(self.df_commodity["商品编码"][co]) conn.commit() # 提交 cursor.close() # 关闭游标 def warehouse_Add():#向库房的表中导入数据'DW-1201090311','其它蔬菜',4,2,20210101,'2年',1,8 global global_id cursor = conn.cursor() #创建游标 for co in range(len(self.df_warehouse["库房编号"])): try: sql = 'insert into warehouse values (%s,%s,%s,%s,%s,%s,%s);'#添加数据 cursor.execute(sql, ( self.df_warehouse["库房编号"][co], self.df_warehouse["库房密码"][co], self.df_warehouse["库房名称"][co], self.df_warehouse["库房地址"][co], self.df_warehouse["库房电话"][co], self.df_warehouse["库房管理人"][co], global_id ) ) # 执行添加数据 except: print(self.df_warehouse["库房编号"][co]) conn.commit() # 提交 cursor.close() # 关闭游标 def customer_Add():#向顾客的表中导入数据 global global_id def go_shopping(): #查看购物情况 sql=' select sum(commodity_amount) from commodity GROUP BY customer_id;' cursor.execute(sql) conn.commit() return cursor.fetchall() cursor = conn.cursor() # 创建游标 for co in range(len(self.df_customer["用户账号"])): try: sql = 'insert into customer values (%s,%s,%s,%s,%s,%s);'#添加数据 cursor.execute(sql, ( self.df_customer["用户账号"][co], self.df_customer["密码"][co], self.df_customer["用户名"][co], self.df_customer["电话"][co], go_shopping(), global_id ) ) # 执行添加数据 except: print(self.df_customer["用户账号"][co]) conn.commit() # 提交 cursor.close() # 关闭游标 commodity_Add() warehouse_Add() customer_Add() def Accord(self): cursor = conn.cursor() #创建游标 a=[] for co1 in range(len(self.df_commodity["商品编码"])): try: sql='select * from commodity where commodity_id="%s"'%(self.df_commodity["商品编码"][co1]) cursor.execute(sql) a.append(cursor.fetchall()) #结果导出 except: continue for co2 in range(len(self.df_warehouse["库房编号"])): try: sql='select * from warehouse where warehouse_id="%s"'%(self.df_warehouse["库房编号"][co2]) cursor.execute(sql) a.append(cursor.fetchall()) #结果导出 except: continue for co in range(len(self.df_customer["用户账号"])): try: sql='select * from customer where customer_id="%s"'%(self.df_customer["用户账号"][co]) cursor.execute(sql) a.append(cursor.fetchall()) #结果导出 except: continue conn.commit() #提交 cursor.close() #关闭游标 return a class Commodity_From(MainForm): #商品界面设置 def __init__(self): super().__init__() self.Commodity_select_photo() photo_word = tkinter.PhotoImage(file=WORDCLOUD_PATH) tkinter.Label(self.root, image=photo_word, width=600, height=400).place(x=30, y=0) tkinter.Label(self.root,text="------------你的年度销量---------",font=("微软雅黑", 20)).place(x=100,y=0) self.button1 = tkinter.Button(self.root, text="增加", command=self.Commodity_add_s, height=3, width=10).place(x=0, y=430) self.button2 = tkinter.Button(self.root, text="删除", command=self.Commodity_drop_s, height=3, width=10).place(x=150, y=430) self.button3 = tkinter.Button(self.root, text="修改", command=self.Commodity_delente_s, height=3, width=10).place(x=300, y=430) self.button4 = tkinter.Button(self.root, text="查询", command=self.Commodity_select_s, height=3, width=10).place(x=450, y=430) self.button1 = tkinter.Button(self.root, text="返回", command=self.The_store_power, height=3, width=10).place(x=600, y=430) self.root.mainloop() def Commodity_add_s(self):# 进入增加界面 self.root.destroy() # 关闭商品界面 Commodity_add() #进入增加界面 def Commodity_drop_s(self):#进入删除界面 self.root.destroy() Commodity_drop() def Commodity_delente_s(self): #进去修改界面 self.root.destroy() Commodity_delente() def Commodity_select_s(self): #进入查询界面 self.root.destroy() Commodity_select() def&