With wxpython simple library management system , you can add books , delete books , modify books , view books . The background database for mysql database, using the pymysql connection database. The system interface is as follows:
The code is as follows:
1. Book class code
#author = liuwei date = 2017-06-02 from datetime import * # Import date module __metaclass__ = type class Book: '''A book information class with book name, author name and book simple information''' def __init__(self, bookName = "", author = "", content = ""): = bookName #Book Names = author # Author's name = content #Book Info self.add_date = () #Books Add Date def setBookName(self, name): = name def getBookName(self): return def setAuthor(self, author): = author def getAuthor(self): return def setContent(self, content): = content def getContent(self): return def getAddDate(self): return self.add_date if __name__ == "__main__": mybook = Book() print()
2. Database operation class code
#author = liuwei date = 2017-06-02 # Database Help Classes import pymysql from book import * __metaclass__ = type class DBHelper: def getCon(self): '''Get the curcor, i.e. the cursor, to operate the database, first of all, establish a connection, you need the server address, port number, username, password and database name.''' #To be able to use Chinese, you have to add the encoding method conn = (host = "localhost", port = 3306, user = "root", password = "201392260", db = "library", charset = "utf8") return conn def insertBook(self, book): '''Insert book information into the book table in the database, book is an object of class Book, containing basic information about the book''' sql = "insert into book(name, author, content, add_date) values(%s, %s, %s, %s)" conn = (); if conn ==None: return cursor = () (sql, ((), (), (), ())) () () () new_id = print("Newly inserted key value id is:", new_id) return new_id def getAllBook(self): '''return database, book table all the book information''' sql = "select *from book" conn = () if conn == None: return cursor = () rownum = (sql) # Execute and return the number of lines found #Get Query Results rows = () list = [] for item in rows: bitem = (item[0], item[1], str(item[4])) (bitem) () () () return list def getBookById(self, bookid): '''Find book information based on book id value''' sql = "select , , from book where id=%s" conn = () if conn == None: return cursor = () (sql, (bookid, )) # Parameters are given as tuples row = () # Fetch the first result () () () return row # Return information about the book def saveUpdate(self, bookid, book): '''Use book object to modify book information with id bookid''' sql = "update book set =%s, =%s, =%s where =%s" conn = () if conn == None: return cursor = () (sql, ((), (), (), bookid)) () () () def deleteBook(self, bookid): '''Delete books based on book id''' sql = "delete from book where = %s" conn = () if conn == None: return cursor = () (sql, (bookid, )) () () () if __name__ == '__main__': db = DBHelper() #book = Book("Qinqiang", "Jia Auping", "Tells about the Xia and Bai families in the Great Northwest, as dictated by Citation.") #(book) list = () for item in list: print(item)
3. Main interface code.
'''A library management system that enables adding books, deleting books, and modify books and view book details, based on mysql database and wxPython''' import wx from book import * from dbhelper import * __metaclass__ = type class AddFrame(): '''Add book popups''' def __init__(self, parent, title): '''Initialize the layout of this small window''' = parent # Generate a 300*300 box .__init__(self, parent, title = title, size = (400, 250)) = (self, pos = (0, 0), size = (400, 250)) ("#FFFFFF") #Background is white # Three edit boxes for book title, author, and book-related information bookName_tip = (, label = "Book Title:", pos = (5, 8), size = (35, 25)) bookName_tip.SetBackgroundColour("#FFFFFF") bookName_text = (, pos = (40, 5), size = (340, 25)) = bookName_text author_tip = (, label = "Author:", pos = (5, 38), size = (35, 25)) author_tip.SetBackgroundColour("#FFFFFF") author_text = (, pos = (40, 35), size = (340, 25)) = author_text content_tip = (, label = "Content:", pos = (5, 68), size = (340, 25)) content_tip.SetBackgroundColour("#FFFFFF") content_text = (, pos = (40, 65), size = (340, 100), style = wx.TE_MULTILINE) = content_text save_button = (, label = "Save the books.", pos = (160, 170)) (wx.EVT_BUTTON, , save_button) # Database interfaces to be used = DBHelper() def saveBook(self, evt): '''Step 1: get the text in text; Step 2, connect to the database; Step 3 insert and get the primary key; Step 4 add to ListCtrl''' bookName = () author = () content = () print("Book Title:"+bookName) if bookName == "" or author == "" or content == "": print("Coming in.") warn = (self, message = "All information cannot be null!!!", caption = "Error warning.", style = wx.YES_DEFAULT | wx.ICON_ERROR) () #Tip Error () return else: print("Begin insertion into the database.") book = Book(bookName, author, content) book_id = (book) (book_id, book) () class UpdateFrame(): def __init__(self, parent, title, select_id): '''Initialize the general layout of the update book information screen''' (parent, title = title, size = (400, 250)) # Used to call the parent frame for easy updating. = parent # Generate a 300*300 box .__init__(self, parent, title = title, size = (400, 250)) = (self, pos = (0, 0), size = (400, 250)) ("#FFFFFF") #Background is white # Three edit boxes for book title, author, and book-related information bookName_tip = (, label = "Book Title:", pos = (5, 8), size = (35, 25)) bookName_tip.SetBackgroundColour("#FFFFFF") bookName_text = (, pos = (40, 5), size = (340, 25)) = bookName_text author_tip = (, label = "Author:", pos = (5, 38), size = (35, 25)) author_tip.SetBackgroundColour("#FFFFFF") author_text = (, pos = (40, 35), size = (340, 25)) = author_text content_tip = (, label = "Content:", pos = (5, 68), size = (340, 25)) content_tip.SetBackgroundColour("#FFFFFF") content_text = (, pos = (40, 65), size = (340, 100), style = wx.TE_MULTILINE) = content_text save_button = (, label = "Save changes", pos = (160, 170)) (wx.EVT_BUTTON, , save_button) #Selected ids and bookids self.select_id = select_id = (select_id, 0).Text # Get the value of column 0 of the select_id row. print(select_id, ) # Database interfaces to be used = DBHelper() () # Show all the original values of text def showAllText(self): '''Show summary of this original message''' data = () #Get book info by id (data[0]) #Set values (data[1]) (data[2]) def saveUpdate(self, evt): '''Save the modified value''' bookName = () # Get the modified value author = () content = () print("Book Title:"+bookName) if bookName == "" or author == "" or content == "": print("Coming in.") warn = (self, message = "All information cannot be null!!!", caption = "Error warning.", style = wx.YES_DEFAULT | wx.ICON_ERROR) () #Tip Error () return else: print("Begin saving modified data to the database.") book = Book(bookName, author, content) # Encapsulate data into a book object (, book) (self.select_id, 1, bookName) () # Automatically destroyed after modification class ShowFrame(): '''Used to display information about books''' def __init__(self, parent, title, select_id): '''Initialize the layout of this small window''' #Easy to call the parent window = parent # Generate a 300*300 box .__init__(self, parent, title = title, size = (400, 250)) = (self, pos = (0, 0), size = (400, 250)) ("#FFFFFF") #Background is white # Three edit boxes for book title, author, and book-related information bookName_tip = (, label = "Book Title:", pos = (5, 8), size = (35, 25)) bookName_tip.SetBackgroundColour("#FFFFFF") bookName_text = (, pos = (40, 5), size = (340, 25)) bookName_text.SetEditable(False) = bookName_text author_tip = (, label = "Author:", pos = (5, 38), size = (35, 25)) author_tip.SetBackgroundColour("#FFFFFF") author_text = (, pos = (40, 35), size = (340, 25)) author_text.SetEditable(False) = author_text content_tip = (, label = "Content:", pos = (5, 68), size = (340, 25)) content_tip.SetBackgroundColour("#FFFFFF") content_text = (, pos = (40, 65), size = (340, 100), style = wx.TE_MULTILINE) content_text.SetEditable(False) = content_text #Selected ids and bookids self.select_id = select_id = (select_id, 0).Text # Get the value of column 0 of the select_id row. # Database interfaces to be used = DBHelper() () # Show all the original values of text def showAllText(self): '''Show summary of this original message''' data = () #Get book info by id (data[0]) #Set values (data[1]) (data[2]) class LibraryFrame(): def __init__(self, parent, title): '''Initialize the general layout of the system, including the various controls''' # Generate a frame box with a width of 400 and a height of 400 .__init__(self, parent, title=title, size=(400, 400)) # Set up a grid layout with two rows and one column. self.main_layout = () # Generate a list = (self, -1, size = (400,300), style = wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES) #| wx.LC_SINGLE_SEL The #list has hashes for book ID, book title, and date added. (0, "ID") (1, "Title of the book.") (2, "Add date") # Set the width of each column (0, 60) # Set the width of each column (1, 230) (2, 92) # Add a set of buttons to realize the add, delete, change and check, with a panel to manage the layout of the group of buttons = (self, pos = (0, 300), size = (400, 100)) # Define a set of buttons add_button = (, label = "Add", pos = (10, 15), size = (60, 30)) #, size = (75, 30) del_button = (, label = "Delete", pos = (110, 15), size = (60, 30)) #, size = (75, 30) update_button = (, label = "Modification", pos = (210, 15), size = (60, 30)) #, size = (75, 30) query_button = (, label = "View.", pos = (310, 15), size = (60, 30)) #, size = (75, 30) #w for the button to bind the corresponding event function, the first parameter is the default parameter, specified as the button class event, the second is the event function name, the third is the button name (wx.EVT_BUTTON, , add_button) (wx.EVT_BUTTON, , del_button) (wx.EVT_BUTTON, , update_button) (wx.EVT_BUTTON, , query_button) # Add lists and panels to the main panel self.main_layout.Add(, 3) self.main_layout.Add(, 1) (self.main_layout) # Add database operation objects = DBHelper() datas = () for data in datas: index = ((), str(data[0])) (index, 1, data[1]) (index, 2, data[2]) def addBook(self, evt): '''Add book button, popup add book box''' add_f = AddFrame(self, "Add Books Window") add_f.Show(True) def delBook(self, evt): '''Delete books button, first select, then delete''' selectId = () if selectId == -1: warn = (self, message = "No entries selected!!!", caption = "Error warning.", style = wx.YES_DEFAULT | wx.ICON_ERROR) () #Tip Error () return else: bookid = (selectId, 0).Text # Get the book id (selectId) # First delete the selected row in listctrl (bookid) def updateBook(self, evt): '''Modify button response event, click modify button, popup modify box''' selectId = () if selectId == -1: warn = (self, message = "No entries selected!!!", caption = "Error warning.", style = wx.YES_DEFAULT | wx.ICON_ERROR) () #Tip Error () return else: update_f = UpdateFrame(self, "Modify Book Window", selectId) update_f.Show(True) def queryBook(self, evt): '''View button response events''' selectId = () if selectId == -1: warn = (self, message = "No entries selected!!!", caption = "Error warning.", style = wx.YES_DEFAULT | wx.ICON_ERROR) () #Tip Error () return else: show_f = ShowFrame(self, "Modify Book Window", selectId) show_f.Show(True) def addToList(self, id, book): index = ((), str(id)) (index, 1, ()) (index, 2, str(())) AppBaseClass = class LibraryApp(AppBaseClass): def OnInit(self): frame = LibraryFrame(None, "library-system") () return True # Similar to the main function in c, but the __name__ value is not "__main__" when imported by other modules. if __name__ == "__main__": app = LibraryApp() ()
The code has detailed comments, you can leave a message if you do not understand, you need the source code can also click on the original link below to get.
More study materials are available on the topic ofManagement system development》。
This is the whole content of this article.