用python制作图书管理系统,包含前端用户界面和后端管理员界面的代码
时间: 2024-05-01 08:21:28 浏览: 181
这是一个基于Python和Tkinter GUI库的简单图书管理系统示例代码。它包括一个前端用户界面和一个后端管理员界面,可以实现添加、删除和搜索图书信息。
前端用户界面代码:
```python
from tkinter import *
import tkinter.messagebox as messagebox
import sqlite3
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("图书管理系统")
self.master.geometry('400x300')
self.create_widgets()
def create_widgets(self):
self.lblTitle = Label(self.master, text='欢迎使用图书管理系统', font=('Arial', 16))
self.lblTitle.pack(pady=20)
self.lblBookname = Label(self.master, text='书名:')
self.lblBookname.pack()
self.entBookname = Entry(self.master)
self.entBookname.pack()
self.btnAdd = Button(self.master, text='添加', command=self.add_book)
self.btnAdd.pack(pady=10)
self.lblSearch = Label(self.master, text='搜索:')
self.lblSearch.pack()
self.entSearch = Entry(self.master)
self.entSearch.pack()
self.btnSearch = Button(self.master, text='搜索', command=self.search_book)
self.btnSearch.pack(pady=10)
self.lblResult = Label(self.master, text='')
self.lblResult.pack()
def add_book(self):
bookname = self.entBookname.get()
if bookname != '':
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute("INSERT INTO books (bookname) VALUES (?)", (bookname,))
conn.commit()
conn.close()
messagebox.showinfo('提示', '添加成功')
self.entBookname.delete(0, END)
else:
messagebox.showwarning('错误', '书名不能为空')
def search_book(self):
keyword = self.entSearch.get()
if keyword != '':
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute("SELECT * FROM books WHERE bookname LIKE ?", ('%' + keyword + '%',))
books = c.fetchall()
conn.close()
if books:
result = '搜索结果:\n'
for book in books:
result += book[1] + '\n'
self.lblResult.config(text=result)
else:
messagebox.showinfo('提示', '没有找到相关书籍')
else:
messagebox.showwarning('错误', '搜索关键字不能为空')
if __name__ == '__main__':
root = Tk()
app = Application(master=root)
app.mainloop()
```
后端管理员界面代码:
```python
from tkinter import *
import tkinter.messagebox as messagebox
import sqlite3
class AdminApplication(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("图书管理系统 - 管理员界面")
self.master.geometry('400x300')
self.create_widgets()
def create_widgets(self):
self.lblTitle = Label(self.master, text='欢迎使用图书管理系统 - 管理员界面', font=('Arial', 16))
self.lblTitle.pack(pady=20)
self.lblBookname = Label(self.master, text='书名:')
self.lblBookname.pack()
self.entBookname = Entry(self.master)
self.entBookname.pack()
self.btnAdd = Button(self.master, text='添加', command=self.add_book)
self.btnAdd.pack(pady=10)
self.lblSearch = Label(self.master, text='搜索:')
self.lblSearch.pack()
self.entSearch = Entry(self.master)
self.entSearch.pack()
self.btnSearch = Button(self.master, text='搜索', command=self.search_book)
self.btnSearch.pack(pady=10)
self.lblResult = Label(self.master, text='')
self.lblResult.pack()
self.btnView = Button(self.master, text='查看所有书籍', command=self.view_books)
self.btnView.pack(pady=10)
self.lblBooks = Label(self.master, text='全部书籍:')
self.lblBooks.pack()
self.lstBooks = Listbox(self.master)
self.lstBooks.pack()
self.btnDelete = Button(self.master, text='删除', command=self.delete_book)
self.btnDelete.pack(pady=10)
self.view_books()
def add_book(self):
bookname = self.entBookname.get()
if bookname != '':
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute("INSERT INTO books (bookname) VALUES (?)", (bookname,))
conn.commit()
conn.close()
messagebox.showinfo('提示', '添加成功')
self.entBookname.delete(0, END)
self.view_books()
else:
messagebox.showwarning('错误', '书名不能为空')
def search_book(self):
keyword = self.entSearch.get()
if keyword != '':
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute("SELECT * FROM books WHERE bookname LIKE ?", ('%' + keyword + '%',))
books = c.fetchall()
conn.close()
if books:
result = '搜索结果:\n'
for book in books:
result += book[1] + '\n'
self.lblResult.config(text=result)
else:
messagebox.showinfo('提示', '没有找到相关书籍')
else:
messagebox.showwarning('错误', '搜索关键字不能为空')
def view_books(self):
self.lstBooks.delete(0, END)
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute("SELECT * FROM books")
books = c.fetchall()
conn.close()
for book in books:
self.lstBooks.insert(END, book[1])
def delete_book(self):
index = self.lstBooks.curselection()
if index != ():
bookname = self.lstBooks.get(index)
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute("DELETE FROM books WHERE bookname=?", (bookname,))
conn.commit()
conn.close()
messagebox.showinfo('提示', '删除成功')
self.view_books()
else:
messagebox.showwarning('错误', '请选择要删除的书籍')
if __name__ == '__main__':
root = Tk()
app = AdminApplication(master=root)
app.mainloop()
```
注意:这段代码中需要连接一个名为“book.db”的 SQLite 数据库来存储图书信息。可以使用以下代码创建:
```python
import sqlite3
conn = sqlite3.connect('book.db')
c = conn.cursor()
c.execute('''CREATE TABLE books
(id INTEGER PRIMARY KEY AUTOINCREMENT,
bookname TEXT)''')
conn.commit()
conn.close()
```
阅读全文