PyQt5小应用之检测中文字数

# -*- coding: utf-8 -*-
'''
Author: LQD
QQ: 743701947
Date: 2020/06/20
'''
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit, QLineEdit, QLabel
from PyQt5.QtGui import QFont
from PyQt5.QtCore import pyqtSignal, QThread
import sys
import time


class CheckWord(QMainWindow):

    def __init__(self):
        super(CheckWord, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.resize(800, 600)
        self.label_file_path = QLabel(self)
        self.label_file_path.setText('文件路径:')
        self.label_file_path.setGeometry(20, 20, 120, 50)
        self.label_file_path.setFont(QFont('Roman times', 16))
        self.lineEdit_file_path = QLineEdit(self)
        self.lineEdit_file_path.setGeometry(145, 20, 500, 50)
        self.lineEdit_file_path.setFont(QFont('Times New Roman', 16))
        self.label_word_num = QLabel(self)
        self.label_word_num.setText('中文字数:')
        self.label_word_num.setFont(QFont('Roman times', 16))
        self.label_word_num.setGeometry(20, 75, 120, 50)
        self.label_word_num_count = QLabel(self)
        self.label_word_num_count.setText('0')
        self.label_word_num_count.setFont(QFont('Times New Roman', 16))
        self.label_word_num_count.setGeometry(145, 75, 100, 50)
        self.btn_start_count_word = QPushButton(self)
        self.btn_start_count_word.setText('开始')
        self.btn_start_count_word.setFont(QFont('Roman times', 16))
        self.btn_start_count_word.setGeometry(20, 130, 100, 50)

        self.textEdit_word = QTextEdit(self)
        self.textEdit_word.setFont(QFont('Times New Roman', 16))
        self.textEdit_word.setGeometry(20, 190, 500, 400)
        self.label_tE_word = QLabel(self)
        self.label_tE_word.setText('当前中文文字数:')
        self.label_tE_word.setFont(QFont('Roman times', 16))
        self.label_tE_word.setGeometry(525, 190, 200, 50)
        self.label_tE_word_count = QLabel(self)
        self.label_tE_word_count.setText('0')
        self.label_tE_word_count.setFont(QFont('Times New Roman', 16))
        self.label_tE_word_count.setGeometry(525, 245, 100, 50)

        self.btn_start_count_word.clicked.connect(self.slot_btn_start_count_word)

        self._thread = CWthread()
        self._thread.start()
        self._thread.trigger.connect(self.real_time_word_count)

    def slot_btn_start_count_word(self):
        try:
            path = self.lineEdit_file_path.text()
            f = open(path, 'r', encoding='utf-8')
            data = f.read()
            f.close()
            n = 0
            for i in range(len(data)):
                if '\u4e00' <= data[i] <= '\u9fff':
                    n += 1
            self.label_word_num_count.setText(str(n))
        except Exception as e:
            pass

    def real_time_word_count(self, msg):
        if self.textEdit_word.toPlainText() != '':
            data = self.textEdit_word.toPlainText()
            n = 0
            for i in range(len(data)):
                if '\u4e00' <= data[i] <= '\u9fff':
                    n += 1
            self.label_tE_word_count.setText(str(n))
        else:
            self.label_tE_word_count.setText('0')


class CWthread(QThread):
    trigger = pyqtSignal(str)

    def __init__(self):
        super(CWthread, self).__init__()

    def run(self):
        while True:
            self.trigger.emit(str(6))
            time.sleep(0.1)


def _run():
    app = QApplication(sys.argv)
    w = CheckWord()
    w.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    _run()

 

你可能感兴趣的:(pyqt5)