当前位置:   article > 正文

Python图书管理系统(课设)_python图书管理系统课程设计

python图书管理系统课程设计

       基于python的tkinter,将图书管理系统的界面进行简单的开发,我在这里使用的方法比较简单理解,但是代码过于繁多。添加、删除及修改并未使用数据库,而是使用了csv文件的读取操作,从而替代数据库。

       基本效果如下图所示:

        该系统将所有代码全都放在一个文件中,源码如下:

  1. import os
  2. import tkinter
  3. import tkinter.messagebox
  4. from tkinter import *
  5. from tkinter import ttk
  6. class LoginPage (object):
  7. def __init__(self, master=None):
  8. self.root = master #定义内部变量root
  9. # 获取当前屏幕的宽高
  10. self.width = self.root.winfo_screenwidth()
  11. self.height = self.root.winfo_screenheight()
  12. # 设置窗口大小
  13. self.h = 600
  14. self.w = 800
  15. # 将窗口居中
  16. self.y = (self.height - 600) / 2
  17. self.x = (self.width - 800) / 2
  18. self.root.geometry("%dx%d+%d+%d" %(self.w,self.h,self.x,self.y))
  19. # 不允许修改窗口大小
  20. self.root.resizable(False,False)
  21. self.addnum = StringVar()
  22. self.addname = StringVar()
  23. self.addauthor = StringVar()
  24. self.addchu = StringVar()
  25. self.adddate = StringVar()
  26. self.addpri = StringVar()
  27. self.altnum = StringVar()
  28. self.altname = StringVar()
  29. self.altauthor = StringVar()
  30. self.altchu = StringVar()
  31. self.altdate = StringVar()
  32. self.altpri = StringVar()
  33. self.createPage()
  34. # 创建页面
  35. def createPage(self):
  36. #表格
  37. tree = ttk.Treeview(self.root)
  38. tree.place(x=10,y=10,width=780,height=260)
  39. # #定义列
  40. tree["columns"] = ("编号","书名","作者", "出版社", "出版日期", "价格")
  41. tree['show'] = 'headings'
  42. # 设置列,列还不显示
  43. tree.column("编号", width=80,anchor ='c')
  44. tree.column("书名", width=150,anchor ='c')
  45. tree.column("作者", width=150,anchor ='c')
  46. tree.column("出版社", width=150,anchor ='c')
  47. tree.column("出版日期", width=150,anchor ='c')
  48. tree.column("价格", width=100,anchor ='c')
  49. #设置表头
  50. tree.heading("编号", text="编号")
  51. tree.heading("书名", text="书名")
  52. tree.heading("作者", text="作者")
  53. tree.heading("出版社", text="出版社")
  54. tree.heading("出版日期", text="出版日期")
  55. tree.heading("价格", text="价格")
  56. #添加数据
  57. f = open('图书.csv','r',encoding='utf-8')
  58. for line in f.readlines():
  59. info = line[:-1].split(",")
  60. tree.insert("", 0, values=(info[0],info[1],info[2],info[3],info[4],info[5]))
  61. f.close()
  62. # 添加编号
  63. addnum = Label(self.root, text="编号",font=('微软雅黑',10,''),anchor='w')
  64. addnum.place(x=42.5,y=280,height=20,width=80)
  65. addnuminput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addnum)
  66. addnuminput.place(x=122.5,y=279,height=24,width=105)
  67. # 添加书名
  68. addname = Label(self.root, text="书名",font=('微软雅黑',10,''),anchor='w')
  69. addname.place(x=42.5,y=310,height=20,width=80)
  70. addnameinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addname)
  71. addnameinput.place(x=122.5,y=309,height=24,width=105)
  72. # 添加作者
  73. addauthor = Label(self.root, text="作者",font=('微软雅黑',10,''),anchor='w')
  74. addauthor.place(x=42.5,y=340,height=20,width=80)
  75. addauthorinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addauthor)
  76. addauthorinput.place(x=122.5,y=339,height=24,width=105)
  77. # 添加出版社
  78. addchu = Label(self.root, text="出版社",font=('微软雅黑',10,''),anchor='w')
  79. addchu.place(x=42.5,y=370,height=20,width=80)
  80. addchuinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addchu)
  81. addchuinput.place(x=122.5,y=369,height=24,width=105)
  82. # 添加出版日期
  83. adddate = Label(self.root, text="出版日期",font=('微软雅黑',10,''),anchor='w')
  84. adddate.place(x=42.5,y=400,height=20,width=80)
  85. adddateinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.adddate)
  86. adddateinput.place(x=122.5,y=399,height=24,width=105)
  87. # 添加价格
  88. addpri = Label(self.root, text="价格",font=('微软雅黑',10,''),anchor='w')
  89. addpri.place(x=42.5,y=430,height=20,width=80)
  90. addpriinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addpri)
  91. addpriinput.place(x=122.5,y=429,height=24,width=105)
  92. # 添加按钮
  93. add = Button(self.root,command=self.click, text ="添加书本",font=('微软雅黑',10,''),activeforeground='#ffffff',fg='#ffffff',activebackground='#7cba59',bd=2,bg='#2aa515')
  94. add.place(x=105,y=500,height=35,width=100)
  95. # 修改编号
  96. altnum = Label(self.root, text="编号",font=('微软雅黑',10,''),anchor='w')
  97. altnum.place(x=292.5,y=280,height=20,width=80)
  98. altnuminput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altnum)
  99. altnuminput.place(x=372.5,y=279,height=24,width=105)
  100. # 修改书名
  101. altname = Label(self.root, text="书名",font=('微软雅黑',10,''),anchor='w')
  102. altname.place(x=292.5,y=310,height=20,width=80)
  103. altnameinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altname)
  104. altnameinput.place(x=372.5,y=309,height=24,width=105)
  105. # 修改作者
  106. altauthor = Label(self.root, text="作者",font=('微软雅黑',10,''),anchor='w')
  107. altauthor.place(x=292.5,y=340,height=20,width=80)
  108. altauthorinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altauthor)
  109. altauthorinput.place(x=372.5,y=339,height=24,width=105)
  110. # 修改出版社
  111. altchu = Label(self.root, text="出版社",font=('微软雅黑',10,''),anchor='w')
  112. altchu.place(x=292.5,y=370,height=20,width=80)
  113. altchuinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altchu)
  114. altchuinput.place(x=372.5,y=369,height=24,width=105)
  115. # 修改出版日期
  116. altdate = Label(self.root, text="出版日期",font=('微软雅黑',10,''),anchor='w')
  117. altdate.place(x=292.5,y=400,height=20,width=80)
  118. altdateinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altdate)
  119. altdateinput.place(x=372.5,y=399,height=24,width=105)
  120. # 修改价格
  121. altpri = Label(self.root, text="价格",font=('微软雅黑',10,''),anchor='w')
  122. altpri.place(x=292.5,y=430,height=20,width=80)
  123. altpriinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altpri)
  124. altpriinput.place(x=372.5,y=429,height=24,width=105)
  125. # 修改按钮
  126. alter = Button(self.root,command=self.altclick, text ="修改书本",font=('微软雅黑',10,''),activeforeground='#ffffff',fg='#ffffff',activebackground='#7cba59',bd=2,bg='#2aa515')
  127. alter.place(x=350,y=500,height=35,width=100)
  128. # 保存按钮
  129. pre = Button(self.root,command=self.show, text ="保存书本信息",font=('微软雅黑',10,''),activeforeground='#ffffff',fg='#ffffff',activebackground='#7cba59',bd=2,bg='#2aa515')
  130. pre.place(x=595,y=500,height=35,width=100)
  131. # 写入判断输入框是否有空值
  132. def Isspace(self,text):
  133. temp = 0
  134. for i in text:
  135. if not i.isspace():
  136. temp = 1
  137. break
  138. if temp==1:
  139. return 0
  140. else:
  141. return 1
  142. # 检查写入是否有空值
  143. def click(self):
  144. addnum = self.addnum.get()
  145. addname = self.addname.get()
  146. addauthor = self.addauthor.get()
  147. addchu = self.addchu.get()
  148. adddate = self.adddate.get()
  149. addpri = self.addpri.get()
  150. if self.Isspace(addnum) or self.Isspace(addname) or self.Isspace(addauthor) or self.Isspace(addchu) or self.Isspace(adddate) or self.Isspace(addpri) :
  151. tkinter.messagebox.showerror(title='提示', message ="请填写所有信息")
  152. else:
  153. self.write(addnum,addname,addauthor,addchu,adddate,addpri)
  154. # 写入信息
  155. def write(self,addnum,addname,addauthor,addchu,adddate,addpri):
  156. f = open('图书.csv','r',encoding='utf-8')
  157. for line in f.readlines():
  158. info = line[:-1].split(",")
  159. if len(info)<6:
  160. break
  161. if info[0] ==addnum and info[1] ==addname:
  162. tkinter.messagebox.showinfo(title='结果', message ="已存在该图书信息!")
  163. f.close()
  164. return
  165. f.close()
  166. f = open('图书.csv','a',encoding='utf-8')
  167. f.write('{},{},{},{},{},{}\n'.format(addnum,addname,addauthor,addchu,adddate,addpri))
  168. f.close()
  169. tkinter.messagebox.showinfo(title='提示', message ="写入成功,点击保存后更新")
  170. # 检查修改信息是否空白
  171. def altclick(self):
  172. altnum = self.altnum.get()
  173. altname = self.altname.get()
  174. altauthor = self.altauthor.get()
  175. altchu = self.altchu.get()
  176. altdate = self.altdate.get()
  177. altpri = self.altpri.get()
  178. if self.Isspace(altnum) or self.Isspace(altname) or self.Isspace(altauthor) or self.Isspace(altchu) or self.Isspace(altdate) or self.Isspace(altpri) :
  179. tkinter.messagebox.showerror(title='提示', message ="输入项为空")
  180. else:
  181. self.modify(altnum,altname,altauthor,altchu,altdate,altpri)
  182. # 修改信息
  183. def modify(self,altnum,altname,altauthor,altchu,altdate,altpri):
  184. temp = 0
  185. with open("图书.csv","r",encoding="utf-8") as f:
  186. lines = f.readlines()
  187. with open("图书.csv","w",encoding="utf-8") as f_w:
  188. for line in lines:
  189. info = line[:-1].split(",")
  190. if info[0] ==altnum:
  191. temp = 1
  192. f_w.write('{},{},{},{},{},{}\n'.format(altnum,altname,altauthor,altchu,altdate,altpri))
  193. continue
  194. f_w.write(line)
  195. if temp==0:
  196. tkinter.messagebox.showerror(title='提示', message ="没有该信息")
  197. else:
  198. tkinter.messagebox.showinfo(title='提示', message ="修改成功,点击保存后更新")
  199. # 保存信息并显示
  200. def show(self):
  201. self.createPage()
  202. root = Tk()
  203. root.title('图书管理')
  204. LoginPage(root)
  205. root.mainloop()

       在运行代码前需要在同级文件夹下创建一个名为“图书”的csv文件,如下图所示:


扫描以下二维码,免费获取更多源码

扫描以下二维码,免费获取更多源码

扫描以下二维码,免费获取更多源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/679487
推荐阅读
相关标签
  

闽ICP备14008679号