python图书管理系统

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from tkinter import *
from tkinter import scrolledtext
import numpy as np
import csv
import os
import tkinter as tk
from tkinter import ttk
from tkinter.constants import CENTER, SINGLE, BOTH, N, HORIZONTAL, VERTICAL
from tkinter import messagebox as msgbox
from tkinter import Toplevel, Label, Entry, StringVar, Radiobutton, Button

def Close():
reply = msgbox.askyesno('提示', "您确定退出系统吗?")
if reply == True :
root.destroy()


def csv_reader(filename):
datas = []
if os.path.getsize(filename):
with open(filename, 'r', encoding='utf-8-sig', errors='ignore') as csvfile :
reader = csv.reader(csvfile)
datas = [row for row in reader]
return datas


def insert(table, book_info) :
# 插入数据
for index, data in enumerate(book_info) :
table.insert('', END, values=data) # 添加数据到末尾


def IsEmpty(text) :
if text is not None :
return 0
else :
return 1


def ShowBookInfo() :
return


# 修改书籍信息函数
def UpdateBookInfo(tree):
# 获取选中的行
selected_items = tree.selection()
if not selected_items:
msgbox.showerror(title='提示', message="请先选择一行要修改的数据!")
return

# 获取选中行的图书编号
book_id = tree.item(selected_items[0])['values'][0]

# 读取图书数据文件中的数据
book_info = csv_reader('图书.csv')

if book_info is not None:
# 根据图书编号获取对应的图书信息

# 创建修改图书信息的窗口
UBIGUI = Toplevel()
UBIGUI.title('修改书籍信息')
UBIGUI.geometry('400x400')

# 定义确认修改的函数
def click():
# 获取用户输入的图书信息
num = addnuminput.get()
title = addtitleinput.get()
author = addauthorinput.get()
publisher = addpublisherinput.get()
category = v.get()

