最终效果
def to_unicode(self):
# 获取中文文本框内容
value_text=self.textEdit.toPlainText()
print('输入的中文内容为:{}'.format(value_text))
# 转为Unicode
unicode_text=value_text.encode("unicode_escape")
print('输入内容的Unicode为:{}'.format(unicode_text))
# 通过textBrowser进行展示
# self.textBrowser.setPlainText(str(unicode_text).replace("b", ""))
self.textBrowser.setPlainText(str(unicode_text).strip("b'"))
self.pushButton.clicked.connect(self.to_unicode)# 设置按钮的功能函数
main.py
import sys
from 中文转Unicode import Ui_MainWindow
from PyQt5.QtWidgets import QApplication,QMainWindow
if __name__ == '__main__':
app=QApplication(sys.argv)
mainWindow = QMainWindow()
ui=Ui_MainWindow()
# 向主窗口上添加控件
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
中文转Unicode.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '.中文转Unicode.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# 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(437, 344)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_3.setObjectName("gridLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout_2.setObjectName("gridLayout_2")
self.textEdit = QtWidgets.QTextEdit(self.groupBox)
self.textEdit.setObjectName("textEdit")
self.gridLayout_2.addWidget(self.textEdit, 0, 0, 1, 1)
self.verticalLayout.addWidget(self.groupBox)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(218, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem1 = QtWidgets.QSpacerItem(68, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setObjectName("groupBox_2")
self.gridLayout = QtWidgets.QGridLayout(self.groupBox_2)
self.gridLayout.setObjectName("gridLayout")
self.textBrowser = QtWidgets.QTextBrowser(self.groupBox_2)
self.textBrowser.setObjectName("textBrowser")
self.gridLayout.addWidget(self.textBrowser, 0, 0, 1, 1)
self.verticalLayout.addWidget(self.groupBox_2)
self.gridLayout_3.addLayout(self.verticalLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.to_unicode)# 设置按钮的功能函数
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.textEdit, self.pushButton)
MainWindow.setTabOrder(self.pushButton, self.textBrowser)
def to_unicode(self):
# 获取中文文本框内容
value_text=self.textEdit.toPlainText()
print('输入的中文内容为:{}'.format(value_text))
# 转为Unicode
unicode_text=value_text.encode("unicode_escape")
print('输入内容的Unicode为:{}'.format(unicode_text))
# 通过textBrowser进行展示
# self.textBrowser.setPlainText(str(unicode_text).replace("b", ""))
self.textBrowser.setPlainText(str(unicode_text).strip("b'"))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "中文转Unicode"))
self.groupBox.setTitle(_translate("MainWindow", "中文"))
self.pushButton.setText(_translate("MainWindow", "中文转Unicode"))
self.groupBox_2.setTitle(_translate("MainWindow", "Unicode"))
执行程序