Python图书管理系统

GloriaAsukDemo发布

自己写完这个图书管理系统收获还是蛮多的,在这里就稍微总结一下遇到的坑吧。

  1. pack()与grid()似乎时不能放在一起使用的。具体在百度的时候查到的,因为时间紧张就没留意内容,所以想要编排样式需要使用place()。place 布局就是其他 GUI 编程中的“绝对布局”,这种布局方式要求程序显式指定每个组件的绝对位置或相对于其他组件的位置。具体关于 place()的使用方法如下:
    • x:指定组件的 X 坐标。x 为 0 代表位于最左边
    • y:指定组件的 Y 坐标。y 为 0 代表位于最右边。
    • relx:指定组件的 X 坐标,以父容器总宽度为单位 1,该值应该在 0.0~1.0 之间,其中 0.0 代表位于窗口最左边,1.0 代表位于窗口最右边,0.5 代表位于窗口中间。
    • rely:指定组件的 Y 坐标,以父容器总高度为单位 1,该值应该在 0.0~1.0 之间,其中 0.0 代表位于窗口最上边,1.0 代表位于窗口最下边,0.5 代表位于窗口中间。
    • width:指定组件的宽度,以 pixel 为单位。
    • height:指定组件的高度,以 pixel 为单位。
    • relwidth:指定组件的宽度,以父容器总宽度为单位 1,该值应该在 0.0~1.0 之间,其中 1.0 代表整个窗口宽度,0.5 代表窗口的一半宽度。
    • relheight:指定组件的高度,以父容器总高度为单位 1,该值应该在 0.0~1.0 之间,其中 1.0 代表整个窗口高度,0.5 代表窗口的一半高度。
  2. 从数据库中读取数据遍历一遍后,其中的数据将不能再被读取。想要在读取的话需要重新从数据库库里拿数据。这个好像是和列表的性质有关,第一次抹除数据。
  3. CheckButton的效果类似于调查问卷中的选项,有一种选择作用。在本系统中我在注册里面使用过一次。具体的使用方法如下:var = IntVar(窗口名称)存放按钮状态。注意括号里面的窗口名称必须填上,否则无法存储按钮状态。别问我怎么知道的QAQc = Checkbutton(窗口名称, text="。。。", variable=var)c.pack()默认情况下,variable 选项设置为 1 表示选中状态,反之设置为 0。你可以使用 onvalue 和 offvalue 选项修改它们的值。
  4. 修改完数据库的值之后别忘了commit一下,比如INSERT、UPDATE、DELETE。
  5. ListBox的使用当你创建一个 Listbox 组件的时候,它是空的,所以第一件要做的事就是添加一行或多行文本进去。我们使用 insert() 方法添加文本,该方法有两个参数:第一个参数是插入的索引号,第二个参数是插入的字符串。索引号通常是项目的序号(0 是列表中第一项的序号)。不过你也可以使用一些特殊的索引号:比如 ACTIVE 表示选中的项目(如果 Listbox 允许多选,那么它表示最后一个被选中的项目);又如 END 表示 Listbox 的最后一行,所以当要插入一个项目到列表时可以使用 END:theLB = tk.Listbox(master)theLB.pack()for item in ["鸡蛋", "鸭蛋", "鹅蛋", "李狗蛋"]: theLB.insert(END, item)清空listbox:listbox.delete(0, "end")清空指定选项:listbox.delete(ACTIVE)获取选项内容:listbox.get(ACTIVE)

以下就是代码,没有使用面向对象的思想,很尴尬

注意创建数据库表的代码都是注释过的

import sqlite3
import tkinter as tk
import tkinter.messagebox
from tkinter import *
import datetime
conn = sqlite3.connect('library.db')
sql = conn.cursor()

#创建图书信息表:id 书名 作者 出版商 数量
#sql.execute('CREATE TABLE books (bid integer primary key, bname varchar(20),author varchar(20),publisher varchar(30),value integer)')
#创建用户信息表:id 用户名 密码 用户身份
#sql.execute('CREATE TABLE users (userid integer primary key, username varchar(20),password varchar(30),identity integer)')
#创建借阅信息表:id 借阅人 操作类型 操作时间 借阅图书 还书状态
#sql.execute('CREATE TABLE control (conid integer primary key, username varchar(20),conkind varchar(30),conbook varchar(20), contime date,constate varchar(20))')

