tkinter实现图书管理系统介绍

7 篇文章 5 订阅 ¥29.90 ¥99.00

前文

岁月不语,时光依旧。本文将介绍基于tkinter实现的图书管理系统以及一些其他功能的实现,让我们一起往下看看吧!

项目介绍

Tkinter(即 tk interface,简称“Tk”)本质上是对 Tcl/Tk 软件包的 Python 接口封装,它是 Python
官方推荐的 GUI 工具包,属于 Python 自带的标准库模块,当您安装好 Python 后,就可以直接使用它,而无须另行安装。 作为一款
Python GUI 工具,Tkinter 拥有良好的跨平台性,支持 Windows、Linux、Mac 平台,它传承了 Python
语法简洁、代码易读的基本特点。 与其他编程语言的 GUI 工具包相比,Tkinter
编码效率高,能够实现快速开发的目的,非常适合初学者学习。Tkinter 使用纯 Python 语言开发,与 C/C++ 开发的 Qt
框架相比,Tkinter 有自身的局限性,比如性能、功能丰富程度等都不及
Qt,因此它只适合开发一些简单的程序,比如计算器的界面,或者一个简易的聊天窗口等。

当我们运行程序时,进入登录界面,选择管理员登录或者普通用户登录,管理员界面有首页、图书管理、用户管理、借书记录、更改密码;普通用户登录界面有首页、搜索、更改密码。
登录流程图
登录流程图
图书管理功能
在这里插入图片描述
图书管理模块
在这里插入图片描述
管理员界面展示
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
普通用户界面展示
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

具体功能

首页界面含有天气预报以及搜索城市天气,默认为北京。数据来源于2345,我们只需要把爬下来的数据排布好就行了,这里涉及到request库的使用,也就是爬虫。天气预报下面的公告栏,管理员可以修改内容并发布,所有的普通用户都能看见,普通用户是不允许修改的。

图书管理界面分别有添加、修改、还书、借书、删除,添加顾名思义就是添加图书,可以设置图书的分类、数量等。修改就是对图书可能出现的错误进行修改。当普通用户要借书的时候,我们可以根据用户的id,来设置借书的时间和借哪本书。有借书就有还书了,输入用户和书的id即可还书成功。有些图书可能已经没有用了,这时候我们就选择把此书删除了。

用户管理有添加、修改、删除,添加就是添加新用户,修改可以修改用户的秘密,有时候用户密码错误次数超过了3次,就不允许登录了,这里我们就可以把改过来,让用户登录。删除就是删除用户,被删除的用户再次登录就是无效的了。

借书记录界面,这里存储着所有用户的借书记录,在这里都能看到,用户的借书时间以及还书时间等。

更改密码界面,管理员修改密码。

我们还增加了菜单,菜单包含功能有注销(返回登录界面),退出(结束程序),帮助(未写)。

普通用户的界面大同小异,没有用户管理,其他都是差不多的,图书管理变成了搜索图书,只能搜索图书了。

入口程序代码展示

这里我们要用到tkinter.ttk.Notebook(),在Python GUI编程中,ttk.Notebook是一个非常有用且常用的控件。它是一个带有选项卡的容器,可以在不同的选项卡中放置不同的控件。所以我们编写的每一个界面都用Frame()作为容器,在主程序中创建一个选项卡(Notebook),然后将其他界面用add()方法添加到容器当中。这样就完成了在同一个界面展示效果。


from page.login import Login
from other_fun.center_gui import gui_center
from page.home_pgae import HomePage
import tkinter
import tkinter.ttk
from tkinter_book.page import admin_page, change_password, commonuser, logmessage, managingusers

'''
运行函数  主函数  都在此运行
每个页面都是单独的 在此汇集显示
'''

