入门级Python+Mysql+Tkinter的图书管理系统,小白都喜欢!!

入门级Python+Mysql+Tkinter的图书管理系统,小白都喜欢!!

功能介绍

首先我们的图书管理系统有着管理员登陆和学生登陆,学生可以注册账号,管理员为固定账号,实现了基本的增删改查功能

学生登陆窗口

次处是我们的程序开始的地方,可以在此页面跳转到的登陆界面和注册界面,同时可以完成登陆的账号密码验证

# 登陆界面
def login():
    global win
    win = tk.Tk()
    win.title('学生登陆页面')
    win.geometry('600x600')
    logo = tk.PhotoImage(file='one.png')
    tk.Label(win, image=logo).place(x=0, y=300)
    tk.Label(win, text='学生登陆页面', font=30, bg='green').place(x=250, y=15)
    tk.Label(win, text='账号 :', font=10).place(x=130, y=60)
    tk.Label(win, text='密码 :', font=10).place(x=130, y=100)
    user_name = tk.Entry(win, width=20, font=10)
    user_name.place(x=230, y=60)
    user_pwd = tk.Entry(win, width=20, font=10)
    user_pwd.place(x=230, y=100)

    def main():
        get_name = user_name.get()
        get_pwd = user_pwd.get()
        sql = "select * from user where user_name = %s and pwd = %s"
        rows = cursor.execute(sql, (get_name, get_pwd))
        print(rows)
        if get_name == '' or get_pwd == '':
            messagebox.showerror('Error', message='用户名或者密码为空')
        else:
            if rows != 0:
                closee()
                studentinterface()
            else:
                messagebox.showerror('Error', message='用户名或者密码错误')

    tk.Button(win, text='登陆', width=28, command=main).place(x=230, y=160)
    tk.Button(win, text='注册', width=28, command=Register).place(x=230, y=200)
    tk.Button(win, text='管理员登陆', width=28, command=Adminlogin).place(x=230, y=240)

    win.mainloop()

注册页面

注册页面可以实现用户的注册, 首先验证用户输入的两次密码是否一致, 如果两次密码一致, 把用户输入的信息存入数据库中

# 注册界面
def Register():
    global use_name_register
    win_ = tk.Tk()
    win_.title('注册')
    win_.geometry('380x300+200+200')
    # 注册的标签
    tk.Label(win_, text='注册', font=('SimHei', 24)).place(x=170, y=20)
    tk.Label(win_, text='账户', font=('KaiTi', 15)).place(x=50, y=70)
    tk.Label(win_, text='密码', font=('KaiTi', 15)).place(x=50, y=120)
    tk.Label(win_, text='确认密码', font=('KaiTi', 15)).place(x=50, y=170)
    # 密码不一致错误提示
    mistake_register = tk.Label(win_, fg='red')
    mistake_register.place(x=170, y=200)

    # 用户名输入
    use_name_register = tk.Entry(win_, font=('FangSong', 15), width=12)
    use_name_register.place(x=170, y=67)

    # 用户密码输入
    use_password_register = tk.Entry(win_, font=('FangSong', 15), width=12, show='*')
    use_password_register.place(x=170, y=117)
    # 重复密码输入
    use_password_register2 = tk.Entry(win_, font=('FangSong', 15), width=12, show='*')
    use_password_register2.place(x=170, y=167)

    def Sure():
        use_Pass1 = use_password_register.get()
        use_Pass2 = use_password_register2.get()
        # 验证俩次输入的密码是否相同
        if use_Pass1 != use_Pass2:
            mistake_register.configure(text='两次密码不一致,请重试')
        elif use_Pass1 == use_Pass2:
            # 获取输入的值
            user_name = use_name_register.get()

            print("here ", user_name)

            user_password = use_password_register.get()

            # 插入mysql的语句
            sql = "insert into user(user_name, pwd ) values (%s, %s )"
            cursor.execute(sql, (user_name, user_password))
            conn.commit()
            win_.destroy()

    # 注册界面的确定和取消
    butt_Reg_sure = tk.Button(win_, text='确认', font=('KaiTi', 15), width=8, command=Sure)
    butt_Reg_sure.place(x=150, y=220)

学生页面

学生页面只有搜索和显示所有图书的功能

# 学生页面
def studentinterface():
    from tkinter import ttk
    windlh1 = tk.Tk()
    windlh1.title('学生页面')
    windlh1.geometry('800x600')
    tk.Label(windlh1, text='书名').place(x=50, y=8)

    columns = ('book_list', 'book_name', 'book_writer')
    treeview = ttk.Treeview(windlh1, height=20, show='headings', columns=columns)
    # 显示表头
    treeview.heading('book_list', text='书本列表')
    treeview.heading('book_name', text='书本姓名')
    treeview.heading('book_writer', text='书本作者')
    treeview.place(x=10, y=50)

    # 设置表的宽度
    treeview.column('book_list', width=160, anchor='center')
    treeview.column('book_name', width=160, anchor='center')
    treeview.column('book_writer', width=160, anchor='center')

    e1 = tk.Entry(windlh1, )
    e1.place(x=100, y=10)

    def sql_conn(sql):
        conn = pymysql.connect(host="127.0.0.1",
                               user="root",
                               password="123456",
                               database="info",
                               charset="utf8")
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute(sql)
        ret = cursor.fetchall()
        cursor.close()
        conn.commit()
        conn.close()
        return ret

    def delete_tab(treeview):
        items = treeview.get_children()
        [treeview.delete(item) for item in items]

    def main():

        delete_tab(treeview)
        sql = "select * from book where name = '" + e1.get() + "' "
        list_book = sql_conn(sql)
        print(list_book)
        for i in range(len(list_book)):
            treeview.insert('', i, values=(list_book[i]['name'], list_book[i]['author'], list_book[i]['bookid']))

    def all_book():
        delete_tab(treeview)
        sql = 'select * from book'

        list_book = sql_conn(sql)
        for i in range(len(list_book)):
            treeview.insert('', i, values=(list_book[i]['name'], list_book[i]['author'], list_book[i]['bookid']))

    tk.Button(windlh1, text='搜索', width=15, height=1, command=main).place(x=280, y=10)
    tk.Button(windlh1, text='显示所有', width=15, height=1, command=all_book).place(x=450, y=10)

    windlh1.mainloop()