conn.commit()
un=''
#添加图书界面
def windowaddbook():
    windowaddb = tk.Tk()
    windowaddb.title('添加图书')
    windowaddb.geometry('500x360')
    l = tk.Label(windowaddb, text='请填写图书信息', font=('Arial', 16), width=500, height=2)
    l.pack()
    l1 = tk.Label(windowaddb, text='图书名称', font=('Arial', 10), width=100, height=2)
    l4 = tk.Label(windowaddb, text='作者', font=('Arial', 10), width=100, height=2)
    l2 = tk.Label(windowaddb, text='图书出版商', font=('Arial', 10), width=100, height=2)
    l3 = tk.Label(windowaddb, text='数量', font=('Arial', 10), width=100, height=2)
    e1 = tk.Entry(windowaddb, show=None, font=('Arial', 14))
    e2 = tk.Entry(windowaddb, show=None, font=('Arial', 14))
    e3 = tk.Entry(windowaddb, show=None, font=('Arial', 14))
    e4 = tk.Entry(windowaddb, show=None,font=('Arial', 14))
    l1.pack()
    e1.pack()
    l4.pack()
    e4.pack()
    l2.pack()
    e2.pack()
    l3.pack()
    e3.pack()
    #添加图书逻辑处理
    def addbook():
        name = e1.get()
        bname = '《'+name+'》'
        publisher = e2.get()
        valu = e3.get()
        author=e4.get()
        if(name=='' or publisher=='' or author=='' or valu==''):
            tk.messagebox.showinfo('警告', '填写不完整')
        else:
            value=int(valu)
            allist=sql.execute("SELECT * FROM books WHERE bname='" + bname + "' and author='" + author + "' and publisher='" + publisher + "'")
            f=999
            for row in allist:
                f=row[0]
            if(f!=999):
                tk.messagebox.showinfo('警告', '该类图书已经添加')
            else:
                sql.execute('INSERT INTO books(bname,author,publisher,value) VALUES (?,?,?,?)', (bname, author, publisher, value))
                conn.commit()
                # e1.select_clear(windowaddb)
                # e2.select_clear(windowaddb)
                # e3.select_clear(windowaddb)
                # e4.select_clear(windowaddb)
                tk.messagebox.showinfo('提示', '添加成功')
    buadd = tk.Button(windowaddb, text='添加', font=('Arial', 12), width=10, height=1, command=addbook)
    buadd.pack()
    windowaddb.mainloop()
#删除图书界面:
def windowdellbook():
    windowdellb = tk.Tk()
    windowdellb.title('删除图书')
    windowdellb.geometry('500x360')
    # 存放按钮状态
    checkbook = IntVar(windowdellb)
    checkid = IntVar(windowdellb)
    l1 = tk.Label(windowdellb, text='请选择删除方式', font=('Arial', 16), width=500, height=2)
    l1.pack()
    Checkbutton(windowdellb, text="图书名称", variable=checkbook).pack()
    Checkbutton(windowdellb, text="图书ID", variable=checkid).pack()
    l = tk.Label(windowdellb, text='请输入要删除的图示名称或id', font=('Arial', 16), width=500, height=2)
    l.pack()
    e1 = tk.Entry(windowdellb, show=None, font=('Arial', 14))
    e1.pack()
    #删除图书逻辑处理
    def dellb():
        name = e1.get()
        if(checkbook.get()==1):
            bname='《' + name + '》'
            alist = sql.execute("SELECT * FROM books WHERE bname='" + bname + "'")
        else:
            iname = name
            alist = sql.execute("SELECT * FROM books WHERE bid='" + iname + "'")
        windowdelb = tk.Tk()
        windowdelb.title('删除图书')
        windowdelb.geometry('1920x1080')
        lb = Listbox(windowdelb, width='500', font=('Arial', 14))
        lb.pack()
        for row in alist:
            book = '图书ID:'+str(row[0]) + '   ' + '图书名称:' + row[1] + '   ' + '图书作者:'+ row[2]  + '   ' + '图书出版社:'+ row[3]  + '   ' + '剩余数量:'+ str(row[4])
            lb.insert(END, book)
        def delb():
            s=lb.get(ACTIVE)
            s=list(s)
            id=s[0]
            sql.execute("DELETE FROM books WHERE bid='" + id + "'")
            conn.commit()
            lb.delete(ACTIVE)
            tk.messagebox.showinfo('提示','删除成功')
        aButton = tk.Button(windowdelb, text="删除", command=delb, width=10,font=('Arial', 12))
        aButton.pack()
    budel = tk.Button(windowdellb, text='查询', font=('Arial', 12), width=10, height=1, command=dellb)
    budel.pack()
    windowdellb.mainloop()
