学生管理系统

import tkinter as tk
from tkinter import messagebox
from view import *

class MainPage:
#def init(self,master: tk.Tk): 完全可以不加 加上只是可以补全代码 不加需要手敲
def init(self,master: tk.Tk):
#root = tk.Tk()
self.root = master
self.root.geometry('600x400')
self.root.title('学生信息管理系统 V1.0.0')
self.create_page()
def create_page(self):
self.about_frame = AboutFrame(self.root)
#self.about_frame = tk.Frame(self.root)
#tk.Label(self.about_frame, text = '关于作品:本品由tkinter 制作').pack()
#tk.Label(self.about_frame, text = '关于作者:群里最弱的萌新').pack()
#tk.Label(self.about_frame, text = '版权所有:B站号-群里最弱的萌新').pack()

    self.change_frame = ChangeFrame(self.root)
    self.insert_frame = InsertFrame(self.root)
    self.delete_frame = DeleteFrame(self.root)
    self.search_frame = SearchFrame(self.root)
    #tk.Label(self.change_frame, text = '修改页面').pack()
    
    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_about(self):
    self.about_frame.pack()#######
    self.change_frame.pack_forget()
    self.insert_frame.pack_forget()
    self.delete_frame.pack_forget()
    self.search_frame.pack_forget()
def show_change(self):    
    self.about_frame.pack_forget()
    self.change_frame.pack()#######
    self.insert_frame.pack_forget()
    self.delete_frame.pack_forget()
    self.search_frame.pack_forget()
    #create_page()
def show_delete(self):
    self.about_frame.pack_forget()
    self.change_frame.pack_forget()
    self.insert_frame.pack_forget()
    self.delete_frame.pack()#######
    self.search_frame.pack_forget()
def show_search(self):
    self.about_frame.pack_forget()
    self.change_frame.pack_forget()
    self.insert_frame.pack_forget()
    self.delete_frame.pack_forget()
    self.search_frame.pack()###########
def show_insert(self):
    self.about_frame.pack_forget()
    self.change_frame.pack_forget()
    self.insert_frame.pack()########
    self.delete_frame.pack_forget()
    self.search_frame.pack_forget()

if name == 'main':
root = tk.Tk()

MainPage(master = root)
#LoginPage()
root.mainloop()

"""
tk.Label(self.about_frame, text = '关于作品:本品由tkinter 制作').pack()
tk.Label(self.about_frame, text = '关于作者:群里最弱的大白菜').pack()
tk.Label(self.about_frame, text = '版权所有:B站号-群里最弱的大白菜').pack()
"""
import tkinter as tk

-------from tkinter import ttk

from db import db

import tkinter.ttk as ttk

from tkinter import *

from tkinter.ttk import *

from tkinter import *

from tkinter.ttk import *

--from tkinter import *

--from tkinter.ttk import *

--

--root = Tk()

--

--tree1 = Treeview(root, columns=('qy', 'dz'))

class AboutFrame(tk.Frame):
def init(self,root):
super().init(root)
tk.Label(self, text = '关于作品:本品由tkinter 制作').pack()
tk.Label(self, text = '关于作者:群里最弱的大白菜').pack()
tk.Label(self, text = '版权所有:B站号-群里最弱的大白菜').pack()

class ChangeFrame(tk.Frame):
def init(self,root):
super().init(root)
#tk.Label(self, text = '修改页面').pack()

    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.math).grid(row=2,column = 2 , pady=10)
    
    tk.Label(self,text = '语 文:').grid(row=3,column = 1 , pady=10)
    tk.Entry(self, textvariable=self.chinese).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.Entry(self, textvariable=self.name).grid(row=4,column = 2 , pady=10)
    tk.Button(self,text = '查询',command = self.search_usr).grid(row=5,column=1,pady=10)
    tk.Button(self,text = '修改',command = self.change_usr).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_usr(self):
    #pass
    flag , info = db.search_by_username(self.name.get())
    if flag:
        self.name.set(info['name'])
        self.math.set(info['math'])
        self.chinese.set(info['chinese'])
        self.english.set(info['english'])
        self.status.set('数据查询成功')
    else:
        self.status.set(info)
def change_usr(self):
    #pass
    stu = {"name": self.name.get(),"math": self.math.get(),
            "chinese": self.chinese.get(),"english": self.english.get()}
    self.name.set('') 
    self.math.set('')
    self.chinese.set('')
    self.english.set('')
    print(stu)
    db.update_user(stu)
    self.status.set('修改数据成功')

class InsertFrame(tk.Frame):
def init(self,root):
super().init(root)
#tk.Label(self, text = '插入页面').pack()

    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.math).grid(row=2,column = 2 , pady=10)
    
    tk.Label(self,text = '语 文:').grid(row=3,column = 1 , pady=10)
    tk.Entry(self, textvariable=self.chinese).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.Entry(self, textvariable=self.name).grid(row=4,column = 2 , pady=10)

    tk.Button(self,text = '录入',command = self.record_info).grid(row=5,column=2,pady=10)
    tk.Label(self,textvariable = self.status).grid(row=6,column=2,pady=10,stick = tk.E)
    
def record_info(self):
    stu = {"name": self.name.get(),"math": self.math.get(),
            "chinese": self.chinese.get(),"english": self.english.get()}
    self.name.set('') 
    self.math.set('')
    self.chinese.set('')
    self.english.set('')
    print(stu)
    db.insert(stu)
    self.status.set('获取数据成功')

class DeleteFrame(tk.Frame):
def init(self,root):
super().init(root)
#tk.Label(self, text = '删除页面').pack()
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.del_info).pack()
#tk.Label(self, text = self.status).pack()
tk.Label(self, textvariable = self.status).pack()
def del_info(self):
username = self.username.get()
flag , message = db.delete_by_username(username)
self.status.set(message)
#pass

class SearchFrame(tk.Frame):
def init(self,root):
super().init(root)
tk.Label(self, text = '查询页面').pack()
self.table_view = tk.Frame()
self.table_view.pack()

    self.create_page()

##--tv=ttk.Treeview(root,columns=ac,show='headings',
##--             height=5)   ############################傻逼了   一定要匹配大小写检查  不然死都不知道咋死的

#reate_page() takes 1 positional argument but 2 were given
def create_page(self):
    columns = ("name","chinese","math","english")
    columns_values = ("姓名","语文","数学","英语")
    #self.tree_view = ttk.TreeView(self,show = 'headings',columns=columns)
    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
    #self.students = db.all()
    students = db.all()
    index = 0
    for stu in students:
        print(stu)
        self.tree_view.insert('',index+1, values = (
            stu['name'],stu['math'],stu['chinese'],stu['english']
        ))

=============================================================

import json

class MysqlDatabases:
def init(self):
with open('users.json',encoding='utf-8') as f:
text = f.read()
#self.users = text.username
#self.password = text.password
self.users = json.loads(text)
self.students = json.loads(open('students.json',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, '登陆失败,密码错误'
            
    return False , '登陆失败,用户不存在'            
    #return self.users
    
def all(self):
    return self.students
    
def insert(self,student):
    self.students.append(student)        
def delete_by_username(self,username):
    for student in self.students:
        #print(student)
        if student['name'] == username:
            self.students.remove(student)
            return True, f'{username}用户删除成功'
    return False, f'{username}用户不存在'    

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

def update_user(self,stu):
    for student in self.students:
        #print(student)
        if student['name'] == stu['name']:
            #self.students.remove(student)
            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.all())
#
#print(db.delete_by_username('admin6'))

print(db.search_by_username('admin1'))

你可能感兴趣的:(学生管理系统)