from tkinter import * from tkinter import messagebox import csv from datetime import datetime from tkinter import ttk import demouser class library(Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.num1 = 0 # 判断打开另一个窗口的条件 self.pack() self.create(time=self.time2()) self.enter = Frame() self.look = Frame() self.showall() def time2(self): while True: times = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.title = Button(text=times, font=("宋体", 10), bg='white', relief=GROOVE, bd=10, width=30, height=1, command=self.time2) self.title.place(x=2, y=80) self.title.after(1000, self.time2) return times def find(self): flag = 0 bookName = self.bookName1.get() bookWriter = self.bookWriter1.get() bookPrice = self.bookPrice1.get() bookOut = self.bookOut1.get() bookInTime = self.bookInTime1.get() bookNum = self.bookNum1.get() filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as f: data1 = csv.reader(f) for line in data1: if line[0] == bookName: self.showName() flag += 1 break elif line[1] == bookWriter: self.showWriter() flag += 1 break elif line[2] == bookPrice: self.showPrice() flag += 1 break elif line[3] == bookOut: self.showOut() flag += 1 break elif line[4] == bookInTime: self.showInTime() flag += 1 break elif line[5] == bookNum: self.showNum() flag += 1 break if flag == 0: messagebox.showinfo('搜索', '不存在') def add(self): rebacker = 0 # 借阅者状态,通过用户名字判断,若状态为0则是无主状态 bookName = self.bookName1.get() bookWriter = self.bookWriter1.get() bookPrice = self.bookPrice1.get() bookOut = self.bookOut1.get() bookInTime = self.bookInTime1.get() bookNum = self.bookNum1.get() if bookName == '' or bookWriter == '' or bookPrice == '' \ or bookOut == '' or bookInTime == '' or bookNum == '': messagebox.showwarning('输入错误', "输入不能为空") else: bookstate = 1 datas =[[bookName, bookWriter, bookPrice, bookOut, bookInTime, bookNum, bookstate,rebacker]] filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as f: data1 = csv.reader(f) for row in data1: print(row) if row[0] == bookName and row[1] == bookWriter and row[2] == bookPrice \ and row[3] == bookOut and row[4] == bookInTime and row[5] == bookNum: messagebox.showinfo("入库", "已有该图书") return with open(filename, 'a', newline="", encoding='utf-8')as f: write = csv.writer(f) for row in datas: write.writerow(row) messagebox.showinfo("入库", "已入库") self.showall() def borrow(self): bookName = self.bookName1.get() bookWriter = self.bookWriter1.get() bookPrice = self.bookPrice1.get() bookOut = self.bookOut1.get() bookInTime = self.bookInTime1.get() bookNum = self.bookNum1.get() filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as h: data2 = csv.reader(h) st = list(data2) if bookName == '' or bookWriter == '' or bookPrice == '' \ or bookOut == '' or bookInTime == '' or bookNum == '': messagebox.showwarning('输入错误', "输入不能为空") else: num = 0 temp = 0 filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as f: data1 = csv.reader(f) for row in data1: if row[0] != bookName: num += 1 x = num # print(x) elif row[0] == bookName and row[1] == bookWriter and row[2] == bookPrice \ and row[3] == bookOut and row[4] == bookInTime and row[5] == bookNum: temp = 1 break with open(filename, 'r', newline="", encoding='utf-8') as h: data2 = csv.reader(h) st = list(data2) if temp == 1 and st[x][6] == '1' and st[x][7] == '0': messagebox.askyesnocancel('借阅', '是否借阅此书') if messagebox.YES: with open(filename, 'r', newline="", encoding='utf-8') as f: reader = csv.reader(f) book = list(reader) book[num][6] = 0 book[num][7] =demouser.app.usagename with open(filename, 'w', newline="", encoding='utf-8') as f1: writer = csv.writer(f1) for i in book: writer.writerow(i) self.showback() else: messagebox.askyesnocancel('借阅', '该书不存在或已经被借走') # 归还,代码与上面的借阅一致 def reback(self): bookName = self.bookName1.get() bookWriter = self.bookWriter1.get() bookPrice = self.bookPrice1.get() bookOut = self.bookOut1.get() bookInTime = self.bookInTime1.get() bookNum = self.bookNum1.get() filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as h: data2 = csv.reader(h) st = list(data2) if bookName == '' or bookWriter == '' or bookPrice == '' \ or bookOut == '' or bookInTime == '' or bookNum == '': messagebox.showwarning('输入错误', "输入不能为空") else: num = 0 temp = 0 filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as f: data1 = csv.reader(f) for row in data1: if row[0] != bookName: num += 1 x = num print(x) elif row[0] == bookName and row[1] == bookWriter and row[2] == bookPrice \ and row[3] == bookOut and row[4] == bookInTime and row[5] == bookNum: temp = 1 break with open(filename, 'r', newline="", encoding='utf-8') as h: data2 = csv.reader(h) st = list(data2) if temp == 1 and st[x][6] == '0' : messagebox.askyesnocancel('归还', '是否归还此书') if messagebox.YES and st[x][7] == demouser.app.usagename: with open(filename, 'r', newline="", encoding='utf-8') as f: reader = csv.reader(f) book = list(reader) book[num][6] = 1 book[num][7] = '0' with open(filename, 'w', newline="", encoding='utf-8') as f1: writer = csv.writer(f1) for i in book: writer.writerow(i) else: messagebox.showinfo('归还', '用户权限不足') self.showback() else: messagebox.askyesnocancel('借阅', '已经归还') # 创建界面 def create(self,time): self.title = Label(text="欢迎参观XXX的图书管理系统", font=("宋体", 40), bg='#e07b69', relief=GROOVE, bd=10) self.title.place(x=0, y=0) # 标签title self.booknumber = 0 # 获取ms时间戳 self.bookName = Label(text="书名", font=("宋体", 10), width=10, height=2, relief=SOLID, bd=2) self.bookWriter = Label(text="作者", font=("宋体", 10), width=10, height=2, relief=SOLID, bd=2) self.bookPrice = Label(text="价格", font=("宋体", 10), width=10, height=2, relief=SOLID, bd=2) self.bookOut = Label(text="出版社", font=("宋体", 10), width=10, height=2, relief=SOLID, bd=2) self.bookInTime = Label(text="入库时间", font=("宋体", 10), width=10, height=2, relief=SOLID, bd=2) self.booknum = Label(text="分配检索号", font=("宋体", 10), width=10, height=2, relief=SOLID, bd=2) self.bookName.place(x=2, y=220) self.bookWriter.place(x=2, y=250) self.bookPrice.place(x=2, y=280) self.bookOut.place(x=2, y=310) self.bookInTime.place(x=2, y=340) self.booknum.place(x=2, y=369) self.title = Button(text="图书列表" , font=("宋体", 10), bg='white', relief=GROOVE, bd=10, width=14, height=1, command=self.showall) self.title.place(x=250, y=80) self.title = Button(text='!!点击看借出去的书本列表!!', font=("宋体", 10), bg='white', relief=SOLID, bd=2, width=50, height=2,command=self.showback) self.title.place(x=2, y=122) # 输入文本框的途径和位置 self.bookName2 = StringVar() self.bookWriter2 = StringVar() self.bookPrice2 = StringVar() self.bookOut2 = StringVar() self.bookInTime2 = StringVar() self.bookNum2 = StringVar() self.bookName1 = Entry(font=("宋体", 20), width=20, relief=SOLID, bd=2, textvariable=self.bookName2) self.bookWriter1 = Entry(font=("宋体", 20), width=20, relief=SOLID, bd=2, textvariable=self.bookWriter2) self.bookPrice1 = Entry(font=("宋体", 20), width=20, relief=SOLID, bd=2, textvariable=self.bookPrice2) self.bookOut1 = Entry(font=("宋体", 20), width=20, relief=SOLID, bd=2, textvariable=self.bookOut2) self.bookInTime1 = Entry(font=("宋体", 20), width=20, relief=SOLID, bd=2, textvariable=self.bookInTime2) self.bookNum1 = Entry(font=("宋体", 20), width=20, relief=SOLID, bd=2, textvariable=self.bookNum2) self.bookName1.place(x=77, y=220) self.bookWriter1.place(x=77, y=250) self.bookPrice1.place(x=77, y=280) self.bookOut1.place(x=77, y=310) self.bookInTime1.place(x=77, y=340) self.bookNum1.place(x=77, y=369) # 事件触发按钮 self.bt1 = Button(text="搜索", width=14, height=1, bg="#AFEEEE", font=("宋体", 15), bd=2, relief=SOLID, command=self.find) self.bt2 = Button(text="入库", width=14, height=1, bg="#AFEEEE", font=("宋体", 15), bd=2, relief=SOLID, command=self.add) self.bt3 = Button(text="借阅", width=14, height=1, bg="#AFEEEE", font=("宋体", 15), bd=2, relief=SOLID, command=self.borrow) self.bt4 = Button(text="归还", width=14, height=1, bg="#AFEEEE", font=("宋体", 15), bd=2, relief=SOLID, command=self.reback) self.bt1.place(x=100, y=410) self.bt2.place(x=100, y=465) self.bt3.place(x=100, y=520) self.bt4.place(x=100, y=585) # 借阅时间的设定 tuk = LabelFrame(bd=2,relief=SOLID,width=360,height=50,text='借阅时间:') tuk.place(x=2, y=165) self.yearEntry = IntVar() self.monthEntry = IntVar() self.dayEntry = IntVar() self.year = Spinbox(from_=2023, to=2200, increment=1, textvariable=self.yearEntry, width=10) self.month = Spinbox(from_=1, to=12, increment=1, textvariable=self.monthEntry, wrap=True, width=10) self.day = Spinbox(from_=1, to=30, increment=1, textvariable=self.dayEntry, wrap=True, width=10) self.borrow =Button(text='确认',bd=2,relief=SOLID,height=1,width=8,command=self.TimeChoice) self.year.place(x=20, y=190) self.month.place(x=104, y=190) self.day.place(x=188, y=190) self.borrow.place(x=280,y=180) def show(self): num=0 filename = 'book.csv' with open(filename, 'r', newline="", encoding='utf-8') as h: data2 = csv.reader(h) st = list(data2) self.tree = ttk.Treeview(rootMain) self.tree.place(x=380, y=80, width=412, height=560) # #定义列 self.tree["columns"] = ("书名", "作者", "价格", "出版社", "入库时间", "分配检索号") self.tree['show'] = 'headings' # 设置列,列还不显示 self.tree.column("书名", width=20, anchor='c') self.tree.column("作者", width=40, anchor='c') self.tree.column("价格", width=40, anchor='c') self.tree.column("出版社", width=40, anchor='c') self.tree.column("入库时间", width=40, anchor='c') self.tree.column("分配检索号", width=23, anchor='c') # 设置表头 self.tree.heading("书名", text="书名") self.tree.heading("作者", text="作者") self.tree.heading("价格", text="价格") self.tree.heading("出版社", text="出版社") self.tree.heading("入库时间", text="入库时间") self.tree.heading("分配检索号", text="分配检索号") self.tree.selection() self.tree.bind('',self.showEntey) def showall(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[6] == '1': self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() def showback(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[6] == '0': self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() def showName(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[0] ==self.bookName2.get(): self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() # 按照作者在视窗显示 def showWriter(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[1] == self.bookWriter2.get(): self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() # 按照出版社在视窗显示 def showOut(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[3] == self.bookOut2.get(): self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() # 按照检索号在视窗显示 def showNum(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[5] == self.bookNum2.get(): self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() # 按照价格在视窗显示 def showPrice(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[2] == self.bookPrice2.get(): self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() # 按照入库时间在视窗显示 def showInTime(self): self.show() f = open('book.csv', 'r', encoding='utf-8') for line in f.readlines(): info = line[:-1].split(",") if info[4] == self.bookInTime2.get(): self.tree.insert("", 0, values=(info[0], info[1], info[2], info[3], info[4], info[5])) f.close() # 选中输入输入框 def showEntey(self,event): selcet1 = self.tree.selection() itemlist = self.tree.item(selcet1) x = list(itemlist.values()) itemchoice = list(x[2]) self.bookName2.set(itemchoice[0]) self.bookWriter2.set(itemchoice[1]) self.bookPrice2.set(itemchoice[2]) self.bookOut2.set(itemchoice[3]) self.bookInTime2.set(itemchoice[4]) self.bookNum2.set(itemchoice[5]) def TimeChoice(self): flag = 0 borrowTimelist = [self.yearEntry.get(), self.monthEntry.get(), self.dayEntry.get()] print(borrowTimelist) times = datetime.now().strftime("%Y-%m-%d") print(times.split('-')) print(int(borrowTimelist[0])-int(times[0])) for j in range(3): # 遍历三次 查看俩值是否相等 if int(borrowTimelist[j])-int(times[j]) == 0: print(int(borrowTimelist[j])-int(times[j])) flag += 1 elif int(borrowTimelist[j])-int(times[j]) < 0 and flag == 0: messagebox.showinfo("借阅", "请重新查看你的时间有误") if flag == 3: messagebox.showinfo("借阅", '您借阅的图书已到期限') if __name__ == '__main__': demouser.rootEnter = Tk() demouser.rootEnter.geometry('410x140'+'+400+300') demouser.rootEnter.title("图书管理系统") demouser.app = demouser.Application(master=demouser.rootEnter) demouser.rootEnter.mainloop() print(demouser.app.num) if demouser.app.num == 1: print(demouser.app.usagename) rootMain = Tk() rootMain.geometry('800x650'"+400+200") main1 = library(master=rootMain) rootMain.title("图书管理系统") rootMain.mainloop()