#修改与查询图书界面
def windoweditbook():
    windoweditb = tk.Tk()
    windoweditb.title('修改与查询图书')
    windoweditb.geometry('1920x1080')
    '''显示所有图书模块'''
    alist = sql.execute("SELECT * FROM books")
    lb1 = Listbox(windoweditb, width='500',font=('Arial', 16))
    lb1.place(x=0,y=0)
    for row in alist:
        book = '图书ID:'+str(row[0]) + '   ' + '图书名称:' + row[1] + '   ' + '图书作者:'+ row[2]  + '   ' + '图书出版社:'+ row[3]  + '   ' + '剩余数量:'+ str(row[4])
        lb1.insert(END, book)
    '''填写新图书信息模块'''
    l2 = tk.Label(windoweditb, text='请填写新的图书信息', font=('Arial', 16))
    l2.place(relx=0.125, rely=0.35)
    booknameLabel = tk.Label(windoweditb, text='图书名称:', font=('Arial', 12), justify='right')
    booknameEntry = tk.Entry(windoweditb, show=None, font=('Arial', 12))
    booknameLabel.place(relx=0.1, rely=0.4)
    booknameEntry.place(relx=0.14, rely=0.401)
    authorLabel = tk.Label(windoweditb, text='作者:', font=('Arial', 12), justify='right')
    authorEntry = tk.Entry(windoweditb, show=None, font=('Arial', 12))
    authorLabel.place(relx=0.1, rely=0.45)
    authorEntry.place(relx=0.14, rely=0.451)
    publisherLabel = tk.Label(windoweditb, text='出版商:', font=('Arial', 12), justify='right')
    publisherEntry = tk.Entry(windoweditb, show=None, font=('Arial', 12))
    publisherLabel.place(relx=0.1, rely=0.5)
    publisherEntry.place(relx=0.14, rely=0.501)
    valueLabel = tk.Label(windoweditb, text='数量:', font=('Arial', 12), justify='right')
    valueEntry = tk.Entry(windoweditb, show=None, font=('Arial', 12))
    valueLabel.place(relx=0.1, rely=0.55)
    valueEntry.place(relx=0.14, rely=0.551)
    #填写新图书信息模块下的修改图书处理逻辑
    def edit():
        name = booknameEntry.get()
        bname='《'+name+'》'
        author = authorEntry.get()
        publisher = publisherEntry.get()
        value = valueEntry.get()
        if (name == '' or publisher == '' or author == '' or value == ''):
            tk.messagebox.showinfo('警告', '填写不完整')
        else:
            s = lb1.get(ACTIVE)
            s = list(s)
            id = s[0]
            sql.execute("UPDATE books SET bname=?, author=?, publisher=?, value=? WHERE bid=?",(bname,author,publisher,value,id))
            conn.commit()
            lb1.delete(0,END)
            blist = sql.execute("SELECT * FROM books")
            for row in blist:
                book = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + row[3] + ' ' + str(row[4])
                lb1.insert(END, book)
            tk.messagebox.showinfo('提示','修改成功')
    editbButton = tk.Button(windoweditb, text="修改", font=('Arial', 12), width=10, height=1,command = edit)
    editbButton.place(relx=0.15, rely=0.6)
    '''查询板块'''
    l3 = tk.Label(windoweditb, text='查询图书信息', font=('Arial', 16))
    l3.place(relx=0.5, rely=0.35)
    # 存放按钮状态
    checkbook = IntVar(windoweditb)
    checkid = IntVar(windoweditb)
    l1 = tk.Label(windoweditb, text='请选择查询方式', font=('Arial', 16))
    l1.place(relx=0.5, rely=0.4)
    Checkbutton(windoweditb, text="图书名称", variable=checkbook).place(relx=0.5, rely=0.45)
    Checkbutton(windoweditb, text="图书ID", variable=checkid).place(relx=0.5, rely=0.48)
    l = tk.Label(windoweditb, text='请输入要修改的图示名称或id', font=('Arial', 16))
    l.place(relx=0.5, rely=0.52)
    e1 = tk.Entry(windoweditb, show=None, font=('Arial', 14))
    e1.place(relx=0.5, rely=0.55)
    #查询逻辑处理
    def search():
        name = e1.get()
        if(checkbook.get()==1):
            bname='《' + name + '》'
            alist = sql.execute("SELECT * FROM books WHERE bname='" + bname + "'")#访问一次就会消失
        else:
            iname = name
            alist = sql.execute("SELECT * FROM books WHERE bid='" + iname + "'")
        f = '999'
        for row in alist:
            f = row[0]
        if (f == '999'):
            tk.messagebox.showinfo('警告', '图书不存在')
        else:
            lb1.delete(0, END)
            if (checkbook.get() == 1):
                bname = '《' + name + '》'
                alist = sql.execute("SELECT * FROM books WHERE bname='" + bname + "'")
            else:
                iname = name
                alist = sql.execute("SELECT * FROM books WHERE bid='" + iname + "'")
            for row in alist:
                book = '图书ID:'+str(row[0]) + '   ' + '图书名称:' + row[1] + '   ' + '图书作者:'+ row[2]  + '   ' + '图书出版社:'+ row[3]  + '   ' + '剩余数量:'+ str(row[4])
                lb1.insert(END, book)

    budel = tk.Button(windoweditb, text='查询', font=('Arial', 12), width=10, height=1, command = search)
    budel.place(relx=0.5, rely=0.6)

    windoweditb.mainloop()



