wxpython实现图书管理系统


Posted in Python onMarch 12, 2018

用wxpython实现的简单图书管理系统,可以实现增加图书,删除图书,修改图书,查看图书。后台数据库为mysql数据库,采用的pymysql连接数据库。系统界面如下:

wxpython实现图书管理系统

代码如下:

1.书本类代码

#author = liuwei date = 2017-06-02
from datetime import *   #导入日期模块
__metaclass__ = type
class Book:
 '''一个书本信息类,包括书本名字,作者名字和书本简单信息'''
 def __init__(self, bookName = "", author = "", content = ""):
 self.bookName = bookName   #书本名字
 self.author = author   #作者名字
 self.content = content   #书本信息
 self.add_date = date.today()   #书本添加日期

 def setBookName(self, name):
 self.bookName = name

 def getBookName(self):
 return self.bookName

 def setAuthor(self, author):
 self.author = author

 def getAuthor(self):
 return self.author

 def setContent(self, content):
 self.content = content

 def getContent(self):
 return self.content

 def getAddDate(self):
 return self.add_date


if __name__ == "__main__":
 mybook = Book()
 print(mybook.date)

2.数据库操作类代码

#author = liuwei date = 2017-06-02
#数据库帮助类
import pymysql
from book import *

__metaclass__ = type
class DBHelper:
 def getCon(self):
 '''获取操作数据库的curcor即游标,首先的建立连接,需要服务器地址,端口号,用户名,密码和数据库名'''
 #为了能用中文,得加上编码方式
 conn = pymysql.connect(host = "localhost", port = 3306, user = "root", password = "201392260", db = "library", charset = "utf8")

 return conn

 def insertBook(self, book):
 '''向数据库中book表插入书本信息,book为Book类对象,包含书本基本信息'''
 sql = "insert into book(name, author, content, add_date) values(%s, %s, %s, %s)"

 conn = self.getCon();
 if conn ==None:
 return

 cursor = conn.cursor()
 cursor.execute(sql, (book.getBookName(), book.getAuthor(), book.getContent(), book.getAddDate()))

 conn.commit()
 cursor.close()
 conn.close()

 new_id = cursor.lastrowid
 print("新插入键值id为:", new_id)

 return new_id

 def getAllBook(self):
 '''返回数据库中,book表中所有的书本信息'''
 sql = "select *from book"

 conn = self.getCon()
 if conn == None:
 return

 cursor = conn.cursor()
 rownum = cursor.execute(sql) #执行并返回找到的行数

 #获取查询结果
 rows = cursor.fetchall()
 list = []

 for item in rows:
 bitem = (item[0], item[1], str(item[4]))

 list.append(bitem)

 conn.commit()
 cursor.close()
 conn.close()

 return list

 def getBookById(self, bookid):
 '''根据书本id值来寻找书本信息'''

 sql = "select book.name, book.author, book.content from book where id=%s"

 conn = self.getCon()
 if conn == None:
 return

 cursor = conn.cursor()
 cursor.execute(sql, (bookid, ))  #参数以元组形式给出
 row = cursor.fetchone()  #取到第一个结果

 conn.commit()
 cursor.close()
 conn.close()

 return row   #返回该书本信息


 def saveUpdate(self, bookid, book):
 '''用book对象来修改id为bookid的书本信息'''
 sql = "update book set book.name=%s, book.author=%s, book.content=%s where book.id=%s"

 conn = self.getCon()
 if conn == None:
 return

 cursor = conn.cursor()
 cursor.execute(sql, (book.getBookName(), book.getAuthor(), book.getContent(), bookid))

 conn.commit()
 cursor.close()
 conn.close()

 def deleteBook(self, bookid):
 '''根据书本id来删除书籍'''
 sql = "delete from book where book.id = %s"

 conn = self.getCon()
 if conn == None:
 return

 cursor = conn.cursor()
 cursor.execute(sql, (bookid, ))

 conn.commit()
 cursor.close()
 conn.close()

if __name__ == '__main__':
 db = DBHelper()
 #book = Book("秦腔", "贾凹平", "讲的是大西北夏家和白家的事情,由引生口述。")
 #db.insertBook(book)
 list = db.getAllBook()
 for item in list:
 print(item)

