|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('主界面')
root.resizable(0,0) #防止用户调整尺寸
books = [{'num':'1','name':'图书管理','author':'xlsfxm','standing':99999,'total':123456},
{'num':'2','name':'西游记','author':'吴承恩','standing':99999,'total':123456},
{'num':'3','name':'三国演义','author':'罗贯中','standing':99999,'total':123456},
{'num':'4','name':'完美世界','author':'辰东','standing':99999,'total':123456},
{'num':'5','name':'斗破苍穹','author':'土豆','standing':99,'total':123456}
]
def main():
messagebox.showinfo('欢迎','欢迎进入图书馆系统主界面!' )
photo = PhotoImage(file='D:\\Dpan桌面\\图书馆主界面.gif')
w = Label(root,image=photo)
w.grid(rowspan = 20,columnspan = 50)
Label(root,text='书号').grid(row = 2,column=2)
Label(root,text='书名').grid(row = 3,column=2)
Label(root,text='箸者').grid(row = 4,column=2)
Label(root,text='增加的个数').grid(row = 5,column=2)
Label(root,text='现存量').grid(row = 8,column=2)
Label(root,text='总库量').grid(row = 9,column=2)
book_num = Entry()
book_num.grid(row=2,column=4)
book_name = Entry()
book_name.grid(row=3,column=4)
book_author = Entry()
book_author.grid(row=4,column=4)
book_add = Entry()
book_add.grid(row=5,column=4)
book_standing= Entry()
book_standing.grid(row=8,column=4)
book_total = Entry()
book_total.grid(row=9,column=4)
def clearEntry():#清空文本
book_num.delete(0,END)
book_name.delete(0,END)
book_author.delete(0,END)
book_standing.delete(0,END)
book_total.delete(0,END)
book_add.delete(0,END)
def new():#采编入库
if book_num.get() == ""or book_name.get() == ""or book_author.get()== "" or book_add.get() =="":
messagebox.showerror('出错了!','书籍信息未填全')
else:
for x in books:
if book_num.get() != x["num"]:
data=[{'num':book_num.get(),'name':book_name.get(),'author':book_author.get(),\
'standing':book_author.get(),'total':book_author.get()}]
books.append(data)
messagebox.showinfo('成功!','书籍入库成功啦!')
clearEntry()
print(book_num.get())
print(list(books))
else:
book_name.insert(0,x['name'])
book_author.insert(0,x['author'])
x['standing'] += int(book_add.get())
x['total'] +=int(book_add.get())
messagebox.showinfo('成功!','书籍二次入库成功!')
def delete():
index=0
count=0
for x in books:
if x['num'] == book_num.get() and x['name'] == book_name.get():
count =1
del books[index]
messagebox.showinfo('成功啦!','删除成功')
index +=1
if count==0:
messagebox.showerror('失败了!','查无此书')
break
def borrow():
messagebox.showinfo('开始借阅!','请在对应的文本框输入你想借的书籍号,点击确定开始,再次点击借阅将借阅书籍')
if book_num.get() != '':
for i in books:
if i['num'] == book_num.get() and i['num'] > 0:
i['standing'] -=1
i['total'] -=1
book_standing.insert(END,i['standing'])
book_total.insert(END,i['total'])
messagebox.showinfo('成功','成功借出')
break
else:
messagebox.showerror('出错了','没有找到指定的书籍!查无此书!')
else:
messagebox.showerror('出错了','请输入书籍号!')
def show():
if book_num.get() != "":
index = 1
for i in books:
if book_num.get() == i['num']:
book_name.insert(0,i['name'])
book_author.insert(0,i['author'])
book_standing.insert(0,i['standing'])
book_total.insert(0,i['total'])
messagebox.showinfo('成功','成功显示,请查看!')
break
elif index > len(books):
index +=1
else:
messagebox.showerror('出错了!','请输入书籍号')
def returnBook():
if book_num.get() != "":
for i in books:
if i['num'] == book_num.get():
i['standing'] +=1
i['total'] +=1
messagebox.showinfo('成功','成功归还,欢迎下次再借!')
break
else:
messagebox.showerror('出错了','没有找到指定的书籍!查无此书!')
break
else:
messagebox.showerror('出错了!','请在对应文本输入对应的书籍!')
newBook = Button(root,text='采编入库',width = 10,height = 2,command=new)
newBook.grid(row=2,column=30)
clearBook = Button(root,text='清除库存',width = 10,height = 2,command=delete)
clearBook.grid(row=3,column=30)
borrowBook = Button(root,text='借阅',width = 10,height = 2,command=borrow)
borrowBook.grid(row=6,column=30)
returnBook = Button(root,text='归还',width = 10,height = 2,command=returnBook)
returnBook.grid(row=7,column=30)
displayBook = Button(root,text='显示',width = 10,height = 2,command = show)
displayBook.grid(row=5,column=30)
displayBook = Button(root,text='退出',width = 10,height = 2,command = root.destroy)
displayBook.grid(row=10,column=30)
clearEntryB = Button(root,text='重新输入',width = 10,height = 2,command = clearEntry)
clearEntryB.grid(row=11,column=30)
mainloop()
main()
我新增了一个 新的图书 :{'num':'6','name':'python','author':'聪明绝顶的我','standing':99999,'total':123456}。
可是当我用show()时,系统报错 :
if book_num.get() == i['num']:
TypeError: list indices must be integers or slices, not str
我的show()方法是查找对应的书籍号。如果列表里有对应的书籍号,就获取改列表成员里的字典每一个key值。
可是如果我想查看原本就在列表里面的数据时,书籍号是 1 或者 2 或者 3 或者 4 或者 5 的时候。系统就不会报错。可以将该书籍的资料显示出来。
这是为什么啊?
第一点:
采编入库时, for x in books:
if book_num.get() != x["num"]:
data=[{'num':book_num.get(),'name':book_name.get(),'author':book_author.get(),\
'standing':book_author.get(),'total':book_author.get()}]
books.append(data)
messagebox.showinfo('成功!','书籍入库成功啦!')
clearEntry()
print(book_num.get())
print(list(books))
else:
book_name.insert(0,x['name'])
book_author.insert(0,x['author'])
x['standing'] += int(book_add.get())
x['total'] +=int(book_add.get())
messagebox.showinfo('成功!','书籍二次入库成功!')
这个不应该放在循环里做,因为我们每次只是添加一种书籍
第二点:
你这个程序没啥用啊,没有数据库保管这些资料,在程序重新启动时,所有修改都归零,你应该使用数据库(如果小型数据库你不会用的话,最低要求你也得使用文本数据库)
|
|