##############以上为图书增删改查信息维护板块
##############以下为人员增删改查信息维护板块

#删除用户界面:
def windowdelluser():
    windowdellu = tk.Tk()
    windowdellu.title('删除用户')
    windowdellu.geometry('500x200')

    l = tk.Label(windowdellu, text='请输入要删除的用户名', font=('Arial', 16), width=500, height=2)
    l.pack()
    e1 = tk.Entry(windowdellu, show=None, font=('Arial', 14))
    e1.pack()
    #删除用户逻辑处理
    def dellu():
        username = e1.get()
        alist = sql.execute("SELECT * FROM users WHERE username='" + username + "'")
        f='999'
        for row in alist:
            f = row[0]
        if (f == '999'):
            tk.messagebox.showinfo('警告', '该用户不存在')
        else:
            id=str(f)
            sql.execute("DELETE FROM users WHERE userid='" + id + "'")
            conn.commit()
            tk.messagebox.showinfo('提示','删除成功')
    budel = tk.Button(windowdellu, text='删除', font=('Arial', 12), width=10, height=1, command=dellu)
    budel.pack()
    windowdellu.mainloop()

#修改与查询用户界面
def windowedituser():
    windoweditu = tk.Tk()
    windoweditu.title('修改与查询用户')
    windoweditu.geometry('1920x1080')
    '''显示所有用户模块'''
    alist = sql.execute("SELECT * FROM users")
    lb1 = Listbox(windoweditu, width='500',font=('Arial', 12))
    lb1.place(x=0,y=0)
    for row in alist:
        user = '用户编号:'+str(row[0]) + '  ' +'用户名:'+ row[1] + '  ' + '密码:'+row[2] + '  ' + '用户身份:'+str(row[3])
        lb1.insert(END, user)
    '''填写新用户信息模块'''
    l2 = tk.Label(windoweditu, text='请填写新的用户信息', font=('Arial', 16))
    l2.place(relx=0.125, rely=0.35)
    usernameLabel = tk.Label(windoweditu, text='用户名:', font=('Arial', 12), justify='right')
    usernameEntry = tk.Entry(windoweditu, show=None, font=('Arial', 12))
    usernameLabel.place(relx=0.1, rely=0.4)
    usernameEntry.place(relx=0.14, rely=0.401)
    passwordLabel = tk.Label(windoweditu, text='密码:', font=('Arial', 12), justify='right')
    passwordEntry = tk.Entry(windoweditu, show='*', font=('Arial', 12))
    passwordLabel.place(relx=0.1, rely=0.45)
    passwordEntry.place(relx=0.14, rely=0.451)
    identityLabel = tk.Label(windoweditu, text='用户身份:', font=('Arial', 12), justify='right')
    identityEntry = tk.Entry(windoweditu, show=None, font=('Arial', 12))
    identityLabel.place(relx=0.1, rely=0.5)
    identityEntry.place(relx=0.14, rely=0.501)

    #填写新用户信息模块下的修改用户信息处理逻辑
    def edit():
        username = usernameEntry.get()
        password = passwordEntry.get()
        identity = identityEntry.get()
        if (username == '' or password == '' or identity == ''):
            tk.messagebox.showinfo('警告', '填写不完整')
        else:
            s = lb1.get(ACTIVE)
            s = list(s)
            id = s[0]
            sql.execute("UPDATE users SET username=?, password=?, identity=? WHERE userid=?",(username,password,identity,id))
            conn.commit()
            lb1.delete(0,END)
            blist = sql.execute("SELECT * FROM users")
            for row in blist:
                user = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + str(row[3])
                lb1.insert(END, user)
            tk.messagebox.showinfo('提示','修改成功')
    editbButton = tk.Button(windoweditu, text="修改", font=('Arial', 12), width=10, height=1,command = edit)
    editbButton.place(relx=0.15, rely=0.6)
    '''查询板块'''
    l3 = tk.Label(windoweditu, text='查询用户信息', font=('Arial', 16))
    l3.place(relx=0.5, rely=0.35)
    l = tk.Label(windoweditu, text='请输入要查询的用户名', font=('Arial', 16))
    l.place(relx=0.5, rely=0.4)
    e1 = tk.Entry(windoweditu, show=None, font=('Arial', 14))
    e1.place(relx=0.5, rely=0.45)
    #查询逻辑处理
    def search():
        username = e1.get()
        alist = sql.execute("SELECT * FROM users WHERE username='" + username + "'")
        f = '999'
        for row in alist:
            f = row[0]
        if (f == '999'):
            tk.messagebox.showinfo('警告', '该用户不存在')
        else:
            lb1.delete(0, END)
            blist = sql.execute("SELECT * FROM users WHERE username='" + username + "'")
            for row in blist:
                user = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + str(row[3])
                lb1.insert(END, user)

    budel = tk.Button(windoweditu, text='查询', font=('Arial', 12), width=10, height=1, command = search)
    budel.place(relx=0.5, rely=0.5)

    windoweditu.mainloop()


