Python实现图书管理系统设计

基于python的tkinter,将图书管理系统的界面进行简单的开发,我在这里使用的方法比较简单理解,但是代码过于繁多。添加、删除及修改并未使用数据库,而是使用了csv文件的读取操作,从而替代数据库。

基本效果如下图所示:

该系统将所有代码全都放在一个文件中,源码如下:

import os
import tkinter
import tkinter.messagebox
from tkinter import *
from tkinter import ttk
 
class LoginPage (object):
    def __init__(self, master=None): 
        self.root = master #定义内部变量root 
        # 获取当前屏幕的宽高
        self.width = self.root.winfo_screenwidth()
        self.height = self.root.winfo_screenheight()
        # 设置窗口大小
        self.h = 600
        self.w = 800
        # 将窗口居中
        self.y = (self.height - 600) / 2
        self.x = (self.width - 800) / 2
        self.root.geometry("%dx%d+%d+%d" %(self.w,self.h,self.x,self.y))
        # 不允许修改窗口大小
        self.root.resizable(False,False)
        self.addnum = StringVar() 
        self.addname = StringVar() 
        self.addauthor = StringVar() 
        self.addchu = StringVar() 
        self.adddate = StringVar() 
        self.addpri = StringVar() 
        self.altnum = StringVar() 
        self.altname = StringVar() 
        self.altauthor = StringVar() 
        self.altchu = StringVar() 
        self.altdate = StringVar() 
        self.altpri = StringVar() 
        self.createPage() 
 
    # 创建页面
    def createPage(self): 
        #表格
        tree = ttk.Treeview(self.root)
        tree.place(x=10,y=10,width=780,height=260)
        # #定义列
        tree["columns"] = ("编号","书名","作者", "出版社", "出版日期", "价格")
        tree['show'] = 'headings'
        # 设置列,列还不显示
        tree.column("编号", width=80,anchor ='c')
        tree.column("书名", width=150,anchor ='c')
        tree.column("作者", width=150,anchor ='c')
        tree.column("出版社", width=150,anchor ='c')
        tree.column("出版日期", width=150,anchor ='c')
        tree.column("价格", width=100,anchor ='c')
        #设置表头
        tree.heading("编号", text="编号")
        tree.heading("书名", text="书名")
        tree.heading("作者", text="作者")
        tree.heading("出版社", text="出版社")
        tree.heading("出版日期", text="出版日期")
        tree.heading("价格", text="价格")
        #添加数据
        f = open('图书.csv','r',encoding='utf-8')
        for line in f.readlines():
            info = line[:-1].split(",")
            tree.insert("", 0, values=(info[0],info[1],info[2],info[3],info[4],info[5]))
        f.close()
 
        # 添加编号
        addnum = Label(self.root, text="编号",font=('微软雅黑',10,''),anchor='w')
        addnum.place(x=42.5,y=280,height=20,width=80)
        addnuminput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addnum)
        addnuminput.place(x=122.5,y=279,height=24,width=105)
        # 添加书名
        addname = Label(self.root, text="书名",font=('微软雅黑',10,''),anchor='w')
        addname.place(x=42.5,y=310,height=20,width=80)
        addnameinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addname)
        addnameinput.place(x=122.5,y=309,height=24,width=105)
        # 添加作者
        addauthor = Label(self.root, text="作者",font=('微软雅黑',10,''),anchor='w')
        addauthor.place(x=42.5,y=340,height=20,width=80)
        addauthorinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addauthor)
        addauthorinput.place(x=122.5,y=339,height=24,width=105)
        # 添加出版社
        addchu = Label(self.root, text="出版社",font=('微软雅黑',10,''),anchor='w')
        addchu.place(x=42.5,y=370,height=20,width=80)
        addchuinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addchu)
        addchuinput.place(x=122.5,y=369,height=24,width=105)
        # 添加出版日期
        adddate = Label(self.root, text="出版日期",font=('微软雅黑',10,''),anchor='w')
        adddate.place(x=42.5,y=400,height=20,width=80)
        adddateinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.adddate)
        adddateinput.place(x=122.5,y=399,height=24,width=105)
        # 添加价格
        addpri = Label(self.root, text="价格",font=('微软雅黑',10,''),anchor='w')
        addpri.place(x=42.5,y=430,height=20,width=80)
        addpriinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.addpri)
        addpriinput.place(x=122.5,y=429,height=24,width=105)
        # 添加按钮
        add = Button(self.root,command=self.click, text ="添加书本",font=('微软雅黑',10,''),activeforeground='#ffffff',fg='#ffffff',activebackground='#7cba59',bd=2,bg='#2aa515')
        add.place(x=105,y=500,height=35,width=100)
 
 
        # 修改编号
        altnum = Label(self.root, text="编号",font=('微软雅黑',10,''),anchor='w')
        altnum.place(x=292.5,y=280,height=20,width=80)
        altnuminput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altnum)
        altnuminput.place(x=372.5,y=279,height=24,width=105)
        # 修改书名
        altname = Label(self.root, text="书名",font=('微软雅黑',10,''),anchor='w')
        altname.place(x=292.5,y=310,height=20,width=80)
        altnameinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altname)
        altnameinput.place(x=372.5,y=309,height=24,width=105)
        # 修改作者
        altauthor = Label(self.root, text="作者",font=('微软雅黑',10,''),anchor='w')
        altauthor.place(x=292.5,y=340,height=20,width=80)
        altauthorinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altauthor)
        altauthorinput.place(x=372.5,y=339,height=24,width=105)
        # 修改出版社
        altchu = Label(self.root, text="出版社",font=('微软雅黑',10,''),anchor='w')
        altchu.place(x=292.5,y=370,height=20,width=80)
        altchuinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altchu)
        altchuinput.place(x=372.5,y=369,height=24,width=105)
        # 修改出版日期
        altdate = Label(self.root, text="出版日期",font=('微软雅黑',10,''),anchor='w')
        altdate.place(x=292.5,y=400,height=20,width=80)
        altdateinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altdate)
        altdateinput.place(x=372.5,y=399,height=24,width=105)
        # 修改价格
        altpri = Label(self.root, text="价格",font=('微软雅黑',10,''),anchor='w')
        altpri.place(x=292.5,y=430,height=20,width=80)
        altpriinput = Entry(self.root,font=('微软雅黑',10,''),textvariable=self.altpri)
        altpriinput.place(x=372.5,y=429,height=24,width=105)
        # 修改按钮
        alter = Button(self.root,command=self.altclick, text ="修改书本",font=('微软雅黑',10,''),activeforeground='#ffffff',fg='#ffffff',activebackground='#7cba59',bd=2,bg='#2aa515')
        alter.place(x=350,y=500,height=35,width=100)
        # 保存按钮
        pre = Button(self.root,command=self.show, text ="保存书本信息",font=('微软雅黑',10,''),activeforeground='#ffffff',fg='#ffffff',activebackground='#7cba59',bd=2,bg='#2aa515')
        pre.place(x=595,y=500,height=35,width=100)
 
    # 写入判断输入框是否有空值
    def Isspace(self,text):
        temp = 0
        for i in text:
           if not i.isspace():
               temp = 1
               break
        if temp==1:
            return 0
        else:
            return 1
    # 检查写入是否有空值
    def click(self):
        addnum = self.addnum.get()
        addname = self.addname.get()
        addauthor = self.addauthor.get()
        addchu = self.addchu.get()
        adddate = self.adddate.get()
        addpri = self.addpri.get()
        if self.Isspace(addnum) or self.Isspace(addname) or self.Isspace(addauthor) or self.Isspace(addchu) or self.Isspace(adddate) or self.Isspace(addpri) :
            tkinter.messagebox.showerror(title='提示', message ="请填写所有信息")
        else:
            self.write(addnum,addname,addauthor,addchu,adddate,addpri)
    # 写入信息
    def write(self,addnum,addname,addauthor,addchu,adddate,addpri):
        f = open('图书.csv','r',encoding='utf-8')
        for line in f.readlines():
            info = line[:-1].split(",")
            if len(info)<6:
                break
            if info[0] ==addnum and info[1] ==addname:
                 tkinter.messagebox.showinfo(title='结果', message ="已存在该图书信息!")
                 f.close()
                 return
 
        f.close()
        f = open('图书.csv','a',encoding='utf-8')
        f.write('{},{},{},{},{},{}\n'.format(addnum,addname,addauthor,addchu,adddate,addpri))
        f.close()
        tkinter.messagebox.showinfo(title='提示', message ="写入成功,点击保存后更新")
 
    # 检查修改信息是否空白
    def altclick(self):
        altnum = self.altnum.get()
        altname = self.altname.get()
        altauthor = self.altauthor.get()
        altchu = self.altchu.get()
        altdate = self.altdate.get()
        altpri = self.altpri.get()
        if self.Isspace(altnum) or self.Isspace(altname) or self.Isspace(altauthor) or self.Isspace(altchu) or self.Isspace(altdate) or self.Isspace(altpri) :
            tkinter.messagebox.showerror(title='提示', message ="输入项为空")
        else:
            self.modify(altnum,altname,altauthor,altchu,altdate,altpri)
 
    # 修改信息
    def modify(self,altnum,altname,altauthor,altchu,altdate,altpri):
        temp = 0
        with open("图书.csv","r",encoding="utf-8") as f:
            lines = f.readlines()
   
        with open("图书.csv","w",encoding="utf-8") as f_w:
            for line in lines:
                info = line[:-1].split(",")
                if info[0] ==altnum:
                    temp = 1
                    f_w.write('{},{},{},{},{},{}\n'.format(altnum,altname,altauthor,altchu,altdate,altpri))
                    continue
                f_w.write(line)
        if temp==0:
            tkinter.messagebox.showerror(title='提示', message ="没有该信息")
        else:
            tkinter.messagebox.showinfo(title='提示', message ="修改成功,点击保存后更新")
    # 保存信息并显示
    def show(self):
        self.createPage() 
