您好,登錄后才能下訂單哦!
用wxpython實現的簡單圖書管理系統,可以實現增加圖書,刪除圖書,修改圖書,查看圖書。后臺數據庫為mysql數據庫,采用的pymysql連接數據庫。系統界面如下:
代碼如下:
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()
代碼中有詳細的注釋,有不懂可以留言,需要源碼的也可以點擊下方原文鏈接獲取。
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。