from tkinter import * from tkinter import ttk import time from tkinter import messagebox win = Tk() win.title('家康粮油蔬菜水果超市--用户管理系统') win.geometry('800x600+400+200') win.resizable(width=False,height=False) #win.attributes('-topmost',-1) #会员查询 group1 = LabelFrame(win,text='查询',font=(None ,10),padx=5,pady=5) group1.grid(row=0,column = 0,padx=20,pady=3) Label(group1,text = '请输入用户姓名/卡号/手机号:',font=(None ,10)).grid(row=0,column = 0,padx=10,pady=10) Id = StringVar() Id.set('') user = Entry(group1,textvariable = Id,width = 35) user.grid(row=0,column = 1,padx=10,pady=10) def clean(): user.delete(0,END) #会员信息 group2 = LabelFrame(win,text='会员信息',font=(None ,10),padx=5,pady=5) group2.grid(row=1,column = 0,padx=10,pady=3) Label(group2,text = '卡号:',font=(None ,10)).grid(row=1,column = 0,padx=5,pady=5) varSid = StringVar() varSid.set('') sid = Entry(group2,width=10,state='disabled',textvariable = varSid) sid.grid(row=1,column = 1,padx=5,pady=5) Label(group2,text = '姓名:',font=(None ,10)).grid(row=1,column = 2,padx=5,pady=5) varName = StringVar() varName.set('') name = Entry(group2,width=10,state='disabled',textvariable = varName) name.grid(row=1,column = 3,padx=5,pady=5) Label(group2,text = '电话:',font=(None ,10)).grid(row=1,column = 4,padx=5,pady=5) varTel = StringVar() varTel.set('') tel = Entry(group2,width=20,state='disabled',textvariable = varTel) tel.grid(row=1,column = 5,padx=5,pady=5) Label(group2,text = '地址:',font=(None ,10)).grid(row=1,column = 6,padx=5,pady=5) varAdd = StringVar() varAdd.set('') adress = Entry(group2,width=30,state='disabled',textvariable = varAdd) adress.grid(row=1,column = 7,padx=5,pady=5) #消费记录 group3 = LabelFrame(win,text='客户消费明细',font=(None ,10),padx=5,pady=5) group3.grid(row=3,column = 0,columnspan=3,padx=10,pady=1) tree = ttk.Treeview(group3,show='headings',columns=('col1','col2','col3','col4', 'col5','col6','col7','col8',)) tree.column('col1',width=70,anchor='center') tree.heading('col1',text='卡号') tree.column('col2',width=80,anchor='center') tree.heading('col2',text='姓名') tree.column('col3',width=110,anchor='center') tree.heading('col3',text='最近消费金额') tree.column('col4',width=120,anchor='center') tree.heading('col4',text='最近消费时间') tree.column('col5',width=140,anchor='center') tree.heading('col5',text='消费总额') tree.column('col6',width=80,anchor='center') tree.heading('col6',text='可兑换次数') tree.column('col7',width=80,anchor='center') tree.heading('col7',text='已兑换次数') tree.column('col8',width=60,anchor='center') tree.heading('col8',text='备注') #加滚动条 vbar = Scrollbar(group3, orient=VERTICAL) vbar.config(command = tree.yview) tree.configure(yscrollcommand=vbar.set) tree.grid(row=3) vbar.grid(row=3, column=1,sticky=NS) tree.grid() #积分兑换 group4 = LabelFrame(win,text='积分兑换',font=(None ,10),padx=5,pady=5) group4.grid(row=2,column =0,padx=20,pady=3) Label(group4,text = '消费总额:',font=(None ,10)).grid(row=0,column = 0,padx=5,pady=5) varTot = StringVar() varTot.set('') total = Entry(group4,width=10,state='disabled',textvariable = varTot) total.grid(row=0,column = 1,padx=5,pady=5) Label(group4,text = '可兑换次数:',font=(None ,10)).grid(row=0,column = 2,padx=5,pady=5) varKe = StringVar() varKe.set('') total = Entry(group4,width=10,state='disabled',textvariable = varKe) total.grid(row=0,column = 3,padx=5,pady=5) Label(group4,text = '已兑换次数:',font=(None ,10)).grid(row=0,column = 4,padx=5,pady=5) varYi = StringVar() varYi.set('') total = Entry(group4,width=10,state='disabled',textvariable = varYi) total.grid(row=0,column = 5,padx=5,pady=5) #查询 def chaXun(): userList = [] tag = False file = open('Data.cyp','r') for j in file: userDict = {} temp = [] temp.append(j.split('|')) userDict['卡号'] = temp[0][0] userDict['姓名'] = temp[0][1] userDict['最近消费金额'] = temp[0][2] userDict['最近消费时间'] = temp[0][3] userDict['消费总额'] = temp[0][4] userDict['可兑换次数'] = temp[0][5] userDict['已兑换次数'] = temp[0][6] userDict['备注'] = temp[0][7] userDict['电话'] = temp[0][8] userDict['地址'] = temp[0][9][:-1] userList.append(userDict) #print(userList) #print(len(Id.get())) if Id.get()!='': #加卡号和电话号 for i in range(len(userList)): if Id.get() == userList[i]['电话']: varSid.set(userList[i]['卡号']) varName.set(userList[i]['姓名']) varTel.set(userList[i]['电话']) varAdd.set(userList[i]['地址']) varTot.set(userList[i]['消费总额']) varKe.set(userList[i]['可兑换次数']) varYi.set(userList[i]['已兑换次数']) tag = True break for i in range(len(userList)): #Id.get() == userList[i]['姓名'] if Id.get() == userList[i]['姓名']: varSid.set(userList[i]['卡号']) varName.set(userList[i]['姓名']) varTel.set(userList[i]['电话']) varAdd.set(userList[i]['地址']) varTot.set(userList[i]['消费总额']) varKe.set(userList[i]['可兑换次数']) varYi.set(userList[i]['已兑换次数']) tag = True break #加电话号 #print(userList) for i in range(len(userList)): if Id.get() == userList[i]['卡号']: varSid.set(userList[i]['卡号']) varName.set(userList[i]['姓名']) varTel.set(userList[i]['电话']) varAdd.set(userList[i]['地址']) varTot.set(userList[i]['消费总额']) varKe.set(userList[i]['可兑换次数']) varYi.set(userList[i]['已兑换次数']) tag = True break if tag == False: messagebox.showinfo('提示','没有发现此用户!!') else: messagebox.showinfo('提示','请先输入卡号再继续操作!') return userList Button(group1,text='查询',width = 15,command = chaXun).grid(row=0,column = 2,padx=10,pady=10) #相关操作 group5 = LabelFrame(win,text='操作管理',font=(None ,10),padx=5,pady=5) group5.grid(row=4,column =0,padx=20,pady=5) #添加用户 def add(): addWin = Tk() addWin.geometry('300x350+500+250') addWin.title('添加用户') addWin.resizable(width=False,height=False) #addWin.attributes('-topmost',-1) Label(addWin,text = '卡号:',font=(None ,10)).grid(row=0,column = 0,padx=15,pady=15) addId = Entry(addWin,width=30) addId.grid(row=0,column = 1,padx=5,pady=5) Label(addWin,text = '姓名:',font=(None ,10)).grid(row=1,column = 0,padx=15,pady=15) addName = Entry(addWin,width=30) addName.grid(row=1,column = 1,padx=5,pady=5) Label(addWin,text = '手机:',font=(None ,10)).grid(row=2,column = 0,padx=15,pady=15) addTel = Entry(addWin,width=30) addTel.grid(row=2,column = 1,padx=5,pady=5) Label(addWin,text = '地址:',font=(None ,10)).grid(row=3,column = 0,padx=15,pady=15) addAdress = Entry(addWin,width=30) addAdress.grid(row=3,column = 1,padx=5,pady=5) Label(addWin,text = '备注:',font=(None ,10)).grid(row=4,column = 0,padx=15,pady=15) addRemarks = Entry(addWin,width=30) addRemarks.grid(row=4,column = 1,padx=5,pady=5) def addUser(): resList = [] file = open('Data.cyp','r') for j in file: userDict = {} temp = [] j = j.strip(' ') temp.append(j.split('|')) if temp[0][0]!=' ': userDict['卡号'] = temp[0][0] userDict['姓名'] = temp[0][1] userDict['最近消费金额'] = temp[0][2] userDict['最近消费时间'] = temp[0][3] userDict['消费总额'] = temp[0][4] userDict['可兑换次数'] = temp[0][5] userDict['已兑换次数'] = temp[0][6] userDict['备注'] = temp[0][7] userDict['电话'] = temp[0][8] userDict['地址'] = temp[0][9] resList.append(userDict) file.close() atg = False for i in range(len(resList)): if addId.get() == resList[i]['卡号']: atg = True if atg == True: messagebox.showinfo('警告','你输入的卡号已经存在!!!') addWin.destroy() else: r = messagebox.askyesno('提示','确定添加该用户吗?') if addId.get()==''or addName.get()==''or addTel.get()==''or addAdress.get()==''or addRemarks=='': messagebox.showinfo('提示','您输入的信息不完整!') addWin.destroy() elif r == True: file = open('Data.cyp','a+') file.write(addId.get()+'|') file.write(addName.get()+'|') file.write('0'+'|')#最近消费金额 file.write(str(time.strftime('%d/%m/%Y'))+'|')#最近消费时间 file.write('0'+'|')#消费总额 file.write('0'+'|')#可兑换次数 file.write('0'+'|')#已兑换次数 file.write(addRemarks.get()+'|') file.write(addTel.get()+'|') file.write(addAdress.get()) file.write(' ') file.close() messagebox.showinfo('提示','添加成功!') addWin.destroy() else: addWin.destroy() Button(addWin,text='添加',width = 20,command = addUser).grid(row=5,column = 0,columnspan=2,padx=15,pady=15) #浏览用户 def infoView(): items = tree.get_children() for item in items: tree.delete(item) file = open('Data.cyp','r') userList = [] for i in file: t = i.split('|') userList.append(t) userList.sort(key=lambda x:x[4]) for i in range(len(userList)): tree.insert('',0,values=(userList[i][0],userList[i][1],userList[i][2],userList[i][3], userList[i][4],userList[i][5],userList[i][6],userList[i][7])) file.close() #读取文件 def read(): resList = [] file = open('Data.cyp','r') for j in file: userDict = {} temp = [] j = j.strip(' ') temp.append(j.split('|')) if temp[0][0]!=' ': userDict['卡号'] = temp[0][0] userDict['姓名'] = temp[0][1] userDict['最近消费金额'] = temp[0][2] userDict['最近消费时间'] = temp[0][3] userDict['消费总额'] = temp[0][4] userDict['可兑换次数'] = temp[0][5] userDict['已兑换次数'] = temp[0][6] userDict['备注'] = temp[0][7] userDict['电话'] = temp[0][8] userDict['地址'] = temp[0][9] resList.append(userDict) file.close() return resList def delUser(): iids = tree.selection() name = tree.item(iids,'values') resList = read() #print(resList) ''' #读取文件 resList = [] file = open('Data.cyp','r') for j in file: userDict = {} temp = [] temp.append(j.split('|')) userDict['序号'] = temp[0][0] userDict['姓名'] = temp[0][1] userDict['最近消费金额'] = temp[0][2] userDict['最近消费时间'] = temp[0][3] userDict['消费总额'] = temp[0][4] userDict['可兑换次数'] = temp[0][5] userDict['已兑换次数'] = temp[0][6] userDict['备注'] = temp[0][7] userDict['电话'] = temp[0][8] userDict['地址'] = temp[0][9][:-1] resList.append(userDict) file.close() ''' if iids == '': messagebox.showinfo('提示','请先选中您所要删除的用户!') else: r = messagebox.askyesno('提示','确定删除该用户吗?') if r == True: for i in range(len(resList)): if name[1] == resList[i]['姓名']: del resList[i] tree.delete(iids) messagebox.showinfo('提示','删除成功') break #写入文件 file = open('Data.cyp','w') for j in range(len(resList)): file.write( resList[j]['卡号']+'|') file.write( resList[j]['姓名']+'|') file.write( resList[j]['最近消费金额']+'|') file.write(resList[j]['最近消费时间']+'|') file.write( resList[j]['消费总额']+'|') file.write( resList[j]['可兑换次数']+'|') #file.write( resList[j]['可兑换次数']+'|') file.write( resList[j]['已兑换次数']+'|') file.write( resList[j]['备注']+'|') file.write( resList[j]['电话']+'|') file.write( resList[j]['地址']) file.write(' ') file.close() infoView() def helpme(): messagebox.showinfo('提示','寒江独钓 QQ:35959248! 电话:13630622588') def winExit(): win.destroy() #消费记录 def buy(): if Id.get()=='': messagebox.showinfo('提示','请先输入姓名再继续操作!') else: chaXun() r = messagebox.askyesno('提示','确定是该用户吗?') if r == True: buyWin = Tk() buyWin.geometry('250x200+500+350') buyWin.title('添加购物记录') varTime = str(time.strftime('%d/%m/%Y')) Label(buyWin,text = '购物时间:',font=(None ,10)).grid(row=0,column = 0,padx=10,pady=30) Label(buyWin,text=varTime,font=(None ,10)).grid(row=0,column = 1,padx=10,pady=10) Label(buyWin,text = '消费金额:',font=(None ,10)).grid(row=1,column = 0,padx=10,pady=10) varTot = StringVar() buyNum = Entry(buyWin,width=15,textvariable = varTot) buyNum.grid(row=1,column = 1,padx=10,pady=10) #更新购物金额 def update(): resList = chaXun() #print(resList) for i in range(len(resList)): if varSid.get() == resList[i]['卡号']: resList[i]['最近消费金额']=buyNum.get() resList[i]['消费总额']=str(int(resList[i]['消费总额'])+int(resList[i]['最近消费金额'])) messagebox.showinfo('提示','更新成功') #写入文件 file = open('Data.cyp','w') for j in range(len(resList)): #print(resDict) file.write( resList[j]['卡号']+'|') file.write( resList[j]['姓名']+'|') file.write( resList[j]['最近消费金额']+'|') file.write(str(time.strftime('%d/%m/%Y'))+'|') file.write( resList[j]['消费总额']+'|') if ((int(resList[i]['消费总额'])-int(resList[i]['已兑换次数'])*500)/500) >=1: resList[i]['可兑换次数'] = str(int((int(resList[i]['消费总额'])-int(resList[i]['已兑换次数'])*500)/500)) else: resList[i]['可兑换次数'] = '0' #file.write( str(int(resList[j]['消费总额'])//500)+'|') file.write( resList[j]['可兑换次数']+'|') file.write( resList[j]['已兑换次数']+'|') file.write( resList[j]['备注']+'|') file.write( resList[j]['电话']+'|') file.write( resList[j]['地址']) file.write(' ') #print( resList[i]) file.close() buyWin.destroy() infoView() Button(buyWin,text='确定',width = 11,command = update).grid(row=2,column = 0,padx=10,pady=10) Button(buyWin,text='取消',width = 11,command = lambda:buyWin.destroy()).grid(row=2,column = 1,padx=10,pady=10) def zzz(): messagebox.showinfo('提示','功能开发中') Button(group1,text='消费记录',width = 15,command = buy).grid(row=0,column = 3,padx=10,pady=10) #Button(group1,text='自定义',width = 7,command = zzz).grid(row=0,column = 4,padx=10,pady=10) def clean(): infoView() Id.set('') varSid.set('') varName.set('') varTel.set('') varAdd.set('') varTot.set('') varKe.set('') varYi.set('') def menoyTotal(): menoySum = 0 resList = read() for i in range(len(resList)): menoySum = menoySum + int(resList[i]['消费总额']) messagebox.showinfo('恭喜','一共赚了'+str(menoySum)+'元') Button(group5,text='刷新数据',width = 11,command = clean).grid(row=4,column = 0,padx=10,pady=10) Button(group5,text='添加用户',width = 11,command = add).grid(row=4,column = 1,padx=10,pady=10) Button(group5,text='浏览用户',width = 11,command = infoView).grid(row=4,column = 2,padx=10,pady=10) Button(group5,text='删除用户',width = 11,command = delUser).grid(row=4,column = 3,padx=10,pady=10) Button(group5,text='销售总额',width = 11,command = menoyTotal).grid(row=4,column = 4,padx=10,pady=10) Button(group5,text='帮助',width = 11,command = helpme).grid(row=4,column = 5,padx=10,pady=10) Button(group5,text='退出',width = 11,command = winExit).grid(row=4,column = 6,padx=10,pady=10) def duihuan(): resList = read() if Id.get()=='': messagebox.showinfo('提示','请先输入姓名再继续操作!') else: r = messagebox.askyesno('提示','确定要兑换吗?') if r == True: for i in range(len(resList)): if varSid.get() == resList[i]['卡号']:# and resList[i]['可兑换次数'] > 0: if int(resList[i]['可兑换次数']) > 0: resList[i]['已兑换次数'] = str(int(resList[i]['已兑换次数']) + 1) if ((int(resList[i]['消费总额'])-int(resList[i]['已兑换次数'])*500)/500) >=1: resList[i]['可兑换次数'] = str(int((int(resList[i]['消费总额'])-int(resList[i]['已兑换次数'])*500)/500)) else: resList[i]['可兑换次数'] = '0' break else: messagebox.showinfo('提示','你的兑换次数已用完!') break #写入文件 file = open('Data.cyp','w') for j in range(len(resList)): file.write( resList[j]['卡号']+'|') file.write( resList[j]['姓名']+'|') file.write( resList[j]['最近消费金额']+'|') file.write(resList[j]['最近消费时间']+'|') file.write( resList[j]['消费总额']+'|') #file.write( str(int(resList[j]['可兑换次数'])//500)+'|') file.write( resList[j]['可兑换次数']+'|') file.write( resList[j]['已兑换次数']+'|') file.write( resList[j]['备注']+'|') file.write( resList[j]['电话']+'|') file.write( resList[j]['地址']) file.write(' ') file.close() infoView() Button(group4,text='申请兑换',width = 13,command = duihuan).grid(row=0,column = 6,padx=10,pady=10) Button(group4,text='自定义',width = 12,command = duihuan).grid(row=0,column = 7,padx=10,pady=10) mainloop()