提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
本文涉及到的方法:
例如:python和数据库的连接,窗口的设计,以及数据库功能的实现都会有所展示。
提示:以下是本篇文章正文内容,下面案例可供参考
首先我们做学生信息管管理系统的思路是让每个使用的人能更有效率,并且提供完备的功能。
代码如下(示例):
登录界面:
import tkinter as tk #引入tk库
from tkinter import messagebox #引入tkinter模块
from db import db #引入db模块
from MinaPage import MinaPage#引入MinaPage模块
class LoginPage: #定义登录界面的类类型
def __init__(self,master):
self.root = master
self.root.geometry('300x200')
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, message = db.check_login(name, pwd)
if flag:
self.page.destroy()
MinaPage(self.root)
else:
messagebox.showwarning(title='warning', message=message)
if __name__ =='__main__':
root = tk.Tk()#设置窗口标题
LoginPage(master=root)
root.mainloop()
功能展示界面:
import tkinter as tk
from views import InsertFrame,SearchFrame,DeleteFrame,ChangeFrame,AboutFrame
class MinaPage:
def __init__(self,master:tk.Tk):
self.root = master
self.root.title('学生信息管理系统 ')
self.root.geometry('600x400')
self.create_page()
def create_page(self):
self.insert_frame = InsertFrame(self.root)
self.search_frame = SearchFrame(self.root)
self.delete_frame = DeleteFrame(self.root)
self.change_frame = ChangeFrame(self.root)
self.about_frame = AboutFrame(self.root)
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.search_frame.pack_forget()
self.delete_frame.pack_forget()
self.change_frame.pack_forget()
self.about_frame.pack_forget()
def show_search(self):
self.insert_frame.pack_forget()
self.search_frame.pack()
self.delete_frame.pack_forget()
self.change_frame.pack_forget()
self.about_frame.pack_forget()
def show_delete(self):
self.insert_frame.pack_forget()
self.search_frame.pack_forget()
self.delete_frame.pack()
self.change_frame.pack_forget()
self.about_frame.pack_forget()
def show_change(self):
self.insert_frame.pack_forget()
self.search_frame.pack_forget()
self.delete_frame.pack_forget()
self.change_frame.pack()
self.about_frame.pack_forget()
def show_about(self):
self.insert_frame.pack_forget()
self.search_frame.pack_forget()
self.delete_frame.pack_forget()
self.change_frame.pack_forget()
self.about_frame.pack()
if __name__ == '__main__':
root = tk.Tk()
MinaPage(root)
root.mainloop()
各功能代码展示:
import tkinter as tk
from tkinter import ttk
from db import db
import sqlite3
def Connect():
dbstring = 'text.db'
con = sqlite3.connect(dbstring)
cur = con.cursor()
sqlsting='create table if not exists student(name varchar primary key,math integer,chinese integer,english integer)'
cur.execute(sqlsting)
return con
class AboutFrame(tk.Frame):#关于页面的修改
def __init__(self, root):
super().__init__(root)
tk.Label(self, text='关于本作品').pack()
tk.Label(self, text='关于本思路').pack()
tk.Label(self, text='本组成员').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.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.Button(self, text='查询', command=self.search_user).grid(row=5, column=2, pady=10)
tk.Button(self, text='修改', command=self.change_user).grid(row=5, column=1, pady=10)
tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10, stick=tk.E)
def search_user(self):
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_user(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('')
db.insert(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.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.Button(self, text='录入', command=self.recode_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 recode_info(self):
stu = {"name":self.name.get(),"math":self.math.get(),
"chinese": self.chinese.get(),"english": self.english.get()}
dbinfo = Connect()
cur = dbinfo.cursor()
sql = "insert into student(name,math,chinese,english) values(?,?,?,?)"
cur.execute(sql,(self.name.get(),self.math.get(),self.chinese.get(),self.english.get()))
dbinfo.commit()
dbinfo.close()
self.name.set('')
self.math.set('')
self.chinese.set('')
self.english.set('')
db.insert(stu)
self.status.set('获取数据成功')
class SearchFrame(tk.Frame):#信息的全部展示
def __init__(self,root):
super().__init__(root)
self.table_view = tk.Frame()
self.table_view.pack()
self.create_page()
tk.Label(self,text='').pack()
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
student = db.all()
index = 0
for stu in student:
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, message = db.delete_by_username(username)
self.status.set(message)
import json
class MysqlDatabases:
def __init__(self):
self.users = json.loads(open('users.json', mode='r', encoding='utf-8').read())
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,'登录失败,密码不存在'
return False,'登录失败,用户不存在'
def all(self):
return self.students
def insert(self, student):
self.students.append(student)
def change(self, student):
self.students.append(student)
def delete_by_username(self, name):
for student in self.students:
print(student)
if student['name'] == name:
self.students.remove(student)
return True, f'{name} 删除成功'
return False, f'{name} 不存在'
def search_by_username(self, name):
for student in self.students:
if student['name'] == name:
self.students.remove(student)
return True, student
return False, f'{name} 不存在'
def update(self, stu):#修改信息的基本代码
for student in self.students:
if student['name'] == stu['name']:
student.insert(stu)#把字典里的数据更新
return True, f'{stu["name"]}用户修改成功'
return False, f'{stu["name"]} 用户不存在'
db = MysqlDatabases()
if __name__ == '__main__':
print(db.search_by_username('正心'))
存储的学生信息:
[{"name":"张三","math": "65","chinese": "75","english": "100" },
{"name":"李四","math": "65","chinese": "75","english": "95" }]
登录用户名和密码:
[{"username":"admin","password": "123"},
{"username": "杨学懂","password": "200088"}]
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。