单字节的字符一般在0127之间,而双字节的汉字一般在128255之间。
拓展举例:(也可以用正则表达式来实现判断)+修改错误;用正则表达式判别电话号码等;
正则表达式:http://www.360doc.com/content/12/0225/13/19147_189500941.shtml
http://www.cnblogs.com/minlecun/archive/2011/12/06/2277399.html
当然,下面的代码是有问题的:
比如输入:i我是汉字,或者我是1个汉字,答案都是错的
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(286, 567) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.formLayout = QtWidgets.QFormLayout(self.centralwidget) self.formLayout.setObjectName("formLayout") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.label_2 = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(16) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.verticalLayout.addWidget(self.label_2) self.textEdit = QtWidgets.QTextEdit(self.centralwidget) self.textEdit.setObjectName("textEdit") self.verticalLayout.addWidget(self.textEdit) self.label = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(16) self.label.setFont(font) self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.setObjectName("lineEdit") self.verticalLayout.addWidget(self.lineEdit) self.pushButton = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth()) self.pushButton.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(16) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.formLayout.setLayout(0, QtWidgets.QFormLayout.LabelRole, self.verticalLayout) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 286, 26)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QmetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label_2.setText(_translate("MainWindow", "输入")) self.label.setText(_translate("MainWindow", "判断")) self.pushButton.setText(_translate("MainWindow", "检测"))逻辑部分
from PyQt5.QtWidgets import * from judgeHZ_UI import * import sys class JDG(QMainWindow, Ui_MainWindow): def __init__(self): """ 继承布局文件,实现进程控制的功能 其中text可以用来放检测的字符串 connecter为按钮监听器 """ super(JDG, self).__init__() self.setupUi(self) self.text="" self.connecter() self.show() def is_Chinese(self,word): """ :param word: 输入字符串进行判断 :return: 返回结果为True or False """ for ch in word: if not 'u4e00' <= ch <= 'u9fff': return False return True def connecter(self): self.pushButton.clicked.connect(self._pushButton) def _pushButton(self): """ 点击后按钮的触发事件 """ self.text=str(self.textEdit.toPlainText()) if self.is_Chinese(self.text): self.lineEdit.setText(str("输入的全是汉字")) else: self.lineEdit.setText(str("输入的包含其他字符")) def main(): app = QApplication(sys.argv) w = JDG() w.show() sys.exit(app.exec_()) main()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)