好久不写微博了,最近抱着linux乱啃。这不,老师催着交进制转换和ASC编码的作业,程序网上海了去了,就是自己太长时间没写程序,手疼的痒痒,所以今天自己写一次。其实啊,进制转化什么的,python都有内置函数,我也懒得自己敲,真正想解决的就是用户从文本框输入审查的问题。
正所谓,没一个用户是正常的!就像二进制转十进制的问题,你能指望用户输入的全部都是丨和0吗?(没错,那不是1,那也不是| 那是丨(拼音:gun))对吧,这样的非法字符串直接丢给python内置函数,无疑就是game over。所以我增加了输入审查。
敲黑板,划重点:
软件功能:有三个文本框,对应二进制,十进制,十六进制。任意一个文本框内容改变,其他文本框也改变。就是一个进制转换工具。
1. 输入审查位置
我在两种情况下增加了输入审查机制。
1. 文本框内容被修改时。
2.用户触发运算机制时(就是将文本框字符串传递给其他函数时)。
2.输入审查内容
根据不同的需要,有不同的输入审查内容。我从简到繁来讲解下。
1.二进制输入审查
我们都知道,二进制只有0和1两个字符。我们从槽函数获取到的是字符串,只要审查它是不是只有0和1两 个字符就行。
1. 对字符串审查的函数
'审查st字符串中是否都是二进制字符' def check_two(st): a=['1', '0'] #创建一个列表,存放二进制字符 for x in st: #对字符串st进行遍历 if x not in a: #如果字符串st中的元素有不在a列表中的 return False #则返回错误 return True #经过遍历,未返回错误,则返回正确
2. 对不合法字符进行删除
如果用户输入了一个不合法的字符,我们就将二进制文本框的最后一个字符删掉。这样的话,对用户来说,他在字符串末尾输入了一个不合法字符,结果文本框内容根本没变化。(如果输入一串不合法字符,或是在合法字符前输入的话,就会循环触发槽函数,从后向前删除字符,直到删除的只剩合法字符为止!例如:原来是111000111,输入了一个2,变成:1110002111,触发槽函数,最后变成:111000)
@pyqtSlot(str) def on_two_textChanged(self, p0): """ 当二进制文本框内容发生改变时,触发这个槽函数 """ if self.two.text() != "" : #如果二进制文本框内容是空的话,则触发else(主要是针对用户输入了,自己又删空,导致十进制和十六进制文本框不能为空的状态) if check_two(self.two.text()): #检查字符串内是否全是二进制字符,否则触发自我修改机制 t=str(int(self.two.text(),2)) #以下是计算,输出的内容 s=hex(int(t)) self.ten.setText(t) self.six.setText(s[2:]) else: self.two.setText(self.two.text()[:-1]) #将文本框内容的最后一个字符删掉 else: #清空十进制和十六进制(six)的内容 self.ten.setText('') self.six.setText('')
2.十进制输入审查
和二进制同理,只不过审查函数数组a变一下。十六进制一样的啦
def check_ten(st): a=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] for x in st: if x not in a: return False return True
3.对ASCII字符进行审查
1.审查函数
如果输入的不是ASCII的字符的话则返回错误,否则则返回正确。主要借用了ord(c)函数,作用是把字符转化为对应的十 进制数字,我们知道,ASCII表字符从0-127所以对应数字大于128的肯定不是ASCII字符,所以一个遍历就可以判断,再调用。上面所写的修改函数,就可以让用户输不进去非ASCII字符了。
'审查字符串str中是不是全是ASCII的字符' def check_asc(str): for x in str: #遍历字符串str if ord(x)>128: #ord(c)是将字符c转化为对应的十进制数字,如果这个数字大于128则不是ASCii表字符 return False return True
下面附程序全部界面
ps: 我就是天域至尊
附全部源码:
源码工程文件包及打包好的应用程序下载地址:
窗口布局代码:Ui_first.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:\Users\Liang\Desktop\新建文件夹\frist.ui' # # Created by: PyQt5 UI code generator 5.9.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(564, 492) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.formLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.formLayoutWidget.setGeometry(QtCore.QRect(10, 30, 261, 118)) self.formLayoutWidget.setObjectName("formLayoutWidget") self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget) self.formLayout.setContentsMargins(0, 0, 0, 0) self.formLayout.setObjectName("formLayout") self.label = QtWidgets.QLabel(self.formLayoutWidget) self.label.setObjectName("label") self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label) self.ten = QtWidgets.QLineEdit(self.formLayoutWidget) self.ten.setObjectName("ten") self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.ten) self.label_2 = QtWidgets.QLabel(self.formLayoutWidget) self.label_2.setObjectName("label_2") self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2) self.two = QtWidgets.QLineEdit(self.formLayoutWidget) self.two.setObjectName("two") self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.two) self.label_3 = QtWidgets.QLabel(self.formLayoutWidget) self.label_3.setObjectName("label_3") self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3) self.six = QtWidgets.QLineEdit(self.formLayoutWidget) self.six.setObjectName("six") self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.six) self.cleck_num = QtWidgets.QPushButton(self.formLayoutWidget) self.cleck_num.setObjectName("cleck_num") self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.cleck_num) spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.formLayout.setItem(4, QtWidgets.QFormLayout.FieldRole, spacerItem) self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 180, 471, 281)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.A_answer = QtWidgets.QTextBrowser(self.gridLayoutWidget) self.A_answer.setObjectName("A_answer") self.gridLayout.addWidget(self.A_answer, 1, 1, 1, 1) spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem1, 1, 2, 1, 1) spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem2, 1, 0, 1, 1) self.B_answer = QtWidgets.QTextBrowser(self.gridLayoutWidget) self.B_answer.setObjectName("B_answer") self.gridLayout.addWidget(self.B_answer, 1, 3, 1, 1) spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem3, 1, 4, 1, 1) self.clickASC = QtWidgets.QPushButton(self.gridLayoutWidget) self.clickASC.setObjectName("clickASC") self.gridLayout.addWidget(self.clickASC, 2, 3, 1, 1) spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem4, 2, 2, 1, 1) self.gridLayout_2 = QtWidgets.QGridLayout() self.gridLayout_2.setObjectName("gridLayout_2") self.label_4 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_4.setObjectName("label_4") self.gridLayout_2.addWidget(self.label_4, 0, 3, 1, 1) self.label_5 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_5.setObjectName("label_5") self.gridLayout_2.addWidget(self.label_5, 0, 1, 1, 1) self.A_end = QtWidgets.QLineEdit(self.gridLayoutWidget) self.A_end.setObjectName("A_end") self.gridLayout_2.addWidget(self.A_end, 1, 3, 1, 1) self.A_first = QtWidgets.QLineEdit(self.gridLayoutWidget) self.A_first.setObjectName("A_first") self.gridLayout_2.addWidget(self.A_first, 1, 1, 1, 1) spacerItem5 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_2.addItem(spacerItem5, 2, 1, 1, 1) spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_2.addItem(spacerItem6, 0, 0, 1, 1) spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_2.addItem(spacerItem7, 0, 4, 1, 1) spacerItem8 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_2.addItem(spacerItem8, 0, 2, 1, 1) self.A_begin = QtWidgets.QPushButton(self.gridLayoutWidget) self.A_begin.setObjectName("A_begin") self.gridLayout_2.addWidget(self.A_begin, 2, 3, 1, 1) self.gridLayout.addLayout(self.gridLayout_2, 0, 1, 1, 1) self.gridLayout_3 = QtWidgets.QGridLayout() self.gridLayout_3.setObjectName("gridLayout_3") spacerItem9 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout_3.addItem(spacerItem9, 2, 1, 1, 1) self.label_6 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_6.setObjectName("label_6") self.gridLayout_3.addWidget(self.label_6, 0, 3, 1, 1) self.label_7 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_7.setObjectName("label_7") self.gridLayout_3.addWidget(self.label_7, 0, 1, 1, 1) self.B_frist = QtWidgets.QLineEdit(self.gridLayoutWidget) self.B_frist.setObjectName("B_frist") self.gridLayout_3.addWidget(self.B_frist, 1, 1, 1, 1) spacerItem10 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_3.addItem(spacerItem10, 0, 0, 1, 1) self.B_end = QtWidgets.QLineEdit(self.gridLayoutWidget) self.B_end.setObjectName("B_end") self.gridLayout_3.addWidget(self.B_end, 1, 3, 1, 1) spacerItem11 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_3.addItem(spacerItem11, 0, 4, 1, 1) spacerItem12 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.gridLayout_3.addItem(spacerItem12, 0, 2, 1, 1) self.B_begin = QtWidgets.QPushButton(self.gridLayoutWidget) self.B_begin.setObjectName("B_begin") self.gridLayout_3.addWidget(self.B_begin, 2, 3, 1, 1) self.gridLayout.addLayout(self.gridLayout_3, 0, 3, 1, 1) spacerItem13 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.gridLayout.addItem(spacerItem13, 3, 1, 1, 1) self.label_8 = QtWidgets.QLabel(self.centralwidget) self.label_8.setGeometry(QtCore.QRect(10, 10, 151, 16)) font = QtGui.QFont() font.setFamily("新宋体") font.setPointSize(12) self.label_8.setFont(font) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(self.centralwidget) self.label_9.setGeometry(QtCore.QRect(20, 150, 161, 21)) font = QtGui.QFont() font.setFamily("新宋体") font.setPointSize(12) self.label_9.setFont(font) self.label_9.setObjectName("label_9") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(490, 430, 62, 19)) self.pushButton_3.setObjectName("pushButton_3") self.label_10 = QtWidgets.QLabel(self.centralwidget) self.label_10.setGeometry(QtCore.QRect(450, 460, 101, 16)) self.label_10.setObjectName("label_10") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) self.pushButton_3.clicked.connect(MainWindow.close) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "十进制")) self.label_2.setText(_translate("MainWindow", "二进制")) self.label_3.setText(_translate("MainWindow", "十六进制")) self.cleck_num.setText(_translate("MainWindow", "清零")) self.clickASC.setText(_translate("MainWindow", "清零")) self.label_4.setText(_translate("MainWindow", "A终止位置")) self.label_5.setText(_translate("MainWindow", "A起始位置")) self.A_begin.setText(_translate("MainWindow", "A计算")) self.label_6.setText(_translate("MainWindow", "B终止位置")) self.label_7.setText(_translate("MainWindow", "B起始位置")) self.B_begin.setText(_translate("MainWindow", "B计算")) self.label_8.setText(_translate("MainWindow", "进制转换工具")) self.label_9.setText(_translate("MainWindow", "ASC码字符比较工具")) self.pushButton_3.setText(_translate("MainWindow", "关闭")) self.label_10.setText(_translate("MainWindow", "power by 天域至尊")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
槽函数及功能实现代码:first.py
# -*- coding: utf-8 -*- """ Module implementing MainWindow. """ from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QMainWindow from Ui_frist import Ui_MainWindow from PyQt5 import QtWidgets def check_ten(st): a=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] for x in st: if x not in a: return False return True '审查st字符串中是否都是二进制字符' def check_two(st): a=['1', '0'] #创建一个列表,存放二进制字符 for x in st: #对字符串st进行遍历 if x not in a: #如果字符串st中的元素有不在a列表中的 return False #则返回错误 return True #经过遍历,未返回错误,则返回正确 def check_six(st): a=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'] for x in st: if x not in a: return False return True '审查字符串str中是不是全是ASCII的字符' def check_asc(str): for x in str: #遍历字符串str if ord(x)>128: #ord(c)是将字符c转化为对应的十进制数字,如果这个数字大于128则不是ASCii表字符 return False return True class MainWindow(QMainWindow, Ui_MainWindow): """ Class documentation goes here. """ def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget @type QWidget """ super(MainWindow, self).__init__(parent) self.setupUi(self) @pyqtSlot(str) def on_ten_textChanged(self, p0): """ Slot documentation goes here. @param p0 DESCRIPTION @type str """ if self.ten.text() != "": if check_ten(self.ten.text()): self.two.setText(bin(int(self.ten.text()))[2:]) self.six.setText(hex(int(self.ten.text()))[2:]) else: self.ten.setText(self.ten.text()[:-1]) else: self.two.setText('') self.six.setText('') @pyqtSlot(str) def on_two_textChanged(self, p0): """ 当二进制文本框内容发生改变时,触发这个槽函数 """ if self.two.text() != "" : #如果二进制文本框内容是空的话,则触发else(主要是针对用户输入了,自己又删空,导致十进制和十六进制文本框不能为空的状态) if check_two(self.two.text()): #检查字符串内是否全是二进制字符,否则触发自我修改机制 t=str(int(self.two.text(),2)) #以下是计算,输出的内容 s=hex(int(t)) self.ten.setText(t) self.six.setText(s[2:]) else: self.two.setText(self.two.text()[:-1]) #将文本框内容的最后一个字符删掉 else: #清空十进制和十六进制(six)的内容 self.ten.setText('') self.six.setText('') @pyqtSlot(str) def on_six_textChanged(self, p0): """ Slot documentation goes here. @param p0 DESCRIPTION @type str """ if self.six.text() != "": if check_six(self.six.text()): t=int(self.six.text(), 16) tw=bin(int(t)) self.two.setText(str(tw)[2:]) self.ten.setText(str(t)) else: self.six.setText(self.six.text()[:-1]) else: self.two.setText('') self.ten.setText('') @pyqtSlot() def on_A_begin_clicked(self): """ Slot documentation goes here. """ if len(self.A_first.text())==1 and len(self.A_end.text())==1 and check_asc(self.A_first.text()) and check_asc(self.A_end.text()): if ord(self.A_first.text()[0])>ord(self.A_end.text()[0]): f=ord(self.A_end.text()[0]) e=ord(self.A_first.text()[0]) else: f=ord(self.A_first.text()[0]) e=ord(self.A_end.text()[0]) self.A_answer.append('字符'+' '+'编码') while f<=e: self.A_answer.append(chr(f)+' '+str(f)) f=f+1 else: self.A_answer.setText("错误!您输入的A数据有误!") @pyqtSlot() def on_cleck_num_clicked(self): """ Slot documentation goes here. """ self.two.setText('') self.ten.setText('') self.six.setText('') @pyqtSlot() def on_clickASC_clicked(self): """ Slot documentation goes here. """ self.A_answer.setText("") self.A_first.setText("") self.A_end.setText("") self.B_end.setText("") self.B_frist.setText("") self.B_answer.setText("") @pyqtSlot() def on_B_begin_clicked(self): """ Slot documentation goes here. """ if len(self.B_frist.text())==1 and len(self.B_end.text())==1 and check_asc(self.B_frist.text()) and check_asc(self.B_end.text()): if ord(self.B_frist.text()[0])>ord(self.B_end.text()[0]): f=ord(self.B_end.text()[0]) e=ord(self.B_frist.text()[0]) else: f=ord(self.B_frist.text()[0]) e=ord(self.B_end.text()[0]) self.B_answer.append('字符'+' '+'编码') while f<=e: self.B_answer.append(chr(f)+' '+str(f)) f=f+1 else: self.B_answer.setText("错误!您输入的B数据有误!") @pyqtSlot(str) def on_A_end_textChanged(self, p0): """ Slot documentation goes here. @param p0 DESCRIPTION @type str """ if check_asc(self.A_end.text()): if len(self.A_end.text())>1: self.A_end.setText(self.A_end.text()[-1]) else: self.A_end.setText('') @pyqtSlot(str) def on_A_first_textChanged(self, p0): """ Slot documentation goes here. @param p0 DESCRIPTION @type str """ if check_asc(self.A_first.text()): if len(self.A_first.text())>1: self.A_first.setText(self.A_first.text()[-1]) else: self.A_first.setText('') @pyqtSlot(str) def on_B_frist_textChanged(self, p0): """ Slot documentation goes here. @param p0 DESCRIPTION @type str """ if check_asc(self.B_frist.text()): if len(self.B_frist.text())>1: self.B_frist.setText(self.B_frist.text()[-1]) else: self.B_frist.setText('') @pyqtSlot(str) def on_B_end_textChanged(self, p0): """ Slot documentation goes here. @param p0 DESCRIPTION @type str """ if check_asc(self.B_end.text()): if len(self.B_end.text())>1: self.B_end.setText(self.B_end.text()[-1]) else: self.B_end.setText('') if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) ui = MainWindow() ui.show() sys.exit(app.exec_())