学生成绩管理系统 ——grade.py年级修改、student.py学生信息管理、result.py学生成绩管理、
哈哈、、
年级设置界面

隐藏表格垂直序号
设置垂直标题即隐藏左侧123序号。

年级设置模块

数据库建表语句
学生信息管理系统附带的数据库建表语句
/* Navicat MySQL Data Transfer Source Server : mr Source Server Version : 80019 Source Host : 127.0.0.1:3306 Source Database : db_student Target Server Type : MYSQL Target Server Version : 80019 File Encoding : 65001 Date: 2020-04-21 13:54:22 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `tb_class` -- ---------------------------- DROP TABLE IF EXISTS `tb_class`; CREATE TABLE `tb_class` ( `classID` int NOT NULL, `gradeID` int NOT NULL, `className` varchar(20) DEFAULT NULL, PRIMARY KEY (`classID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_class -- ---------------------------- INSERT INTO `tb_class` VALUES ('1', '1', '一班'); INSERT INTO `tb_class` VALUES ('2', '1', '二班'); INSERT INTO `tb_class` VALUES ('3', '1', '三班'); INSERT INTO `tb_class` VALUES ('4', '2', '一班'); INSERT INTO `tb_class` VALUES ('5', '2', '二班'); -- ---------------------------- -- Table structure for `tb_examkinds` -- ---------------------------- DROP TABLE IF EXISTS `tb_examkinds`; CREATE TABLE `tb_examkinds` ( `kindID` int NOT NULL, `kindName` varchar(40) DEFAULT NULL, PRIMARY KEY (`kindID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_examkinds -- ---------------------------- INSERT INTO `tb_examkinds` VALUES ('1', '期中考试'); INSERT INTO `tb_examkinds` VALUES ('2', '期末考试'); -- ---------------------------- -- Table structure for `tb_grade` -- ---------------------------- DROP TABLE IF EXISTS `tb_grade`; CREATE TABLE `tb_grade` ( `gradeID` int NOT NULL DEFAULT '1', `gradeName` varchar(20) DEFAULT NULL, PRIMARY KEY (`gradeID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_grade -- ---------------------------- INSERT INTO `tb_grade` VALUES ('1', '初一'); INSERT INTO `tb_grade` VALUES ('2', '初二'); INSERT INTO `tb_grade` VALUES ('3', '初三'); -- ---------------------------- -- Table structure for `tb_result` -- ---------------------------- DROP TABLE IF EXISTS `tb_result`; CREATE TABLE `tb_result` ( `ID` int NOT NULL AUTO_INCREMENT, `stuID` varchar(20) DEFAULT NULL, `kindID` int DEFAULT NULL, `subID` int DEFAULT NULL, `result` double DEFAULT '0', PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_result -- ---------------------------- INSERT INTO `tb_result` VALUES ('1', 'BS0101001', '1', '1', '80'); INSERT INTO `tb_result` VALUES ('2', 'BS0101001', '1', '2', '85'); INSERT INTO `tb_result` VALUES ('3', 'BS0101001', '1', '3', '100'); INSERT INTO `tb_result` VALUES ('4', 'BS0101002', '1', '3', '100'); INSERT INTO `tb_result` VALUES ('5', 'BS0101002', '1', '1', '90'); INSERT INTO `tb_result` VALUES ('6', 'BS0101002', '1', '2', '98'); -- ---------------------------- -- Table structure for `tb_student` -- ---------------------------- DROP TABLE IF EXISTS `tb_student`; CREATE TABLE `tb_student` ( `stuID` varchar(20) NOT NULL DEFAULT 'SID00101001', `stuName` varchar(20) DEFAULT NULL, `classID` int DEFAULT NULL, `gradeID` int DEFAULT NULL, `age` int DEFAULT NULL, `sex` char(4) DEFAULT NULL, `phone` char(20) DEFAULT NULL, `address` varchar(100) DEFAULT NULL, PRIMARY KEY (`stuID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_student -- ---------------------------- INSERT INTO `tb_student` VALUES ('BS0101001', '小王', '1', '1', '20', '男', '13610780204', '北京市朝阳区'); INSERT INTO `tb_student` VALUES ('BS0101002', '小科', '1', '1', '21', '男', '1300000000', '山西省长治市'); INSERT INTO `tb_student` VALUES ('BS0102001', '小科', '2', '1', '21', '男', '1300000000', '山西省长治市'); INSERT INTO `tb_student` VALUES ('BS0201001', '王子', '4', '2', '19', '男', '15500000000', '吉林省长春市'); -- ---------------------------- -- Table structure for `tb_subject` -- ---------------------------- DROP TABLE IF EXISTS `tb_subject`; CREATE TABLE `tb_subject` ( `subID` int NOT NULL, `subName` varchar(50) DEFAULT NULL, PRIMARY KEY (`subID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_subject -- ---------------------------- INSERT INTO `tb_subject` VALUES ('1', '数学'); INSERT INTO `tb_subject` VALUES ('2', '语文'); INSERT INTO `tb_subject` VALUES ('3', '编程'); -- ---------------------------- -- Table structure for `tb_user` -- ---------------------------- DROP TABLE IF EXISTS `tb_user`; CREATE TABLE `tb_user` ( `userName` varchar(20) NOT NULL, `userPwd` varchar(50) DEFAULT NULL, PRIMARY KEY (`userName`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of tb_user -- ---------------------------- INSERT INTO `tb_user` VALUES ('mr', 'mrsoft'); INSERT INTO `tb_user` VALUES ('小科', '111'); -- ---------------------------- -- View structure for `v_classinfo` -- ---------------------------- DROP VIEW IF EXISTS `v_classinfo`; CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_classinfo` AS select `tb_class`.`classID` AS `classID`,`tb_grade`.`gradeID` AS `gradeID`,`tb_grade`.`gradeName` AS `gradeName`,`tb_class`.`className` AS `className` from (`tb_class` join `tb_grade`) where (`tb_class`.`gradeID` = `tb_grade`.`gradeID`) ; -- ---------------------------- -- View structure for `v_studentinfo` -- ---------------------------- DROP VIEW IF EXISTS `v_studentinfo`; CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_studentinfo` AS select `tb_student`.`stuID` AS `stuID`,`tb_student`.`stuName` AS `stuName`,`tb_student`.`age` AS `age`,`tb_student`.`sex` AS `sex`,`tb_student`.`phone` AS `phone`,`tb_student`.`address` AS `address`,`tb_class`.`className` AS `className`,`tb_grade`.`gradeName` AS `gradeName` from ((`tb_student` join `tb_class`) join `tb_grade`) where ((`tb_student`.`classID` = `tb_class`.`classID`) and (`tb_student`.`gradeID` = `tb_grade`.`gradeID`)) ; -- ---------------------------- -- View structure for `v_resultinfo` -- ---------------------------- DROP VIEW IF EXISTS `v_resultinfo`; CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_resultinfo` AS select `tb_result`.`ID` AS `ID`,`tb_result`.`stuID` AS `stuID`,`v_studentinfo`.`stuName` AS `stuName`,`tb_examkinds`.`kindName` AS `kindName`,`tb_subject`.`subName` AS `subName`,`v_studentinfo`.`className` AS `className`,`v_studentinfo`.`gradeName` AS `gradeName`,`tb_result`.`result` AS `result` from (((`tb_subject` join `tb_result`) join `tb_examkinds`) join `v_studentinfo`) where ((`tb_result`.`stuID` = `v_studentinfo`.`stuID`) and (`tb_result`.`kindID` = `tb_examkinds`.`kindID`) and (`tb_result`.`subID` = `tb_subject`.`subID`)) ;
组件取名,见名知意的问题
可改进的地方,组件取名,见名知意的问题。
在creator右侧给组件改名后,再生成py文件。


性别多选框添加男和女两个选项


tuple类型怎么能赋值给select语句了呢
看下边tuple在sql语句中正常查询成功了。
查询返回值



连接槽函数的时候不能add(),为什么

学生成绩管理模块

kindName后边用了中文逗号

错误:Unknown column 'kindName,subName' in 'field list',因为kindName后边用了中文逗号
把成绩记录的id也作为一列查询出来,展示在表格里

在成绩记录tableWiget里,选中编号后,把该条记录填充考试类别,年级,班级,姓名,科目,成绩。选中的编号作为在v_resultinfo里的编号,也插入到tableWidget里作为一列。

为什么要str(result)
从数据库查出来的有int,float
(1, 'BS0101001', '小王', '初一一班', '数学', '期中考试', 98.0)
<class 'int'>
1
<class 'str'>
BS0101001
<class 'str'>
小王
<class 'str'>
初一一班
<class 'str'>
数学
<class 'str'>
期中考试
<class 'float'>
98.0

第一次打开表格时多选框的默认值

第一次打开时,考试类别多选框已经有了“所有”,“期中考试”,“期末考试”,默认显示了第一个“所有”;年级也是同理;班级和年级联动,年级没选,他现在为空;学生姓名和班级联动,也为空;
为什么我用distinct,他没有用distinct
因为他是从v_studentinfo查的,我从v_resultinfo查的。
从哪个里边查好呢?从初一一班中查出所有的同学姓名,从v_studentinfo里查好。
bindName函数:
result = service.query("select distinct stuName from v_resultinfo where gradeName = %s and className = %s", curGrade,curClass)
result = service.query("select stuName from v_studentinfo where gradeName=%s and className=%s", self.cboxGrade.currentText(), self.cboxClass.currentText())
写一个python测试sql各种返回值,错误
写一个python测试sql各种返回值,错误的测试程序。sql语句执行分几种情况?
select参数多加了图中黄色括号
导致学生姓名查不出来的原因,多加了图中黄色括号。【不去掉括号,只在curClass后边加一个逗号也出错】

下边是我写的:
增 result = service.exec("insert into tb_result(stuID,kindID,subID,result) values (%s,%s,%s,%s)",(stuID, kindID, subID, score))
删 result = service.exec("DELETE FROM tb_result WHERE id = %s",id)
改 result = service.exec("UPDATE tb_result SET result = %s WHERE id = %s",(edited_score,self.select))
查
result = service.query("SELECT ID,stuID,stuName,CONCAT(gradeName,className),subName,kindName,result FROM v_resultinfo WHERE
gradeName = %s AND className = %s AND kindName = %s",curGrade,curClass,curExamKind)
实例写的,【不管几个参数,都括起来写】。【并不是,他也写的比较乱。】
result = service.exec("delete from tb_result where ID= %s", (self.select,))
为什么我用result【0】【0】,他用result[0]
因为result【0】作为一个tuple,作为参数在sql语句中仍然正常查询成功了。

subname = "语文" result = service.query("select subID from tb_subject where subName=%s", subname) print(result) # 输出结果:((2,),) subID = result[0] print(subID) # 输出结果:(2,) 用这个元组去sql语句查询,仍查出来了 result = service.query("select subName from tb_subject where subID=%s",subID) print(result) # 输出结果:(('语文',),)
python等号的判断:
java有讲过,判断地址,判断内容,equals


type edit_score <class 'str'>
type result[0][0]<class 'float'>
修改成绩,和原来一样会显示修改成功
修改成绩,和原来一样会显示修改成功,result = service.exec("UPDATE tb_result SET result = %s WHERE id = %s",(edited_score,self.select)),result等于1
在sqlyog中不是1

抛异常的使用
def edit(self): try: if self.select != "": # 判断是否选择了要修改的数据 ID = self.select # 记录要修改的编号 score = self.editResult.text() # 记录成绩 # 执行修改操作 result = service.exec("update tb_result set result=%s where ID=%s", (score, ID)) if result > 0: # 如果结果大于0,说明修改成功 self.query() # 在表格中显示最新数据 QMessageBox.information(None, '提示', '信息修改成功!', QMessageBox.Ok) except: QMessageBox.warning(None, '警告', '请先选择要修改的数据!', QMessageBox.Ok)
我的写法。self.select是在getItem中定义的,如果不点击一列,直接点击修改,程序崩溃。因为没有调用getItem,self.select不存在
def edit(self): # 一种方式:是否点击了第一列item # 第二种方式:各个框的currenttext是否符合规范 # 应该用第一种方式 # 选中了就只能改成绩这一项,修改tb_result表(stuID, kindID, subID, score) if self.select != "": # 从v_resultinfo中获取了记录的id,这个id和tb_result表里的id是一样的 # 获取成绩 edited_score = self.lineEdit_score.text() # 根据id从v_resultinfo查出考试类别,年级,班级,姓名,科目与currentTtext比较,不能改,成绩不能为空,不能和原先一样 # 这个比较没有写,也没有判空,和原来一样的成绩会修改成功 result = service.exec("UPDATE tb_result SET result = %s WHERE id = %s",(edited_score,self.select)) print("修改成功,%s",result) if result > 0: QMessageBox.information(None,"提示","信息修改成功",QMessageBox.Ok) self.query() else: QMessageBox.information(None, "提示", "请选择一条记录", QMessageBox.Ok)
getItem函数在哪里调用的,item参数谁赋值的?
从信号与槽函数理解。
def getItem(self, item):
self.tbResult.itemClicked.connect(self.getItem) # 获取选中的单元格数据
itemClickedQTableWidget 的信号处理函数声明
Qt 框架中 QTableWidget 的信号处理函数声明。在 Qt 框架中,itemClicked 是 QTableWidget 类的一个信号(signal),而非普通成员函数。信号的实现是由 Qt 框架内部完成的(通常在 C++ 层面),并不需要开发者自己实现,也不会暴露在 Python 绑定的代码中。
def itemClicked(self, item): # real signature unknown; restored from __doc__ """ itemClicked(self, item: QTableWidgetItem) [signal] """ pass
关闭按钮MainWindow.close
MainWindow是什么?close函数哪里来的
self.pushButton_exit.clicked.connect(MainWindow.close)
为什么返回值是类似矩阵的嵌套元组
查询初一一班的所有同学,有两人,小王和小科。
(('小王',), ('小科',))
一个最简洁的qt程序
一个简洁的qt程序,可以直接运行的
import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QGridLayout, QPushButton, QLineEdit) from PyQt5.QtCore import Qt class Calculator(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): # 设置窗口标题和大小 self.setWindowTitle('简易计算器') self.setGeometry(300, 300, 300, 400) # 创建中央部件和布局 central_widget = QWidget() self.setCentralWidget(central_widget) main_layout = QVBoxLayout(central_widget) # 创建显示框 self.display = QLineEdit() self.display.setAlignment(Qt.AlignRight) self.display.setReadOnly(True) self.display.setStyleSheet("font-size: 24px; padding: 10px;") main_layout.addWidget(self.display) # 创建按钮布局 button_layout = QGridLayout() # 按钮文本和位置 buttons = [ ('7', 0, 0), ('8', 0, 1), ('9', 0, 2), ('/', 0, 3), ('4', 1, 0), ('5', 1, 1), ('6', 1, 2), ('*', 1, 3), ('1', 2, 0), ('2', 2, 1), ('3', 2, 2), ('-', 2, 3), ('0', 3, 0), ('.', 3, 1), ('=', 3, 2), ('+', 3, 3), ('C', 4, 0), ('←', 4, 1) ] # 创建按钮并添加到布局 for text, row, col in buttons: button = QPushButton(text) button.setStyleSheet("font-size: 18px; padding: 15px;") button.clicked.connect(lambda checked, t=text: self.on_button_click(t)) button_layout.addWidget(button, row, col) main_layout.addLayout(button_layout) # 显示窗口 self.show() def on_button_click(self, text): """处理按钮点击事件""" if text == '=': try: # 计算表达式结果 result = str(eval(self.display.text())) self.display.setText(result) except Exception as e: self.display.setText("错误") elif text == 'C': # 清除显示 self.display.clear() elif text == '←': # 删除最后一个字符 current = self.display.text() self.display.setText(current[:-1]) else: # 添加数字或运算符 self.display.setText(self.display.text() + text) if __name__ == '__main__': # 创建应用实例 app = QApplication(sys.argv) # 创建并显示计算器窗口 calculator = Calculator() # 进入应用主循环 sys.exit(app.exec_())


浙公网安备 33010602011771号