##############以上为人员增删改查信息维护板块
##############以下为借阅系统界面

#用户借书界面
def windowborrow():
    windowborr = tk.Tk()
    windowborr.title('借阅图书')
    windowborr.geometry('600x700')
    '''显示所有图书模块'''
    alist = sql.execute("SELECT * FROM books")
    lb1 = Listbox(windowborr, width='500', font=('Arial', 12))
    lb1.pack()
    for row in alist:
        book = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + row[3] + ' ' + str(row[4])
        lb1.insert(END, book)
    #借书逻辑处理
    def borr():
        s = lb1.get(ACTIVE)
        s = list(s)
        id = s[0]
        alist = sql.execute("SELECT value,bname FROM books WHERE bid='"+ id + "'")
        for row in alist:
            f=row[0]
            bname=row[1]
        if(f<=0):
            tk.messagebox.showinfo('提示','抱歉,该书已经被借光啦QAQ')
        else:
            f=f-1
            sql.execute("UPDATE books SET value=? WHERE bid=?",(f,id))
            conn.commit()
            time=datetime.datetime.now()
            conkind='借阅'
            constate='1'
            sql.execute("INSERT INTO control (username, conkind, conbook,contime,constate) VALUES (?,?,?,?,?)",(un,conkind,bname,time,constate))
            conn.commit()
            tk.messagebox.showinfo('提示','借阅成功!请凭该界面办理借书手续')

    bu = tk.Button(windowborr, text='借阅', font=('Arial', 12), width=10, height=1,command = borr)
    bu.place(relx=0.38, rely=0.5)
    l3 = tk.Label(windowborr, text='查询图书信息', font=('Arial', 16))
    l3.place(relx=0.38, rely=0.3)
    l = tk.Label(windowborr, text='请输入要查询的图书名称', font=('Arial', 16))
    l.place(relx=0.38, rely=0.35)
    e1 = tk.Entry(windowborr, show=None, font=('Arial', 14))
    e1.place(relx=0.38, rely=0.4)
    # 查询逻辑处理
    def search():
        name = e1.get()
        bname='《' + name + '》'
        alist = sql.execute("SELECT * FROM books WHERE bname='" + bname + "'")
        f = '999'
        for row in alist:
            f = row[0]
        if (f == '999'):
            tk.messagebox.showinfo('警告', '该图书不存在')
        else:
            lb1.delete(0, END)
            blist = sql.execute("SELECT * FROM books WHERE bname='" + bname + "'")
            for row in blist:
                book = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + row[3] + ' ' + str(row[4])[0:20]
                lb1.insert(END, book)

    budel = tk.Button(windowborr, text='查询', font=('Arial', 12), width=10, height=1, command=search)
    budel.place(relx=0.38, rely=0.45)
    windowborr.mainloop()