class MainGui:

    def stu_gui(self, user_name):
        self.root = tkinter.Tk()
        self.root.overrideredirect(True)
        gui_center(self.root, 800, 650)
        self.root.title('主页面')
        self.root.resizable()
        # 添加菜单
        menu = tkinter.Menu(self.root)
        menu.add_command(label='注销', command=self.log_off)
        menu.add_command(label='退出', command=self.root.destroy)
        menu.add_command(label='帮助')
        self.root.config(menu=menu)

        # 选择框
        style = tkinter.ttk.Style()
        note = tkinter.ttk.Notebook(self.root)
        style.configure('TNotebook.Tab', font=("微软雅黑", 20, 'bold'), foreground='red')

        # 首页 共有的
        home = HomePage(1)
        note.add(home.home_frame, text='首页')
        note.pack(fill='both', expand='true')

        # 搜索
        search_common_user = commonuser.CommonUser()
        note.add(search_common_user.main_show_frame, text='搜索')
        note.pack(fill='both', expand='true')
        # 借书消息   self.v.v_user.get()
        books_message = logmessage.LogMessage(1, user=user_name)
        note.add(books_message.main_show_frame, text='消息')
        note.pack(fill='both', expand='true')
        # 更改密码
        password = change_password.ChangePassword(user_name, 1)
        note.add(password.change_frame, text='更改密码')
        note.pack(fill='both', expand='true')

        self.root.mainloop()

    '''
quit()导致mainloop退出,但不会直接导致任何小部件被销毁。
但是,如果调用mainloop后没有代码,那么脚本将退出,所有小部件都将被销毁
destroy()将销毁小部件。如果销毁根窗口,那么所有其他小部件都将被销毁,mainloop将停止
    
    '''

    # 注销
    def log_off(self):
        # 很神奇不报错了
        self.root.quit()
        self.root.destroy()
        self.main()

    def admin_gui(self):
        self.root = tkinter.Tk()
        self.root.overrideredirect(True)
        gui_center(self.root, 800, 650)
        self.root.title('页面')
        self.root.resizable()
        # 添加菜单
        menu = tkinter.Menu(self.root)
        menu.add_command(label='注销', command=self.log_off)
        menu.add_command(label='退出', command=self.root.destroy)
        menu.add_command(label='帮助')
        self.root.config(menu=menu)

        # 选择框
        style = tkinter.ttk.Style()
        note = tkinter.ttk.Notebook(self.root)
        style.configure('TNotebook.Tab', font=("微软雅黑", 20, 'bold'), foreground='red')

        # 首页
        home = HomePage(2)
        note.add(home.home_frame, text='首页')
        note.pack(fill='both', expand='true')

        # 图书管理
        admin_books = admin_page.Admin()
        note.add(admin_books.main_show_frame, text='图书管理')
        note.pack(fill='both', expand='true')

        # 用户管理
        user_page = managingusers.ManagingUsers()
        note.add(user_page.main_show_frame, text='用户管理')
        note.pack(fill='both', expand='true')

        # 日志消息
        log_message = logmessage.LogMessage(2)
        note.add(log_message.main_show_frame, text='借书记录')
        note.pack(fill='both', expand='true')

        # 更改密码
        change_password_ = change_password.ChangePassword('', 2)
        note.add(change_password_.change_frame, text='更改密码')
        note.pack(fill='both', expand='true')
        self.root.mainloop()

    def main(self):
        self.v = Login()
        if self.v.flag:
            if self.v.vali.get() == 1:
                print('学生登录')
                self.stu_gui(self.v.v_user.get())
            if self.v.vali.get() == 2:
                print('管理员登录')
                self.admin_gui()


if __name__ == '__main__':
    MainGui().main()

结尾

