文章插图
【为Python图形界面添加自定义函数功能】最终效果
定义一个函数获取图形界面输入的内容并将输入的中文转为Unicode后显示到图形界面
def to_unicode(self):# 获取中文文本框内容value_text=self.textEdit.toPlainText()print('输入的中文内容为:{}'.format(value_text))# 转为Unicodeunicode_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.pyimport sysfrom 中文转Unicode import Ui_MainWindowfrom PyQt5.QtWidgets import QApplication,QMainWindowif __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, QtWidgetsclass 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))# 转为Unicodeunicode_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.translateMainWindow.setWindowTitle(_translate("MainWindow", "中文转Unicode"))self.groupBox.setTitle(_translate("MainWindow", "中文"))self.pushButton.setText(_translate("MainWindow", "中文转Unicode"))self.groupBox_2.setTitle(_translate("MainWindow", "Unicode"))
运行 文章插图
执行程序
推荐阅读
- 三国魏蜀吴最后谁完成了统一中国 蜀汉和东吴为什么不联手灭曹
- 从数据库到可视化性能,5个大数据分析工具测评,python只排倒数
- 皇帝陪葬妃子为什么腿是劈开的 古代陪葬的妃子腿为什么是分开的
- 为什么现在认为蜀汉是正统 三国演义为什么以蜀汉为正统
- 为什么法国的黑人那么多
- 霍山黄大茶可降脂减肥,霍山黄大茶正式认定为国家地理标志维护产品
- 平面直角坐标系右下方为第几象限?
- 服用一次紧急避孕药有那些危害
- 沙漠里的死骆驼为什么不能碰
- 老白茶的市场怎么样,为什么云南能出杯好白茶