#用户还书界面
def windowreturn():
    windowretu = tk.Tk()
    windowretu.title('归还图书')
    windowretu.geometry('600x700')
    '''显示用户所借的图书模块'''
    alist = sql.execute("SELECT * FROM control WHERE username='"+ un +"' AND conkind='借阅' AND constate='1'")
    lb1 = Listbox(windowretu, width='500', font=('Arial', 12))
    lb1.pack()
    for row in alist:
        bbook = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + row[3]+ ' ' + str(row[4])[0:19]
        lb1.insert(END, bbook)
    #还书逻辑处理
    def retu():
        s = lb1.get(ACTIVE)
        s=str(s)
        s=s.split(' ')
        id = s[0]
        s=s[3]
        print(id)
        alist = sql.execute("SELECT value,bname FROM books WHERE bname='" + s + "'")
        for row in alist:
            ff=row[0]
            bbname = row[1]
        if (ff <= 0):
            tk.messagebox.showinfo('提示', '抱歉,该书已经被借光啦QAQ')
        else:
            ff = ff + 1
            d='0'
            sql.execute("UPDATE books SET value=? WHERE bname=?", (ff, s))
            sql.execute("UPDATE control SET constate=? WHERE conid=?", (d, id))
            conn.commit()
            time = datetime.datetime.now()
            conkind = '归还'
            sql.execute("INSERT INTO control (username, conkind, conbook,contime) VALUES (?,?,?,?)",(un, conkind, bbname, time))
            conn.commit()
            tk.messagebox.showinfo('提示', '还书成功')
    budel = tk.Button(windowretu, text='还书', font=('Arial', 12), width=10, height=1, command=retu)
    budel.pack()
    windowretu.mainloop()

#借书记录界面
def windowbrr():
    windowbr = tk.Tk()
    windowbr.title('记录')
    windowbr.geometry('500x300')
    '''显示用户所借的图书模块'''
    alist = sql.execute("SELECT * FROM control ")
    lb1 = Listbox(windowbr, width='500', font=('Arial', 12))
    lb1.pack()
    for row in alist:
        bbook = str(row[0]) + ' ' + row[1] + ' ' + row[2] + ' ' + row[3] + ' ' + str(row[4])[0:19]
        lb1.insert(END, bbook)
    windowbr.mainloop()
#管理员主界面
def windowmain():
    windowlog.destroy()
    windowma = tk.Tk()
    windowma.title('图书管理系统-管理员')
    windowma.geometry('500x200')
    l1 = tk.Label(windowma, text='图书管理', font=('Arial', 10))
    l1.grid(row=0, column=0)
    bu1 = tk.Button(windowma, text='添加图书', font=('Arial', 12), width=10, height=1, command=windowaddbook)
    bu1.grid(row = 1,column=0)
    bu2 = tk.Button(windowma, text='删除图书', font=('Arial', 12), width=10, height=1, command=windowdellbook)
    bu2.grid(row = 1,column=2)
    bu3 = tk.Button(windowma, text='修改与查询图书', font=('Arial', 12), width=15, height=1, command=windoweditbook)
    bu3.grid(row = 1,column=1)


    l2 = tk.Label(windowma, text='用户管理', font=('Arial', 10))
    l2.grid(row=2, column=0)
    bu22 = tk.Button(windowma, text='删除用户', font=('Arial', 12), width=10, height=1, command=windowdelluser)
    bu22.grid(row=3, column=0)
    bu33 = tk.Button(windowma, text='修改与查询用户', font=('Arial', 12), width=15, height=1, command =windowedituser)
    bu33.grid(row=3, column=1)

    l3 = tk.Label(windowma, text='借阅情况', font=('Arial', 10))
    l3.grid(row=4, column=0)
    bu222 = tk.Button(windowma, text='借阅记录', font=('Arial', 12), width=10, height=1,command = windowbrr)
    bu222.grid(row=5, column=0)

    windowma.mainloop()