root = Tk() 
root.title('图书管理') 
LoginPage(root) 
root.mainloop() 

在运行代码前需要在同级文件夹下创建一个名为“图书”的csv文件,如下图所示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python简易版图书管理系统

    本文实例为大家分享了python图书管理系统的具体代码,供大家参考,具体内容如下 """ 图书管理系统 """ import random # books={书名:剩余数量, 书名:剩余数量} import time books = {"防脱发指南": 5, "颈椎康复指南": 3, "从删库到跑路": 0} # students_books=[name,{书}] students_bo

  • 基于python图书馆管理系统设计实例详解

    写完这个项目后,导师说这个你完全可以当作毕业项目使用了,写的很全,很多的都设计考虑周全,但我的脚步绝不止于现在,我想要的是星辰大海!与君共勉! 这个项目不是我的作业, 只是无意中被拉进来了,然后就承担了所有,肝了一周多,终于完成,但这个也算是一个很大的项目了吧,对于我现在来说,写这个项目遇到了很多困难,这是真的,其中涉及到数据库的使用,就遇到了一点瓶颈, 但这不算什么,还是要被我搞定的. 梦想就像这个远处夕阳,终究触手可及! Python项目: 项目前提: 这个项目涉及到的知识点有很多, 知识串

  • python实现简单图书管理系统

    用python实现一个简单的图书管理系统 ,供大家参考,具体内容如下 1.工具:PyCharm3.6 社区版 我创建了一个工程叫fairy,把解释器换成Pytnon3.6 创建一个pytnon file:图书管理系统.py 2.实现简单界面如下: """ 图书管理系统 1.查询图书 2.增加图书 3.借阅图书 4.归还图书 5.退出系统 """ 3.代码实现 # 书的属性:书名,作者,状态,位置 # 管理系统: class Book(object

  • python面向对象法实现图书管理系统

    本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下 需求: 图书管理系统 1.查询图书 2.增加图书 3.借阅图书 4.归还图书 5.退出系统 书:书名,作者,状态,位置 管理系统: 实现如下: class Book(object): def __init__(self, name, author, status, bookindex): self.name = name self.author = author self.status = status sel

  • python实现图书管理系统

    本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下 import mysql.connector import sys, os import time import datetime from tkinter import * from tkinter.messagebox import * class Libop: user = 'root' pwd = '' host = 'localhost' db = 'library' data_file = 'mys

  • python代码实现图书管理系统

    本文实例为大家分享了python代码实现图书管理系统的具体代码,供大家参考,具体内容如下 图书管理系统 功能简介 添加图书时,图书ID不能重复,图书名可重复 删除,查询,修改功能,输入图书名之后提供所有的同名的图书,用户可以按照图书序号对具体的一本书进行操作 显示书籍,分行显示,每行一本书的信息 书籍信息以如下格式保存在txt文本文件格式下 源代码 #-*- coding=utf8 -*- # @author:sololi # date: 2020/11/12 # 文件说明 : data imp

  • 教你用python实现一个无界面的小型图书管理系统

    一.需求了解 功能模块 图书信息 二.环境准备 安装mysql数据库 参考文章: MySQL数据库压缩版本安装与配置 MySQL msi版本下载安装图文教程 创建数据库表 创建数据库 CREATE DATABASE bookmanage; 使用数据库 use bookmanage; 创建表 create table books( id int unsigned primary key auto_increment not null, name varchar(20) default ""

  • python图书管理系统

    本文实例为大家分享了python图书管理系统的具体代码,供大家参考,具体内容如下 实现语言:python 图形框架:DTK+2.0 数据库框架:SQLite 3.0 本程序需要以下部件运行: Python2.5.0.GTK+2.16.Pygtk 2.16.PyGobject 2.14.Pycairo 1.4 LibiaryManager.py #!/usr/bin/python # -*- coding: utf-8 -*- import pygtk pygtk.require('2.0') i

  • wxpython实现图书管理系统

    用wxpython实现的简单图书管理系统,可以实现增加图书,删除图书,修改图书,查看图书.后台数据库为mysql数据库,采用的pymysql连接数据库.系统界面如下: 代码如下: 1.书本类代码 #author = liuwei date = 2017-06-02 from datetime import * #导入日期模块 __metaclass__ = type class Book: '''一个书本信息类,包括书本名字,作者名字和书本简单信息''' def __init__(self, b

  • 基于python实现图书管理系统

    本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下 添加新书 查询 借阅 二次添加新书(读取已有的.xls并修改) 代码: import xlwt import xlrd def read_old_data(row0_len): try: filename=".\图书.xls" old_data = []#读取表格已有内容 data = xlrd.open_workbook(filename) sheet0 = data.sheet_by_index(

随机推荐