python学生管理系统笔记(+增删改查,但不存入数据库或文件中)

原本的基础上+增删改查,但不存入数据库或文件中,就是数据只在一次运行的页面中进行增删改查,但是重新运行不会有之前的数据,因为没有更新到json或者数据库中。

1.LoginPage.py

import tkinter as tk
from tkinter import messagebox
from db import db
from MainPage import MainPage


class LoginPage:
    def __init__(self, master):
        self.root = master
        self.root.geometry('300x180')
        self.root.title('登录页')

        self.username = tk.StringVar()
        self.password = tk.StringVar()
        #  实现翻页
        self.page = tk.Frame(root)
        self.page.pack()

        # 布局
        tk.Label(self.page).grid(row=0, column=0)

        tk.Label(self.page, text='用户:').grid(row=1, column=1)
        tk.Entry(self.page, textvariable=self.username).grid(row=1, column=2)

        tk.Label(self.page, text='密码:').grid(row=2, column=1, pady=10)
        tk.Entry(self.page, textvariable=self.password).grid(row=2, column=2)

        tk.Button(self.page, text='登录', command=self.login).grid(row=3, column=1, pady=10)
        tk.Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=2)

    # 获取登录信息
    def login(self):
        name = self.username.get()
        pwd = self.password.get()
        flag, msg = db.check_login(name, pwd)
        if flag:
            self.page.destroy()  # 销毁当前页面
            MainPage(self.root)  # 跳转第二个页面
        else:
            messagebox.showwarning(title='警告', message=msg)


if __name__ == '__main__':
    root = tk.Tk()
    LoginPage(master=root)
    root.mainloop()  # 将整个页面显示出来

 

2.MainPage.py

import tkinter as tk
from views import AboutFrame, ChangeFrame, InsertFrame, SearchFram, DeleteFrame


class MainPage:
    # 如果master不写成 master: tk.TK 的话,会没有提示,写了也没有影响,只是说明这个master是一个tk的对象
    def __init__(self, master: tk.Tk):
        self.root = master
        self.root.title('学生信息管理系统 v0.0.1')
        self.root.geometry('600x400')
        self.creat_page()

    # 菜单栏
    def creat_page(self):
        """
        可以直接封装成一个views对象,用的时候直接调用即可
        # '修改'页面
        self.change_frame = tk.Frame(self.root)
        tk.Label(self.change_frame, text='修改页面').pack()

        # '关于'页面布局
        self.about_frame = tk.Frame(self.root)  # 绑定到root对象中
        tk.Label(self.about_frame, text='关于作品:关于本作品由tkinter制作').pack()
        tk.Label(self.about_frame, text='关于制作:关于本作品由tkinter制作').pack()
        """
        """封装后直接调用"""
        self.insert_frame = InsertFrame(self.root)
        self.delete_frame = DeleteFrame(self.root)
        self.chang_frame = ChangeFrame(self.root)
        self.search_frame = SearchFram(self.root)
        self.about_frame = AboutFrame(self.root)

        # 记得要绑定时间command
        menubar = tk.Menu(self.root)
        menubar.add_command(label='录入', command=self.show_insert)
        menubar.add_command(label='查询', command=self.show_search)
        menubar.add_command(label='删除', command=self.show_delete)
        menubar.add_command(label='修改', command=self.show_change)
        menubar.add_command(label='关于', command=self.show_about)
        self.root['menu'] = menubar

    def show_insert(self):
        self.insert_frame.pack()

        self.delete_frame.pack_forget()
        self.chang_frame.pack_forget()
        self.search_frame.pack_forget()
        self.about_frame.pack_forget()

    def show_delete(self):
        self.delete_frame.pack()
        self.insert_frame.pack_forget()

        self.chang_frame.pack_forget()
        self.search_frame.pack_forget()
        self.about_frame.pack_forget()

    def show_change(self):
        self.chang_frame.pack()
        self.insert_frame.pack_forget()
        self.delete_frame.pack_forget()

        self.search_frame.pack_forget()
        self.about_frame.pack_forget()

    def show_search(self):
        self.search_frame.pack()
        self.insert_frame.pack_forget()
        self.delete_frame.pack_forget()
        self.chang_frame.pack_forget()

        self.about_frame.pack_forget()

    def show_about(self):
        self.about_frame.pack()  # 当点击关于时,进行布局的显示
        self.insert_frame.pack_forget()
        self.delete_frame.pack_forget()
        self.chang_frame.pack_forget()
        self.search_frame.pack_forget()


if __name__ == '__main__':
    root = tk.Tk()
    MainPage(root)
    root.mainloop()

 

3.views.py

"""
封装MainPage中的frame对象
"""
import tkinter as tk
from tkinter import ttk
from db import db


class AboutFrame(tk.Frame):
    def __init__(self, root):
        # 继承
        super().__init__(root)
        tk.Label(self, text='关于作品:关于本作品由tkinter制作').pack()
        tk.Label(self, text='关于制作:关于本作品由tkinter制作').pack()


class ChangeFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.name = tk.StringVar()
        self.math = tk.StringVar()
        self.chinese = tk.StringVar()
        self.english = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)
        tk.Label(self, text='姓 名:').grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.name).grid(row=1, column=2, pady=10)
        tk.Label(self, text='语 文:').grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.chinese).grid(row=2, column=2, pady=10)
        tk.Label(self, text='数 学:').grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.math).grid(row=3, column=2, pady=10)
        tk.Label(self, text='英 语:').grid(row=4, column=1, pady=10)
        tk.Entry(self, textvariable=self.english).grid(row=4, column=2, pady=10)

        tk.Button(self, text='查询', command=self.search_user).grid(row=5, column=1, pady=10)
        tk.Button(self, text='修改', command=self.change_user).grid(row=5, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10, stick=tk.E)

    def search_user(self):
        # info有可能是信息,有可能是字符串
        flag, info = db.search_by_username(self.name.get())
        if flag:
            # 查询到数据
            self.name.set(info['name'])
            self.chinese.set(info['chinese'])
            self.math.set(info['math'])
            self.english.set(info['english'])
        else:
            # 查询不到学生信息
            self.status.set(info)

    def change_user(self):
        # 获取数据
        stu = {"name": self.name.get(), "chinese": self.chinese.get(),
               "math": self.math.get(), "english": self.english.get()}
        # 把旧数据清空
        self.name.set('')
        self.chinese.set('')
        self.math.set('')
        self.english.set('')
        db.update(stu)
        self.status.set('更新成功')




class InsertFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.name = tk.StringVar()
        self.math = tk.StringVar()
        self.chinese = tk.StringVar()
        self.english = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self).grid(row=0, pady=10)
        tk.Label(self, text='姓 名:').grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.name).grid(row=1, column=2, pady=10)
        tk.Label(self, text='语 文:').grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.chinese).grid(row=2, column=2, pady=10)
        tk.Label(self, text='数 学:').grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.math).grid(row=3, column=2, pady=10)
        tk.Label(self, text='英 语:').grid(row=4, column=1, pady=10)
        tk.Entry(self, textvariable=self.english).grid(row=4, column=2, pady=10)

        tk.Button(self, text='录入', command=self.recode_info).grid(row=5, column=2, pady=10, sticky=tk.E)
        tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10, stick=tk.E)

    def recode_info(self):
        stu = {"name": self.name.get(), "math": self.math.get(), "chinese": self.chinese.get(), "english": self.english.get()}
        # 将之前录入的数据清空
        self.name.set('')
        self.chinese.set('')
        self.math.set('')
        self.english.set('')

        db.insert(stu)
        self.status.set('录入学生信息成功')

class SearchFram(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.table_view = tk.Frame()
        self.table_view.pack()
        self.create_page()

    # 布局表格
    def create_page(self):
        columns = ("name", "chinese", "math", "english")
        # columns_values = ("姓名", "语文", "数学", "英语")
        self.tree_view = ttk.Treeview(self, show='headings', columns=columns)
        self.tree_view.column('name', width=80, anchor='center')
        self.tree_view.column('chinese', width=80, anchor='center')
        self.tree_view.column('math', width=80, anchor='center')
        self.tree_view.column('english', width=80, anchor='center')
        self.tree_view.heading('name', text='姓名')
        self.tree_view.heading('chinese', text='语文')
        self.tree_view.heading('math', text='数学')
        self.tree_view.heading('english', text='英语')
        self.tree_view.pack(fill=tk.BOTH, expand=True)
        # 显示查询到的数据
        self.show_data_frame()

        tk.Button(self, text="刷新数据", command=self.show_data_frame).pack(anchor=tk.E, pady=5)

    # 展示数据
    def show_data_frame(self):
        # 删除旧原来显示的数据: 遍历每一个节点,然后进行删除
        for _ in map(self.tree_view.delete, self.tree_view.get_children('')):
            pass

        students = db.all()
        index = 0
        for stu in students:
            self.tree_view.insert('', index + 1, values=(
                stu['name'], stu['chinese'], stu['math'], stu['english']
            ))


class DeleteFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.username = tk.StringVar()
        self.status = tk.StringVar()
        tk.Label(self, text='根据名字删除数据').pack()
        tk.Entry(self, textvariable=self.username).pack()
        tk.Button(self, text='删除', command=self.delete).pack()
        tk.Label(self, textvariable=self.status).pack()

    def delete(self):
        username = self.username.get()
        flag, msg = db.delete_by_username(username)
        self.status.set(msg)

 

4.db.py

"""模型层"""
import json


class MysqlDatabases:
    def __init__(self):
        # 加载数据:读取文件
        with open('users.json', mode='r', encoding='utf-8') as f:
            text = f.read()
        self.users = json.loads(text)
        self.students = json.loads(open('students.json', mode='r', encoding='utf-8').read())

    def check_login(self, username, password):
        for user in self.users:
            if username == user['username']:
                if password == user['password']:
                    return True, '登录成功'
                else:
                    return False, '登录失败,密码错误'
            else:
                return False, '登录失败,用户不存在'

    def all(self):
        return self.students

    def insert(self, student):
        self.students.append(student)

    def delete_by_username(self, name):
        for student in self.students:
            if student['name'] == name:
                self.students.remove(student)
                return True, '删除成功'
        return False, f'{name} 用户不存在'

    def search_by_username(self, name):
        for student in self.students:
            if student['name'] == name:
                return True, student
        return False, f'{name} 用户不存在'

    def update(self, stu):
        for student in self.students:
            if student['name'] == stu['name']:
                student.update(stu)
                return True, f'{stu["name"]} 用户数据修改成功'
        return False, f'{stu["name"]} 用户不存在'

db = MysqlDatabases()
if __name__ == '__main__':
    print(db.check_login('admin', '123456'))
    # print(db.search_by_username('你好'))
    print(db.all())

 

5.students.json

[
  {"name":  "张三", "math": "65", "chinese":  "75", "english":  "100"},
  {"name":  "李四", "math": "75", "chinese":  "95", "english":  "91"},
  {"name":  "王五", "math": "99", "chinese":  "85", "english":  "83"}
]

 

6.users.json

[
  {"username":  "admin", "password":  "123456"}
]

 

7.效果

 录入新的信息后

 

 

 

 

 

 更新小明的数据

 

posted on 2023-05-16 20:56  201812  阅读(260)  评论(0编辑  收藏  举报