结队成员:
林清青 201421123095
郑莹 201421123097
本次要求把这个程序做成 GUI(可以是 Windows PC 上的,也可以是 Mac、Linux,web,手机上的),成为一个有基本功能、一定价值的程序。
在下面的功能需求中实现两个:
1. 在作业 1 的基础上,让程序基于 GUI 界面,使得界面简洁易操作并且较为美观
2. 支持中文简体 / 繁体 / 英语三种语言,用户可自主选择
3. 程序退出时,能将用户所做题目保存在一个 txt 文件中,存为历史记录
4. 程序拥有计时的功能,每道题目都能显示时间,并能保存于文件中
1. 基本功能:
支持整数、真分数的四则运算程序,用户能够选择题目数。在用户答题后,能够判断对错,错误及显示正确答案并统计正确率。
2. 扩展功能:
说明:基础功能在作业一中已有体现,以下为此次作业为实现扩展功能的主要代码。
将 python 代码实现 ui 界面。
在 pyqt4 的文件夹里搜索找到脚本,然后将其复制到 ui 文件所在的文件夹,在配置好 python 环境的命令行窗口中转到 ui 所在的文件夹,如图输入以下命令
用于生成窗口:
- if __name__ == "__main__":
- import sys
- app = QtGui.QApplication(sys.argv)
- Form = QtGui.QWidget()
- ui = Ui_Form()
- ui.setupUi(Form)
- Form.show()
- sys.exit(app.exec_())
在此窗体上创建主键
- def setupUi(self, Form):
- Form.setObjectName(_fromUtf8("Form"))
- Form.resize(568, 291)
- self.pushButton = QtGui.QPushButton(Form)
- self.pushButton.setGeometry(QtCore.QRect(480, 180, 75, 23))
- self.pushButton.setObjectName(_fromUtf8("pushButton"))
- self.label_2 = QtGui.QLabel(Form)
- self.label_2.setGeometry(QtCore.QRect(230, 140, 54, 12))
- self.label_2.setObjectName(_fromUtf8("label_2"))
- self.label_3 = QtGui.QLabel(Form)
- self.label_3.setGeometry(QtCore.QRect(230, 190, 54, 12))
- self.label_3.setObjectName(_fromUtf8("label_3"))
- self.lineEdit = QtGui.QLineEdit(Form)
- self.lineEdit.setGeometry(QtCore.QRect(300, 130, 113, 31))
- self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
- self.pushButton_2 = QtGui.QPushButton(Form)
- self.pushButton_2.setGeometry(QtCore.QRect(50, 20, 75, 23))
- self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
- self.pushButton_3 = QtGui.QPushButton(Form)
- self.pushButton_3.setGeometry(QtCore.QRect(140, 20, 75, 23))
- self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
- self.pushButton_4 = QtGui.QPushButton(Form)
- self.pushButton_4.setGeometry(QtCore.QRect(230, 20, 75, 23))
- self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
- self.label_4 = QtGui.QLabel(Form)
- self.label_4.setGeometry(QtCore.QRect(60, 120, 141, 41))
- self.label_4.setText(_fromUtf8(""))
- self.label_4.setObjectName(_fromUtf8("label_4"))
- self.label = QtGui.QLabel(Form)
- self.label.setGeometry(QtCore.QRect(100, 120, 111, 61))
- self.label.setText(_fromUtf8(""))
- self.label.setObjectName(_fromUtf8("label"))
- self.pushButton_5 = QtGui.QPushButton(Form)
- self.pushButton_5.setGeometry(QtCore.QRect(480, 130, 75, 23))
- self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
- self.label_5 = QtGui.QLabel(Form)
- self.label_5.setGeometry(QtCore.QRect(300, 180, 121, 41))
- self.label_5.setText(_fromUtf8(""))
- self.label_5.setObjectName(_fromUtf8("label_5"))
- self.retranslateUi(Form)
- QtCore.QMetaObject.connectSlotsByName(Form)
测试第三方繁体简体转换:
- from langconv import *
- #line=('和')
- line='我'
- print (line.unicode())
- #line = line.decode ('utf-8')
- line = Converter('zh-hans').convert( line)
- #line = line.decode('utf-8')
- #line = line.encode ('utf-8')
- print (line)
部分简体和繁体转换代码:
- # -*- coding: utf-8 -*-
- # copy fom wikipedia
- zh2Hant = {
- '确': '確',
- "定": "定",
- '你': '你',
- "的": "的",
- "答": "答",
- "案": "案",
- "是": "是",
- "否": "否",
- "正": "正",
- "目": "目",
- "显": "显",
- "示": "示",
- "题": "题",
- "简": "简",
- "体": "体",
- "繁": "繁",
- "英": "英",
- "文": "文"
显示做题开始和结束时间,为了精确,取到毫级:
- import time
- t1=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
- with open('new.txt','a+') as f:
- f.write("start"+str(t1)+'\n\r')
- f.write(str(h5))
- with open('new.txt','a+') as f:
- f.write("right"+'\n\r')
- f.write("final"+str(t2)+'\n\r')
做题结束,显示正确率,并可以选择返回主界面:
- def retranslateUi(self, Form):
- Form.setWindowTitle(_translate("Form", "Form", None))
- self.pushButton.setText(_translate("Form", "显示正确率", None))
- self.commandLinkButton.setText(_translate("Form", " 返回主界面", None))
- def huhu(self):
- global rightcount
- global errorcount
- #f.write("error"+'\n\r')
- f = open("new.txt", "r")
- for line in f:
- if line.find('right')!=-1:
- rightcount=rightcount+1
- elif line.find('error')!=-1:
- errorcount=errorcount+1
- zcount=rightcount+errorcount
- if zcount!=0:
- mingzhonglv=float(rightcount)/zcount
- r=float('%.3f'%mingzhonglv)
- tt=str(float('%.3f'%mingzhonglv)*100)+'%'
- self.label.setText(tt)
文件读取和文件创建:
- with open('new.txt','w') as f:
- pass
- with open('new.txt','a+') as f:
- f.write("right"+'\n\r')
- f.write("final"+str(t4)+'\n\r')
开始界面,选择题目数
进入答题页面,进行答题:
用户可自主切换繁体和英文界面进行答题:
结束答题,显示正确率,点击可以返回初始界面:
答题结束,生产 txt 文件,显示所做题目、题目正确与否以及时间:
PSP2.1 | Personal Software Process Stages | Time (%) Senior Student(/hour) | Time (%)(/hour) |
· Planning | 计划 | 2 | 1.5 |
· Estimate | 估计这个任务需要多少时间 | 30 | 27 |
· Analysis | 需求分析 (包括学习新技术) | 5 | 6 |
· Coding Standard | 代码规范 | 1 | 1.5 |
· Design | 具体设计 | 3 | 3.5 |
· Coding | 具体编码 | 30 | 35 |
· Test | 测试(自我测试,修改代码,提交修改) | 1 | 2 |
Reporting | 报告 | 1.5 | 1 |
来源: http://www.bubuko.com/infodetail-1982794.html