3.主界面代码:

'''一个图书管理系统,能够实现增加书籍,删除书籍,
修改书籍和查看图书详情,基于mysql数据库和
wxPython'''

import wx
from book import *
from dbhelper import *

__metaclass__ = type

class AddFrame(wx.Frame):
 '''添加书籍弹出的小窗口'''

 def __init__(self, parent, title):
 '''初始化该小窗口的布局'''

 self.mainframe = parent
 #生成一个300*300的框
 wx.Frame.__init__(self, parent, title = title, size = (400, 250))

 self.panel = wx.Panel(self, pos = (0, 0), size = (400, 250))
 self.panel.SetBackgroundColour("#FFFFFF")  #背景为白色

 #三个编辑框,分别用来编辑书名,作者,书籍相关信息
 bookName_tip = wx.StaticText(self.panel, label = "书名:", pos = (5, 8), size = (35, 25))
 bookName_tip.SetBackgroundColour("#FFFFFF")
 bookName_text = wx.TextCtrl(self.panel, pos = (40, 5), size = (340, 25))
 self.name = bookName_text

 author_tip = wx.StaticText(self.panel, label = "作者:", pos = (5, 38), size = (35, 25))
 author_tip.SetBackgroundColour("#FFFFFF")
 author_text = wx.TextCtrl(self.panel, pos = (40, 35), size = (340, 25))
 self.author = author_text

 content_tip = wx.StaticText(self.panel, label = "内容:", pos = (5, 68), size = (340, 25))
 content_tip.SetBackgroundColour("#FFFFFF")
 content_text = wx.TextCtrl(self.panel, pos = (40, 65), size = (340, 100), style = wx.TE_MULTILINE)
 self.content = content_text

 save_button = wx.Button(self.panel, label = "保存书籍", pos = (160, 170))
 self.Bind(wx.EVT_BUTTON, self.saveBook, save_button)

 #需要用到的数据库接口
 self.dbhelper = DBHelper()


 def saveBook(self, evt):
 '''第一步:获取text中文本;第二步,连接数据库;第三步插入并获得主键;第四步添加到ListCtrl中'''
 bookName = self.name.GetValue()
 author = self.author.GetValue()
 content = self.content.GetValue()

 print("书名:"+bookName)
 if bookName == "" or author == "" or content == "":
 print("进来了")
 warn = wx.MessageDialog(self, message = "所有信息不能为空!!!", caption = "错误警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示错误
 warn.Destroy()
 return
 else:
 print("开始插入到数据库中")
 book = Book(bookName, author, content)
 book_id = self.dbhelper.insertBook(book)
 self.mainframe.addToList(book_id, book)

 self.Destroy()


class UpdateFrame(wx.Frame):
 def __init__(self, parent, title, select_id):
 '''初始化更新图书信息界面总布局'''

 wx.Frame(parent, title = title, size = (400, 250))

 #用来调用父frame,便于更新
 self.mainframe = parent
 #生成一个300*300的框
 wx.Frame.__init__(self, parent, title = title, size = (400, 250))

 self.panel = wx.Panel(self, pos = (0, 0), size = (400, 250))
 self.panel.SetBackgroundColour("#FFFFFF")  #背景为白色

 #三个编辑框,分别用来编辑书名,作者,书籍相关信息
 bookName_tip = wx.StaticText(self.panel, label = "书名:", pos = (5, 8), size = (35, 25))
 bookName_tip.SetBackgroundColour("#FFFFFF")
 bookName_text = wx.TextCtrl(self.panel, pos = (40, 5), size = (340, 25))
 self.name = bookName_text

 author_tip = wx.StaticText(self.panel, label = "作者:", pos = (5, 38), size = (35, 25))
 author_tip.SetBackgroundColour("#FFFFFF")
 author_text = wx.TextCtrl(self.panel, pos = (40, 35), size = (340, 25))
 self.author = author_text

 content_tip = wx.StaticText(self.panel, label = "内容:", pos = (5, 68), size = (340, 25))
 content_tip.SetBackgroundColour("#FFFFFF")
 content_text = wx.TextCtrl(self.panel, pos = (40, 65), size = (340, 100), style = wx.TE_MULTILINE)
 self.content = content_text

 save_button = wx.Button(self.panel, label = "保存修改", pos = (160, 170))
 self.Bind(wx.EVT_BUTTON, self.saveUpdate, save_button)

 #选中的id和bookid
 self.select_id = select_id
 self.bookid = self.mainframe.list.GetItem(select_id, 0).Text #获取第select_id行的第0列的值
 print(select_id, self.bookid)
 #需要用到的数据库接口
 self.dbhelper = DBHelper()
 self.showAllText()  #展现所有的text原来取值


 def showAllText(self):
 '''显示概述本原始信息'''
 data = self.dbhelper.getBookById(self.bookid)  #通过id获取书本信息

 self.name.SetValue(data[0])   #设置值
 self.author.SetValue(data[1])
 self.content.SetValue(data[2])

 def saveUpdate(self, evt):
 '''保存修改后的值'''
 bookName = self.name.GetValue()   #获得修改后的值
 author = self.author.GetValue()
 content = self.content.GetValue()

 print("书名:"+bookName)
 if bookName == "" or author == "" or content == "":
 print("进来了")
 warn = wx.MessageDialog(self, message = "所有信息不能为空!!!", caption = "错误警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示错误
 warn.Destroy()
 return
 else:
 print("开始将修改后的数据保存到数据库中")
 book = Book(bookName, author, content)  #将数据封装到book对象中
 self.dbhelper.saveUpdate(self.bookid, book)
 self.mainframe.list.SetItem(self.select_id, 1, bookName)

 self.Destroy()    #修改完后自动销毁


class ShowFrame(wx.Frame):
 '''用来显示书籍的信息'''

 def __init__(self, parent, title, select_id):
 '''初始化该小窗口的布局'''

 #便于调用父窗口
 self.mainframe = parent

 #生成一个300*300的框
 wx.Frame.__init__(self, parent, title = title, size = (400, 250))

 self.panel = wx.Panel(self, pos = (0, 0), size = (400, 250))
 self.panel.SetBackgroundColour("#FFFFFF")  #背景为白色

 #三个编辑框,分别用来编辑书名,作者,书籍相关信息
 bookName_tip = wx.StaticText(self.panel, label = "书名:", pos = (5, 8), size = (35, 25))
 bookName_tip.SetBackgroundColour("#FFFFFF")
 bookName_text = wx.TextCtrl(self.panel, pos = (40, 5), size = (340, 25))
 bookName_text.SetEditable(False)
 self.name = bookName_text

 author_tip = wx.StaticText(self.panel, label = "作者:", pos = (5, 38), size = (35, 25))
 author_tip.SetBackgroundColour("#FFFFFF")
 author_text = wx.TextCtrl(self.panel, pos = (40, 35), size = (340, 25))
 author_text.SetEditable(False)
 self.author = author_text

 content_tip = wx.StaticText(self.panel, label = "内容:", pos = (5, 68), size = (340, 25))
 content_tip.SetBackgroundColour("#FFFFFF")
 content_text = wx.TextCtrl(self.panel, pos = (40, 65), size = (340, 100), style = wx.TE_MULTILINE)
 content_text.SetEditable(False)
 self.content = content_text

 #选中的id和bookid
 self.select_id = select_id
 self.bookid = self.mainframe.list.GetItem(select_id, 0).Text #获取第select_id行的第0列的值

 #需要用到的数据库接口
 self.dbhelper = DBHelper()
 self.showAllText()  #展现所有的text原来取值

 def showAllText(self):
 '''显示概述本原始信息'''
 data = self.dbhelper.getBookById(self.bookid)  #通过id获取书本信息

 self.name.SetValue(data[0])   #设置值
 self.author.SetValue(data[1])
 self.content.SetValue(data[2])



class LibraryFrame(wx.Frame):
 def __init__(self, parent, title):
 '''初始化系统总体布局,包括各种控件'''

 #生成一个宽为400,高为400的frame框
 wx.Frame.__init__(self, parent, title=title, size=(400, 400)) 

 #定一个网格布局,两行一列
 self.main_layout = wx.BoxSizer(wx.VERTICAL)


 #生成一个列表
 self.list = wx.ListCtrl(self, -1, size = (400,300), style = wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES) #| wx.LC_SINGLE_SEL
 #列表有散列,分别是书本ID,书名,添加日期
 self.list.InsertColumn(0, "ID")
 self.list.InsertColumn(1, "书名")
 self.list.InsertColumn(2, "添加日期")
 #设置各列的宽度
 self.list.SetColumnWidth(0, 60)   #设置每一列的宽度
 self.list.SetColumnWidth(1, 230)
 self.list.SetColumnWidth(2, 92)

 #添加一组按钮,实现增删改查,用一个panel来管理该组按钮的布局
 self.panel = wx.Panel(self, pos = (0, 300), size = (400, 100))

 #定义一组按钮
 add_button = wx.Button(self.panel, label = "添加", pos = (10, 15), size = (60, 30)) #, size = (75, 30)
 del_button = wx.Button(self.panel, label = "删除", pos = (110, 15), size = (60, 30)) #, size = (75, 30)
 update_button = wx.Button(self.panel, label = "修改", pos = (210, 15), size = (60, 30)) #, size = (75, 30)
 query_button = wx.Button(self.panel, label = "查看", pos = (310, 15), size = (60, 30)) #, size = (75, 30)
 #w为按钮绑定相应事件函数,第一个参数为默认参数,指明为按钮类事件,第二个为事件函数名,第三个为按钮名
 self.Bind(wx.EVT_BUTTON, self.addBook, add_button)
 self.Bind(wx.EVT_BUTTON, self.delBook, del_button)
 self.Bind(wx.EVT_BUTTON, self.updateBook, update_button)
 self.Bind(wx.EVT_BUTTON, self.queryBook, query_button)

 #将列表和panel添加到主面板
 self.main_layout.Add(self.list, 3)
 self.main_layout.Add(self.panel, 1)

 self.SetSizer(self.main_layout)

 #添加数据库操作对象
 self.dbhelper = DBHelper()
 datas = self.dbhelper.getAllBook()

 for data in datas:
 index = self.list.InsertItem(self.list.GetItemCount(), str(data[0]))
 self.list.SetItem(index, 1, data[1])
 self.list.SetItem(index, 2, data[2])


 def addBook(self, evt):
 '''添加书籍按钮,弹出添加书籍框'''
 add_f = AddFrame(self, "添加书籍窗口")
 add_f.Show(True)


 def delBook(self, evt):
 '''删除书籍按钮,先选中,然后删除'''
 selectId = self.list.GetFirstSelected()
 if selectId == -1:
 warn = wx.MessageDialog(self, message = "未选中任何条目!!!", caption = "错误警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示错误
 warn.Destroy()
 return
 else:
 bookid = self.list.GetItem(selectId, 0).Text   #得到书本id
 self.list.DeleteItem(selectId)   #先在listctrl中删除选中行
 self.dbhelper.deleteBook(bookid)



 def updateBook(self, evt):
 '''修改按钮响应事件,点击修改按钮,弹出修改框'''
 selectId = self.list.GetFirstSelected()
 if selectId == -1:
 warn = wx.MessageDialog(self, message = "未选中任何条目!!!", caption = "错误警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示错误
 warn.Destroy()
 return
 else:
 update_f = UpdateFrame(self, "修改书籍窗口", selectId)
 update_f.Show(True)

 def queryBook(self, evt):
 '''查看按钮响应事件'''
 selectId = self.list.GetFirstSelected()
 if selectId == -1:
 warn = wx.MessageDialog(self, message = "未选中任何条目!!!", caption = "错误警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示错误
 warn.Destroy()
 return
 else:
 show_f = ShowFrame(self, "修改书籍窗口", selectId)
 show_f.Show(True)

 def addToList(self, id, book):
 index = self.list.InsertItem(self.list.GetItemCount(), str(id))
 self.list.SetItem(index, 1, book.getBookName())
 self.list.SetItem(index, 2, str(book.getAddDate()))



AppBaseClass = wx.App

class LibraryApp(AppBaseClass):
 def OnInit(self):
 frame = LibraryFrame(None, "library-system")
 frame.Show()

 return True


#类似于c中的main函数,但被其他模块导入时,__name__值不是"__main__"
if __name__ == "__main__":
 app = LibraryApp()
 app.MainLoop()

代码中有详细的注释,有不懂可以留言,需要源码的也可以点击下方原文链接获取。

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python代理抓取并验证使用多线程实现
May 03 Python
python中getaddrinfo()基本用法实例分析
Jun 28 Python
Python实现可自定义大小的截屏功能
Jan 20 Python
Python统计单词出现的次数
Apr 04 Python
pyspark 读取csv文件创建DataFrame的两种方法
Jun 07 Python
python numpy实现文件存取的示例代码
May 26 Python
python pytest进阶之fixture详解
Jun 27 Python
Django ORM多对多查询方法(自定义第三张表&ManyToManyField)
Aug 09 Python
python 中xpath爬虫实例详解
Aug 26 Python
浅谈tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意点
Jun 08 Python
python如何调用java类
Jul 05 Python
python爬取音频下载的示例代码
Oct 19 Python
人生苦短我用python python如何快速入门?
Mar 12 #Python
tensorflow实现KNN识别MNIST
Mar 12 #Python
Python操作MySQL模拟银行转账
Mar 12 #Python
python3 图片referer防盗链的实现方法
Mar 12 #Python
tensorflow构建BP神经网络的方法
Mar 12 #Python
Python管理Windows服务小脚本
Mar 12 #Python
python实现教务管理系统
Mar 12 #Python
You might like
php过滤危险html代码
2008/08/18 PHP
php INI配置文件的解析实现分析
2011/01/04 PHP
PHP中判断变量为空的几种方法小结
2013/11/12 PHP
PHP添加Xdebug扩展的方法
2014/02/12 PHP
PHP实现登陆并抓取微信列表中最新一组微信消息的方法
2017/07/10 PHP
浅谈Javascript事件模拟
2012/06/27 Javascript
js导出格式化的excel 实例方法
2013/07/17 Javascript
jquery的flexigrid无法显示数据提示获取到数据
2013/07/19 Javascript
Jquery 动态循环输出表格具体方法
2013/11/23 Javascript
js淡入淡出焦点图幻灯片效果代码分享
2015/09/08 Javascript
浅析JavaScript中的array数组类型系统
2016/07/18 Javascript
javascript中异常处理案例(推荐)
2016/10/03 Javascript
tablesorter.js表格排序使用方法(支持中文排序)
2017/02/10 Javascript
Bootstrap table 定制提示语的加载过程
2017/02/20 Javascript
详解在Vue中通过自定义指令获取dom元素
2017/03/04 Javascript
JS实现课堂随机点名和顺序点名
2017/03/09 Javascript
bootstrap日期控件问题(双日期、清空等问题解决)
2017/04/19 Javascript
微信小程序实现弹出层效果
2020/05/26 Javascript
JavaScript数据结构与算法之检索算法示例【二分查找法、计算重复次数】
2019/02/22 Javascript
浅谈Vue页面级缓存解决方案feb-alive(上)
2019/04/14 Javascript
Python标准库之sqlite3使用实例
2014/11/25 Python
在Python中使用poplib模块收取邮件的教程
2015/04/29 Python
python创建关联数组(字典)的方法
2015/05/04 Python
利用Python将文本中的中英文分离方法
2018/10/31 Python
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2020/02/26 Python
一款利用纯css3实现的超炫3D表单的实例教程
2014/12/01 HTML / CSS
伦敦最有品味的百货:Liberty London
2016/11/12 全球购物
编程实现去掉XML的重复结点
2014/05/28 面试题
编辑找工作求职信分享
2014/01/03 职场文书
医药营销个人求职信范文
2014/02/07 职场文书
公司委托书怎么写
2014/08/02 职场文书
无财产离婚协议书范本
2014/10/28 职场文书
2014年体育工作总结
2014/11/24 职场文书
2015年维修电工工作总结
2015/04/25 职场文书
2016清明节森林防火广播稿
2015/12/17 职场文书
python中的random模块和相关函数详解
2022/04/22 Python