# 判断用户是否输入了所有信息
if IsEmpty(num) or IsEmpty(title) or IsEmpty(author) or IsEmpty(publisher) or IsEmpty(category):
msgbox.showerror(title='提示', message="请填写所有信息!")
else:
# 更新图书数据文件中的信息
book_info_new = [row if row[0] != book_id else [num, title, author, publisher, category] for row in book_info]
with open('图书.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(book_info_new)

# 更新表格中的信息
tree.delete(selected_items[0])
insert(tree, [[num, title, author, publisher, category]])

# 提示修改成功
reply = msgbox.showinfo(title='提示', message="修改成功!")
if reply:
UBIGUI.destroy()

# 创建标签和输入框,用于输入图书信息
addnum = Label(UBIGUI, text="序号", font=("Microsoft YaHei", 10), anchor='w')
addnum.place(x=30, y=30, height=20, width=80)
addnuminput = Entry(UBIGUI, font=("Microsoft YaHei", 10))
addnuminput.place(x=100, y=30, height=24, width=150)

addtitle = Label(UBIGUI, text="书名", font=("Microsoft YaHei", 10), anchor='w')
addtitle.place(x=30, y=60, height=20, width=80)
addtitleinput = Entry(UBIGUI, font=("Microsoft YaHei", 10))
addtitleinput.place(x=100, y=60, height=24, width=150)

addauthor = Label(UBIGUI, text="作者", font=("Microsoft YaHei", 10), anchor='w')
addauthor.place(x=30, y=90, height=20, width=80)
addauthorinput = Entry(UBIGUI, font=("Microsoft YaHei", 10))
addauthorinput.place(x=100, y=90, height=24, width=150)

addpublisher = Label(UBIGUI, text="出版社", font=("Microsoft YaHei", 10), anchor='w')
addpublisher.place(x=30, y=120, height=20, width=80)
addpublisherinput = Entry(UBIGUI, font=("Microsoft YaHei", 10))
addpublisherinput.place(x=100, y=120, height=24, width=150)

addcategory = Label(UBIGUI, text="类别", font=("Microsoft YaHei", 10), anchor='w')
addcategory.place(x=30, y=150, height=20, width=80)

# 创建单选按钮,用于选择图书类别
v = StringVar()
w1 = Radiobutton(UBIGUI, text="专业书", value='专业书', variable=v)
w2 = Radiobutton(UBIGUI, text="工具书", value='工具书', variable=v)
w3 = Radiobutton(UBIGUI, text="报告", value='报告', variable=v)
w4 = Radiobutton(UBIGUI, text="小说", value='小说', variable=v)
w5 = Radiobutton(UBIGUI, text="其它", value='其它', variable=v)
w1.place(x=100, y=150, height=20, width=60)
w2.place(x=160, y=150, height=20, width=60)
w3.place(x=220, y=150, height=20, width=60)
w4.place(x=100, y=180, height=20, width=60)
w5.place(x=160, y=180, height=20, width=60)

# 创建确认按钮,用于提交修改
btn = Button(UBIGUI, text="确认修改", font=("Microsoft YaHei", 10), command=click)
btn.place(x=200, y=210, height=30, width=80)

# 运行窗口
UBIGUI.mainloop()


# 修改书籍信息按钮的回调函数
def UpdateBookInfoButton(tree):
# 获取选中的行
selected_items = tree.selection()
if not selected_items:
msgbox.showerror(title='提示', message="请先选择一行要修改的数据!")
return

# 调用修改书籍信息的函数
UpdateBookInfo(tree)



def FindBook():
query = search_entry.get().strip()
if not query:
msgbox.showinfo(title='提示', message='请输入查询内容!')
return

book_info = csv_reader('图书.csv')
if not book_info:
msgbox.showinfo(title='提示', message='图书信息为空!')
return

search_result = []
for row in book_info:
if query in row[0] or query in row[1] or query in row[2] or query in row[3] or query in row[4]:
search_result.append(row)

if not search_result:
msgbox.showinfo(title='提示', message='未找到相关图书信息!')
else:
for i in tree.get_children():
tree.delete(i)
insert(tree, search_result)
msgbox.showinfo(title='提示', message=f'共找到 {len(search_result)} 条相关图书信息!')

def DeleteBook():
selected_items = tree.selection()
if not selected_items:
msgbox.showerror(title='提示', message="请先选择一行要删除的数据!")
return

for item in selected_items:
book_id = tree.item(item)['values'][0]
book_info = csv_reader('图书.csv')

if book_info is not None:
book_info = [row for row in book_info if row[0] != book_id]

with open('图书.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(book_info)

tree.delete(item)

msgbox.showinfo(title='提示', message="删除成功!")

def CreateBookInfo():
CBIGUI = Toplevel()
CBIGUI.title('录入书籍信息')
CBIGUI.geometry('300x300')

def click():
num = addnuminput.get()
title = addtitleinput.get()
author = addauthorinput.get()
publisher = addpublisherinput.get()
category = v.get() # 获取单选框选中的值
newbook_info = []

if IsEmpty(num) or IsEmpty(title) or IsEmpty(author) or IsEmpty(publisher) or IsEmpty(category):
msgbox.showerror(title='提示', message="请填写所有信息!")
else:
info = csv_reader('图书.csv')
if info is not None:
info = np.array(info)
if num in info[:, 0]:
msgbox.showinfo(title='错误', message="序号已存在!")
CBIGUI.destroy()
return

with open('图书.csv', 'a+', encoding='utf-8-sig') as csvfile:
csvfile.write('{},{},{},{},{}\n'.format(num, title, author, publisher, category))
newbook_info.append([num, title, author, publisher, category])
insert(tree, newbook_info)

reply = msgbox.showinfo(title='提示', message="写入成功!")
if reply:
CBIGUI.destroy()

addnum = Label(CBIGUI, text="序号", font=("Microsoft YaHei", 10), anchor='w')
addnum.place(x=30, y=30, height=20, width=80)
addnuminput = Entry(CBIGUI, font=("Microsoft YaHei", 10))
addnuminput.place(x=100, y=30, height=24, width=150)

addtitle = Label(CBIGUI, text="书名", font=("Microsoft YaHei", 10), anchor='w')
addtitle.place(x=30, y=60, height=20, width=80)
addtitleinput = Entry(CBIGUI, font=("Microsoft YaHei", 10))
addtitleinput.place(x=100, y=60, height=24, width=150)

addauthor = Label(CBIGUI, text="作者", font=("Microsoft YaHei", 10), anchor='w')
addauthor.place(x=30, y=90, height=20, width=80)
addauthorinput = Entry(CBIGUI, font=("Microsoft YaHei", 10))
addauthorinput.place(x=100, y=90, height=24, width=150)

addpublisher = Label(CBIGUI, text="出版社", font=("Microsoft YaHei", 10), anchor='w')
addpublisher.place(x=30, y=120, height=20, width=80)
addpublisherinput = Entry(CBIGUI, font=("Microsoft YaHei", 10))
addpublisherinput.place(x=100, y=120, height=24, width=150)

addcategory = Label(CBIGUI, text="类别", font=("Microsoft YaHei", 10), anchor='w')
addcategory.place(x=30, y=150, height=20, width=80)

# 使用单选框代替输入框
v = StringVar()
v.set('其它')
w1 = Radiobutton(CBIGUI, text="专业书", value='专业书', variable=v)
w2 = Radiobutton(CBIGUI, text="工具书", value='工具书', variable=v)
w3 = Radiobutton(CBIGUI, text="报告", value='报告', variable=v)
w4 = Radiobutton(CBIGUI, text="小说", value='小说', variable=v)
w5 = Radiobutton(CBIGUI, text="其它", value='其它', variable=v)
w1.place(x=100, y=150, height=20, width=60)
w2.place(x=100, y=170, height=20, width=60)
w3.place(x=95, y=190, height=20, width=60)
w4.place(x=95, y=210, height=20, width=60)
w5.place(x=95, y=230, height=20, width=60)

Buttonadd = Button(CBIGUI, command=click, text="添加", font=("Microsoft YaHei", 10))
Buttonadd.place(x=120, y=260, height=30, width=70)

if __name__ == '__main__':
# 主窗口
root = tk.Tk()
root.title("book_manage_system")
root.geometry("1000x500+100+100")
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="菜单", menu=filemenu)
filemenu.add_command(label="打开")
filemenu.add_command(label="保存")
filemenu.add_command(label="退出", command=root.destroy)
root.config(menu=menubar)
pw = tk.PanedWindow(root, orient="vertical", sashrelief="sunken")
pw.pack(fill="both", expand=1)
pw1 = tk.PanedWindow(pw, orient="horizontal", sashrelief="sunken")
pw2 = tk.PanedWindow(pw, orient="horizontal", sashrelief="sunken")
top_frame, left_frame, right_frame = ttk.Frame(pw1, height=100, relief="flat"), ttk.Frame(pw2, relief="flat", borderwidth=0), ttk.Frame(
pw2, relief="ridge")
pw.add(pw1), pw.add(pw2), pw1.add(top_frame), pw2.add(left_frame),
pw2.add(right_frame)

# 填充文本&输入框
ttk.Label(top_frame, text="图书管理系统", font=("Microsoft YaHei", 25)).pack(pady=30)
search_label = ttk.Label(top_frame, text="搜索", font=("Microsoft YaHei", 10))
search_label.pack(side="right", padx=10)
search_entry = ttk.Entry(top_frame, font=("Microsoft YaHei", 10), width=20)
search_entry.pack(side="right", padx=10)
button1 = ttk.Button(left_frame, text="查询图书", width=12, command=FindBook).pack(padx=15, pady=40)
button2 = ttk.Button(left_frame, text="增加图书", width=12, command=CreateBookInfo).pack(padx=15)
btn_update = ttk.Button(left_frame, text="修改图书", width=12, command=lambda: UpdateBookInfo(tree))
btn_update.pack(padx=15, pady=40)
button4 = ttk.Button(left_frame, text="删除图书", width=12, command=DeleteBook).pack(padx=15)
labelframe = tk.LabelFrame(right_frame, text="图书信息",
font=("Microsoft YaHei Light", 16), labelanchor=N)
# 列表框&滚动条
labelframe = tk.LabelFrame(right_frame, text="图书信息", font=("Microsoft YaHei Light", 16), labelanchor=N)
listbox = tk.Listbox(labelframe, font=("黑体", 10), selectmode=SINGLE)
listbox.select_set(0)
xscroll = ttk.Scrollbar(listbox, orient=HORIZONTAL, command=listbox.xview)
yscroll = ttk.Scrollbar(listbox, orient=VERTICAL, command=listbox.yview)
# 链接滚动条&列表框
labelframe.pack(expand=0), listbox.pack(side="left", pady=2)
columns = ("图书编号", "图书名称", "图书作者", "出版社", "图书类别")
tree = ttk.Treeview(
master=listbox, # 父容器
height=10, # 表格显示的行数,height 行
columns=columns, # 显示的列
show='headings', # 隐藏首列
xscrollcommand=xscroll.set, # x 轴滚动条
yscrollcommand=yscroll.set, # y 轴滚动条
)
xscroll.config(command=listbox.xview)
xscroll.pack(side="bottom", fill="x", pady=2)
yscroll.config(command=listbox.yview)
yscroll.pack(side="right", fill="y", padx=2)
for column in columns :
tree.heading(column=column, text=column, anchor=CENTER) # 定义表头
tree.column(column=column, width=130, anchor=CENTER) # 定义列
tree.pack(fill=BOTH, expand=True)
book_info = csv_reader('图书.csv')
insert(tree, book_info)
root.mainloop()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值