管理员登陆


# 管理员登陆界面
def Adminlogin():
    win_A = tk.Tk()

    win_A.title('管理员页面')
    win_A.geometry('600x300')
    tk.Label(win_A, text='管理员登陆页面', font=30, bg='green').place(x=250, y=15)
    tk.Label(win_A, text='账号 :', font=10).place(x=130, y=60)
    tk.Label(win_A, text='密码 :', font=10).place(x=130, y=100)
    adminName = tk.Entry(win_A, width=20, font=10)
    adminName.place(x=230, y=60)
    adminPwd = tk.Entry(win_A, width=20, font=10)
    adminPwd.place(x=230, y=100)

    def main():
        if adminName.get() == adminname and int(adminPwd.get()) == adminpwd:
            closee()
            win_A.destroy()
            Admininterface()
        else:
            messagebox.showerror('Error', message='管理员账号或者密码错误')

    tk.Button(win_A, text='登陆', width=28, command=main).place(x=230, y=160)
    tk.Button(win_A, text='退出', width=28, command=win_A.destroy).place(x=230, y=200)
    win_A.mainloop()

管理员界面


# 管理员页面
def Admininterface():
    windlh1 = tk.Tk()
    windlh1.title('管理员页面')
    windlh1.geometry('800x600')

    columns = ('book_list', 'book_name', 'book_writer')
    treeview = ttk.Treeview(windlh1, height=20, show='headings', columns=columns)
    # 显示表头
    treeview.heading('book_list', text='书本名字')
    treeview.heading('book_name', text='书本作者')
    treeview.heading('book_writer', text='书本序号')
    treeview.place(x=10, y=50)

    # 设置表的宽度
    treeview.column('book_list', width=160, anchor='center')
    treeview.column('book_name', width=160, anchor='center')
    treeview.column('book_writer', width=160, anchor='center')

    tk.Label(windlh1, text='书本名字', ).place(x=550, y=70)
    tk.Label(windlh1, text='书本作者', ).place(x=550, y=130)
    tk.Label(windlh1, text='书本序号', ).place(x=550, y=190)
    e1 = tk.Entry(windlh1)
    e1.place(x=630, y=75)
    e2 = tk.Entry(windlh1)
    e2.place(x=630, y=135)
    e3 = tk.Entry(windlh1)
    e3.place(x=630, y=195)
    e4 = tk.Entry(windlh1)
    e4.place(x=100, y=10)

    def sql_conn(sql):
        conn = pymysql.connect(host="127.0.0.1",
                               user="root",
                               password="123456",
                               database="info",
                               charset="utf8")
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute(sql)
        ret = cursor.fetchall()
        cursor.close()
        conn.commit()
        conn.close()
        return ret

    # 清空列表
    def delete_tab(treeview):
        items = treeview.get_children()
        [treeview.delete(item) for item in items]

    # 搜素
    def main():
        delete_tab(treeview)
        sql = "select * from book where name = '" + e4.get() + "' "
        list_book = sql_conn(sql)
        print(list_book)
        for i in range(len(list_book)):
            treeview.insert('', i, values=(list_book[i]['name'], list_book[i]['author'], list_book[i]['bookid']))

    # 显示所有的数据
    def all_book():
        delete_tab(treeview)
        sql = 'select * from book'

        list_book = sql_conn(sql)
        for i in range(len(list_book)):
            treeview.insert('', i, values=(list_book[i]['name'], list_book[i]['author'], list_book[i]['bookid']))

    # 插入功能
    def insert():
        if e1.get() == '' and e2.get() == '' and e3.get() == '':
            tk.messagebox.showerror('警告', '请填写完整!')
        else:
            try:
                sql = "insert into book values ('" + e1.get() + "','" + e2.get() + "','" + e3.get() + "');"
                ret = sql_conn(sql)
                tk.messagebox.showinfo('', '插入成功!')
            except:
                tk.messagebox.showerror('警告', '插入字段已存在!')
        delete_tab(treeview)

    def delete():
        sql = "delete from book where name='" + e1.get() + "';"
        ret = sql_conn(sql)
        tk.messagebox.showinfo('', '删除成功!')
        delete_tab(treeview)

    def alter():
        if e1.get() == '' and e2.get() == '' and e3.get() == '':
            tk.messagebox.showerror('警告', '请填写完整!')
        else:
            sql = "update book set name='" + e1.get() + "',author='" + e2.get() + "'  where  bookid='" + e3.get() + "';"
            ret = sql_conn(sql)
            delete_tab(treeview)
            tk.messagebox.showinfo('', '修改成功!')


    tk.Button(windlh1, text='搜索', width=15, command=main).place(x=280, y=10)
    tk.Button(windlh1, text='增加', width=15, command=insert).place(x=600, y=250)
    tk.Button(windlh1, text='删除', width=15, command=delete).place(x=600, y=310)
    tk.Button(windlh1, text='修改', width=15, command=alter).place(x=600, y=370)
    tk.Button(windlh1, text='显示所有', width=15, height=1, command=all_book).place(x=450, y=10)

    windlh1.mainloop()

界面展示

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

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

在这里插入图片描述

  • 27
    点赞
  • 248
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值