莫愁前路无知己,天下谁人不识君。本文主要介绍了tkiinter模块写一个图书管理系统和一些主要的功能。点击跳转到详情页,欢迎大家的点赞收藏哈,再见了。 ————2023.8.9 20:12

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,以下是用 Python+MySQL+tkinter 实现图书管理系统,并实现图书数据分析的代码示例: 1. 创建数据库并建立连接 ``` import MySQLdb # 建立数据库连接 db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="library") # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 创建图书表 cursor.execute("""CREATE TABLE books ( id INT(11) NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, publisher VARCHAR(255) NOT NULL, pubdate DATE NOT NULL, price FLOAT NOT NULL, PRIMARY KEY (id) )""") ``` 2. 创建图书管理系统界面 ``` import tkinter as tk # 创建主窗口 root = tk.Tk() root.title("图书管理系统") root.geometry("500x400") # 标题 title_label = tk.Label(root, text="图书管理系统", font=("Arial", 20)) title_label.pack(pady=20) # 添加图书表单 title_label = tk.Label(root, text="添加图书", font=("Arial", 16)) title_label.pack() title_entry = tk.Entry(root) title_entry.pack(pady=10) author_entry = tk.Entry(root) author_entry.pack(pady=10) publisher_entry = tk.Entry(root) publisher_entry.pack(pady=10) pubdate_entry = tk.Entry(root) pubdate_entry.pack(pady=10) price_entry = tk.Entry(root) price_entry.pack(pady=10) add_button = tk.Button(root, text="添加图书", command=add_book) add_button.pack(pady=20) # 查询图书表单 title_label = tk.Label(root, text="查询图书", font=("Arial", 16)) title_label.pack() query_entry = tk.Entry(root) query_entry.pack(pady=10) query_button = tk.Button(root, text="查询图书", command=query_book) query_button.pack(pady=20) # 图书列表 books_listbox = tk.Listbox(root, height=10, width=80) books_listbox.pack(pady=20) # 删除图书按钮 delete_button = tk.Button(root, text="删除图书", command=delete_book) delete_button.pack() root.mainloop() ``` 3. 实现图书管理功能 ``` def add_book(): # 获取表单数据 title = title_entry.get() author = author_entry.get() publisher = publisher_entry.get() pubdate = pubdate_entry.get() price = price_entry.get() # 插入数据到数据库 cursor.execute("INSERT INTO books (title, author, publisher, pubdate, price) VALUES (%s, %s, %s, %s, %s)", (title, author, publisher, pubdate, price)) db.commit() # 清空表单 title_entry.delete(0, tk.END) author_entry.delete(0, tk.END) publisher_entry.delete(0, tk.END) pubdate_entry.delete(0, tk.END) price_entry.delete(0, tk.END) def query_book(): # 获取查询关键词 keyword = query_entry.get() # 查询数据 cursor.execute("SELECT * FROM books WHERE title LIKE %s OR author LIKE %s OR publisher LIKE %s", ('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%')) books = cursor.fetchall() # 清空列表 books_listbox.delete(0, tk.END) # 添加查询结果到列表 for book in books: books_listbox.insert(tk.END, book) def delete_book(): # 获取选中的图书 ID selected_books = books_listbox.curselection() book_ids = [books_listbox.get(index)[0] for index in selected_books] # 删除数据 for book_id in book_ids: cursor.execute("DELETE FROM books WHERE id=%s", (book_id,)) db.commit() # 刷新列表 query_book() ``` 4. 数据分析功能 ``` import pandas as pd import matplotlib.pyplot as plt # 从数据库中读取数据 cursor.execute("SELECT * FROM books") books = cursor.fetchall() # 转化为 pandas DataFrame df = pd.DataFrame(list(books), columns=['id', 'title', 'author', 'publisher', 'pubdate', 'price']) # 按年份统计图书数量 df['year'] = pd.to_datetime(df['pubdate']).dt.year year_count = df.groupby('year')['id'].count() # 绘制图书借阅量随时间的变化曲线 plt.plot(year_count.index, year_count.values) plt.xlabel('Year') plt.ylabel('Number of books') plt.show() ``` 5. 完善功能 你可以根据需求添加更多的功能,例如图书分类、借阅记录等。 希望这些代码能帮助你完成图书管理系统和数据分析功能。注意修改连接数据库的用户名、密码和数据库名等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天501

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值