溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

wxpython實現(xiàn)圖書管理系統(tǒng)

發(fā)布時間:2020-10-18 05:24:00 來源:腳本之家 閱讀:211 作者:玩人 欄目:開發(fā)技術(shù)

用wxpython實現(xiàn)的簡單圖書管理系統(tǒng),可以實現(xiàn)增加圖書,刪除圖書,修改圖書,查看圖書。后臺數(shù)據(jù)庫為mysql數(shù)據(jù)庫,采用的pymysql連接數(shù)據(jù)庫。系統(tǒng)界面如下:

wxpython實現(xiàn)圖書管理系統(tǒng)

代碼如下:

1.書本類代碼

#author = liuwei date = 2017-06-02
from datetime import *   #導(dǎo)入日期模塊
__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.數(shù)據(jù)庫操作類代碼

#author = liuwei date = 2017-06-02
#數(shù)據(jù)庫幫助類
import pymysql
from book import *

__metaclass__ = type
class DBHelper:
 def getCon(self):
 '''獲取操作數(shù)據(jù)庫的curcor即游標(biāo),首先的建立連接,需要服務(wù)器地址,端口號,用戶名,密碼和數(shù)據(jù)庫名'''
 #為了能用中文,得加上編碼方式
 conn = pymysql.connect(host = "localhost", port = 3306, user = "root", password = "201392260", db = "library", charset = "utf8")

 return conn

 def insertBook(self, book):
 '''向數(shù)據(jù)庫中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):
 '''返回數(shù)據(jù)庫中,book表中所有的書本信息'''
 sql = "select *from book"

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

 cursor = conn.cursor()
 rownum = cursor.execute(sql) #執(zhí)行并返回找到的行數(shù)

 #獲取查詢結(jié)果
 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):
 '''根據(jù)書本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, ))  #參數(shù)以元組形式給出
 row = cursor.fetchone()  #取到第一個結(jié)果

 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):
 '''根據(jù)書本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.主界面代碼:

'''一個圖書管理系統(tǒng),能夠?qū)崿F(xiàn)增加書籍,刪除書籍,
修改書籍和查看圖書詳情,基于mysql數(shù)據(jù)庫和
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")  #背景為白色

 #三個編輯框,分別用來編輯書名,作者,書籍相關(guān)信息
 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 = "內(nèi)容:", 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)

 #需要用到的數(shù)據(jù)庫接口
 self.dbhelper = DBHelper()


 def saveBook(self, evt):
 '''第一步:獲取text中文本;第二步,連接數(shù)據(jù)庫;第三步插入并獲得主鍵;第四步添加到ListCtrl中'''
 bookName = self.name.GetValue()
 author = self.author.GetValue()
 content = self.content.GetValue()

 print("書名:"+bookName)
 if bookName == "" or author == "" or content == "":
 print("進(jìn)來了")
 warn = wx.MessageDialog(self, message = "所有信息不能為空?。?!", caption = "錯誤警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示錯誤
 warn.Destroy()
 return
 else:
 print("開始插入到數(shù)據(jù)庫中")
 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))

 #用來調(diào)用父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")  #背景為白色

 #三個編輯框,分別用來編輯書名,作者,書籍相關(guān)信息
 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 = "內(nèi)容:", 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)
 #需要用到的數(shù)據(jù)庫接口
 self.dbhelper = DBHelper()
 self.showAllText()  #展現(xiàn)所有的text原來取值


 def showAllText(self):
 '''顯示概述本原始信息'''
 data = self.dbhelper.getBookById(self.bookid)  #通過id獲取書本信息

 self.name.SetValue(data[0])   #設(shè)置值
 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("進(jìn)來了")
 warn = wx.MessageDialog(self, message = "所有信息不能為空?。。?, caption = "錯誤警告", style = wx.YES_DEFAULT | wx.ICON_ERROR)
 warn.ShowModal()    #提示錯誤
 warn.Destroy()
 return
 else:
 print("開始將修改后的數(shù)據(jù)保存到數(shù)據(jù)庫中")
 book = Book(bookName, author, content)  #將數(shù)據(jù)封裝到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):
 '''初始化該小窗口的布局'''

 #便于調(diào)用父窗口
 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")  #背景為白色

 #三個編輯框,分別用來編輯書名,作者,書籍相關(guān)信息
 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 = "內(nèi)容:", 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列的值

 #需要用到的數(shù)據(jù)庫接口
 self.dbhelper = DBHelper()
 self.showAllText()  #展現(xiàn)所有的text原來取值

 def showAllText(self):
 '''顯示概述本原始信息'''
 data = self.dbhelper.getBookById(self.bookid)  #通過id獲取書本信息

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



class LibraryFrame(wx.Frame):
 def __init__(self, parent, title):
 '''初始化系統(tǒng)總體布局,包括各種控件'''

 #生成一個寬為400,高為400的frame框
 wx.Frame.__init__(self, parent, title=title, size=(400, 400)) 

 #定一個網(wǎng)格布局,兩行一列
 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, "添加日期")
 #設(shè)置各列的寬度
 self.list.SetColumnWidth(0, 60)   #設(shè)置每一列的寬度
 self.list.SetColumnWidth(1, 230)
 self.list.SetColumnWidth(2, 92)

 #添加一組按鈕,實現(xiàn)增刪改查,用一個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為按鈕綁定相應(yīng)事件函數(shù),第一個參數(shù)為默認(rèn)參數(shù),指明為按鈕類事件,第二個為事件函數(shù)名,第三個為按鈕名
 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)

 #添加數(shù)據(jù)庫操作對象
 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):
 '''修改按鈕響應(yīng)事件,點擊修改按鈕,彈出修改框'''
 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):
 '''查看按鈕響應(yīng)事件'''
 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函數(shù),但被其他模塊導(dǎo)入時,__name__值不是"__main__"
if __name__ == "__main__":
 app = LibraryApp()
 app.MainLoop()

代碼中有詳細(xì)的注釋,有不懂可以留言,需要源碼的也可以點擊下方原文鏈接獲取。

更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI