首先运行example.py创建example.db
得到zhanghumima表和students_result表
import sqlite3
# 连接到数据库(如果数据库文件不存在,则会创建一个新的数据库文件)
conn = sqlite3.connect('example.db')
# 创建一个游标对象,用于执行SQL语句
cursor = conn.cursor()
# 创建一个表
cursor.execute('''CREATE TABLE IF NOT EXISTS zhanghumima
(username INT PRIMARY KEY NOT NULL,
password TEXT NOT NULL)''')
#查找zhanghumima表中所有的元素
cursor.execute("SELECT * FROM zhanghumima")
rows = cursor.fetchall()
if ('admin','1233') not in rows:
cursor.execute(''' insert into zhanghumima (username,password) VALUES(?,?)''', ('admin', 1233))
cursor.execute('''CREATE TABLE IF NOT EXISTS students_result
(username INT PRIMARY KEY NOT NULL,
math TEXT NOT NULL,
english TEXT NOT NULL,
chinese TEXT NOT NULL)''')
# 提交更改到数据库
conn.commit()
# 关闭数据库连接
conn.close()
之后运行 学生管理系统桌面版.py
import tkinter
import sqlite3
class loginpage:
def __init__(self, root2):
# 定义窗口对象
# 本子
self.root = root2
# 一页
# 页纸
self.login_frame = tkinter.Frame(self.root)
self.login_frame.pack()
# 设置标题
self.root.title('学生信息管理系统')
# 设置窗口大小
self.root.geometry('300x180') # 注意是小写字母x
# 可以使用StringVar变量对象来跟踪和更新文本框、标签等Tkinter控件中的值。
# 你可以通过操作StringVar对象来修改相应控件的值,或者使用它来获取控件中的当前值。
self.username = tkinter.StringVar()
self.password = tkinter.StringVar()
self.create_page()
# 显示窗口对象
self.root.mainloop()
def create_page(self):
# 设置登录注册页面
tkinter.Label(self.login_frame, width=10).grid(row=0, column=0, padx=5, pady=5)
tkinter.Label(self.login_frame, text='账户').grid(row=1, column=0, padx=5, pady=5)
tkinter.Entry(self.login_frame, textvariable=self.username).grid(row=1, column=1, padx=5, pady=5)
tkinter.Label(self.login_frame, text='密码').grid(row=2, column=0, padx=5, pady=5)
tkinter.Entry(self.login_frame, textvariable=self.password).grid(row=2, column=1, padx=5, pady=5)
tkinter.Button(self.login_frame, text='登录', command=self.check_login).grid(row=3, column=0, padx=5, pady=5)
tkinter.Button(self.login_frame, text='退出', command=self.root.quit).grid(row=3, column=1, padx=5, pady=5)
# 点击登录,校验参数
def check_login(self):
# 连接到数据库(如果数据库文件不存在,则会创建一个新的数据库文件)
conn = sqlite3.connect('example.db')
# 创建一个游标对象,用于执行SQL语句
cursor = conn.cursor()
username = self.username.get()
password = self.password.get()
cursor.execute("SELECT * FROM zhanghumima")
rows = cursor.fetchall()
for row in rows:
if username == row[0] and password == row[1]:
print("登录成功")
# 换页
self.login_frame.destroy()
# 登录成功后切换到第二个界面
Mainpage(self.root)
break
else:
tkinter.Label(self.login_frame, text='登录失败').grid(row=4, column=0, padx=10, pady=10)
break
# 提交更改到数据库
conn.commit()
# 关闭数据库连接
conn.close()
class Mainpage:
def __init__(self, root2):
self.root = root2
self.root.title('学生信息管理系统')
self.root.geometry('600x400')
self.create_page()
# 自己定义一个组件
self.insert_frame = InsertFrame(self.root)
self.search_frame = SearchFrame(self.root)
self.delete_frame = DeleteFrame(self.root)
self.about_frame = AboutFrame(self.root)
def create_page(self):
# 创建菜单主面
menu = tkinter.Menu(self.root)
menu.add_command(label='录入', command=self.show_insert_frame)
menu.add_command(label='查询', command=self.show_search_frame)
menu.add_command(label='删除', command=self.show_delete_frame)
menu.add_command(label='关于', command=self.show_about_frame)
self.root['menu'] = menu
def show_insert_frame(self):
self.insert_frame.pack()
self.search_frame.forget()
self.delete_frame.forget()
self.about_frame.forget()
def show_search_frame(self):
self.search_frame.pack()
self.insert_frame.forget()
self.delete_frame.forget()
self.about_frame.forget()
def show_delete_frame(self):
self.delete_frame.pack()
self.search_frame.forget()
self.insert_frame.forget()
self.about_frame.forget()
def show_about_frame(self):
self.delete_frame.forget()
self.search_frame.forget()
self.insert_frame.forget()
self.about_frame.pack()
# 录入
class InsertFrame(tkinter.Frame):
# 继承
def __init__(self, root):
super().__init__(master=root)
self.username = tkinter.StringVar()
self.chinese = tkinter.StringVar()
self.english = tkinter.StringVar()
self.math = tkinter.StringVar()
self.create_page()
def create_page(self):
# 设置登录注册页面
tkinter.Label(self, width=10).grid(row=0, column=0, padx=5, pady=5) # pad指填充边框
tkinter.Label(self, text='姓名').grid(row=1, column=0, padx=5, pady=5)
tkinter.Entry(self, textvariable=self.username).grid(row=1, column=1, padx=5, pady=5)
tkinter.Label(self, text='数学').grid(row=2, column=0, padx=5, pady=5)
tkinter.Entry(self, textvariable=self.math).grid(row=2, column=1, padx=5, pady=5)
tkinter.Label(self, text='英语').grid(row=3, column=0, padx=5, pady=5)
tkinter.Entry(self, textvariable=self.english).grid(row=3, column=1, padx=5, pady=5)
tkinter.Label(self, text='语文').grid(row=4, column=0, padx=5, pady=5)
tkinter.Entry(self, textvariable=self.chinese).grid(row=4, column=1, padx=5, pady=5)
tkinter.Button(self, text='录入', command=self.recode).grid(row=5, column=0, padx=5, pady=5)
def recode(self):
print("记录数据的事件被触发了")
username = self.username.get()
math = self.math.get()
english = self.english.get()
chinese = self.chinese.get()
# 连接到数据库(如果数据库文件不存在,则会创建一个新的数据库文件)
conn = sqlite3.connect('example.db')
# 创建一个游标对象,用于执行SQL语句
cursor = conn.cursor()
cursor.execute("SELECT username FROM students_result")
rows = cursor.fetchone()
tkinter.Label(self, text='正在录入中').grid(row=6, column=0)
# 设置一个定时器,在1秒后‘将文本替换为空格
root.after(1000, lambda: tkinter.Label(self, text=' ').grid(row=6, column=0))
if username in rows:
root.after(1200, lambda: tkinter.Label(self, text='录入失败').grid(row=6, column=0))
root.after(1200, lambda: tkinter.Label(self, text='你已经录入过该对象').grid(row=6, column=1))
else:
try:
cursor.execute(''' insert into students_result (username,math,english,chinese)
VALUES(?,?,?,?)''', (username, math, english, chinese))
root.after(2000, lambda: tkinter.Label(self, text='录入成功').grid(row=6, column=0))
root.after(3000, lambda: tkinter.Label(self, text=' ').grid(row=6, column=0))
except sqlite3.ProgrammingError:
root.after(1200, lambda: tkinter.Label(self, text='请输入完整信息').grid(row=6, column=0))
# 提交更改到数据库
conn.commit()
# 关闭数据库连接
conn.close()
# 查询
class SearchFrame(tkinter.Frame):
# 继承
def __init__(self, root):
super().__init__(master=root)
self.username = tkinter.StringVar()
tkinter.Button(self, text='查询所有', command=self.display_data_all).grid()
tkinter.Button(self, text='查询单个', command=self.show_search).grid()
def show_search(self):
tkinter.Label(self, text='姓名').grid(row=2, column=0)
tkinter.Entry(self, textvariable=self.username).grid(row=3, column=0)
tkinter.Button(self, text='查询', command=self.display_data).grid(row=4, column=0, padx=5, pady=5)
def display_data(self):
tT = tkinter.Text(self)
tT.grid(row=5, column=0)
xingming = self.username.get()
# 连接到数据库
conn = sqlite3.connect('example.db')
# 创建游标对象
cursor = conn.cursor()
# 执行查询,获取所有数据
cursor.execute("SELECT * FROM students_result where username = ?", (xingming,))
# 获取所有数据
rows = cursor.fetchall()
# 关闭游标和数据库连接
cursor.close()
conn.close()
# 在Text控件中显示数据
for row in rows:
tT.insert(5.0, str(row) + "\n")
def display_data_all(self):
tT = tkinter.Text(self)
tT.grid(row=5, column=0)
# 连接到数据库
conn = sqlite3.connect('example.db')
# 创建游标对象
cursor = conn.cursor()
# 执行查询,获取所有数据
cursor.execute("SELECT * FROM students_result")
# 获取所有数据
rows = cursor.fetchall()
# 关闭游标和数据库连接
cursor.close()
conn.close()
# 在Text控件中显示数据
for row in rows:
tT.insert(5.0, str(row) + "\n")
# 删除
class DeleteFrame(tkinter.Frame):
# 继承
def __init__(self, root):
super().__init__(master=root)
self.username = tkinter.StringVar()
tkinter.Button(self, text='删除所有', command=self.delete_data_all).grid(row=1, column=0)
tkinter.Button(self, text='删除单个', command=self.show_delete).grid(row=2, column=0)
def show_delete(self):
tkinter.Label(self, text='姓名').grid(row=3, column=0)
tkinter.Entry(self, textvariable=self.username).grid(row=4, column=0)
tkinter.Button(self, text='删除', command=self.delete_data).grid(row=5, column=0, padx=5, pady=5)
def delete_data(self):
xingming = self.username.get()
# 连接到数据库
conn = sqlite3.connect('example.db')
# 创建游标对象
cursor = conn.cursor()
cursor.execute('''delete from students_result where username = ? ''', (xingming,))
# 执行查询,获取所有数据
cursor.execute("SELECT * FROM students_result where username = ?", (xingming,))
# 获取所有数据
rows = cursor.fetchall()
if xingming in rows:
tkinter.Label(self, text='删除失败').grid(row=6, column=0)
else:
tkinter.Label(self, text='删除成功').grid(row=6, column=0)
# 提交更改到数据库
conn.commit()
# 关闭游标和数据库连接
cursor.close()
conn.close()
def delete_data_all(self):
# 连接到数据库
conn = sqlite3.connect('example.db')
# 创建游标对象
cursor = conn.cursor()
cursor.execute('''delete from students_result ''')
# 执行查询,获取所有数据
cursor.execute("SELECT * FROM students_result ")
# 获取所有数据
rows = cursor.fetchall()
if rows:
tkinter.Label(self, text='删除失败').grid(row=6, column=0)
else:
tkinter.Label(self, text='删除成功').grid(row=6, column=0)
# 提交更改到数据库
conn.commit()
# 关闭游标和数据库连接
cursor.close()
conn.close()
# 关于
class AboutFrame(tkinter.Frame):
def __init__(self, root):
super().__init__(master=root)
tkinter.Label(self, text='关于我们啥也没有').grid()
root = tkinter.Tk()
login_page = loginpage(root)
root.mainloop()
即可得到运行结果
登录账户:admin
密码:1233
运行结果如下: