首頁>技術>

定義一個函式獲取圖形介面輸入的內容並將輸入的中文轉為Unicode後顯示到圖形介面

 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 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)) # 轉為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"))
執行

65

Python

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 資料治理之資料脫敏技術研究