#学生主界面
def windowsain():
    windowlog.destroy()
    windowsa = tk.Tk()
    windowsa.title('图书管理系统-普通用户')
    windowsa.geometry('500x150')
    l1 = tk.Label(windowsa, text='业务选择', font=('Arial', 10))
    l1.grid(row=0, column=0)
    bu1 = tk.Button(windowsa, text='借阅图书', font=('Arial', 12), width=10, height=1, command = windowborrow)
    bu1.grid(row = 1,column=0)
    bu2 = tk.Button(windowsa, text='归还图书', font=('Arial', 12), width=10, height=1, command = windowreturn)
    bu2.grid(row = 1,column=1)
    windowsa.mainloop()
#注册界面
def windowregister():
    windowre = tk.Tk()
    windowre.title('图书管理系统-普通用户')
    windowre.geometry('500x200')
    l1 = tk.Label(windowre, text='填写注册信息', font=('Arial', 10))
    l1.grid(row=0, column=0)
    lab1 = tk.Label(windowre, text='用户名', font=('Arial', 12), width=10, height=1)
    ent1 = tk.Entry(windowre,show=None,font=('Arial', 12))
    lab1.grid(row=1, column=0)
    ent1.grid(row=1,column=1)
    lab2 = tk.Label(windowre, text='密 码', font=('Arial', 12), width=10, height=1)
    ent2 = tk.Entry(windowre, show='*', font=('Arial', 12))
    lab2.grid(row=2, column=0)
    ent2.grid(row=2, column=1)
    # 注册逻辑处理
    def register():
        username = ent1.get()
        password = ent2.get()
        if(checkuser.get()==1):
            identity=0
        else:
            identity=1
        if (username == '' or password == ''):
            tk.messagebox.showinfo('警告', '填写不完整')
        else:
            allist = sql.execute("SELECT * FROM users WHERE username='" + username + "' and password='" + password + "'")
            f = 999
            for row in allist:
                f = row[0]
            if (f != 999):
                tk.messagebox.showinfo('警告', '该用户名已被占用')
            else:
                sql.execute("INSERT INTO users (username,password,identity) VALUES(?,?,?)",(username,password,identity))
                conn.commit()
                tk.messagebox.showinfo('提示','注册成功')
                windowre.destroy()

    #存放按钮状态
    checkuser = IntVar(windowre)
    checkadmin = IntVar(windowre)
    Checkbutton(windowre, text="普通用户",variable=checkuser).grid(row=3,column=0)
    Checkbutton(windowre, text="管理员",variable = checkadmin).grid(row=3,column=1)
    bu = tk.Button(windowre,text="注册", font=('Arial', 12), width=10, height=1,command=register)
    bu.grid(row=4,column=1)
    windowre.mainloop()

#登录界面
windowlog = tk.Tk()
def windowlogin():
    windowlog.title('图书管理系统')
    windowlog.geometry('1920x1080')
    l = tk.Label(windowlog, text='欢迎使用图书管理系统', font=('Arial', 20))
    l.place(relx = 0.4,rely=0.2)
    usernameLabel = tk.Label(windowlog, text='用户名:', font=('Arial', 12),justify = 'right')
    usernameEntry = tk.Entry(windowlog,show=None,font=('Arial', 12))
    usernameLabel.place(relx=0.4, rely=0.5)
    usernameEntry.place(relx=0.44, rely=0.501)
    passwordLabel = tk.Label(windowlog, text='密 码:', font=('Arial', 12),justify = 'right')
    passwordEntry = tk.Entry(windowlog,show='*',font=('Arial', 12))
    passwordLabel.place(relx=0.4, rely=0.55)
    passwordEntry.place(relx=0.44, rely=0.551)
    # 登录逻辑处理
    def login():
        username = usernameEntry.get()
        global un
        un = username
        password = passwordEntry.get()
        alist = sql.execute("SELECT * FROM users WHERE username='" + username + "' and password='" + password + "'")
        f=999
        for row in alist:
            f=row[3]
        if(f!=999):
            if(f==1):
                windowmain()
            else:
                windowsain()
        else:
            tk.messagebox.showinfo('提示', '用户名或密码错误')
    loginButton = tk.Button(windowlog, text="登录", font=('Arial', 12), width=10, height=1, command=login)
    loginButton.place(relx=0.44, rely=0.6)
    registButton = tk.Button(windowlog, text="注册", font=('Arial', 12), width=10, height=1, command=windowregister)
    registButton.place(relx=0.44, rely=0.65)
    windowlog.mainloop()

windowlogin()

0 条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注