如何从 QPlainTextEdit 正确获取 Unicode 文本输入? [复制]
Posted
技术标签:
【中文标题】如何从 QPlainTextEdit 正确获取 Unicode 文本输入? [复制]【英文标题】:How to correctly get Unicode text input from QPlainTextEdit? [duplicate] 【发布时间】:2018-10-15 09:22:01 【问题描述】:刚刚运行应用程序,我在屏幕上的 QPlainTextEdit 区域得到了正确的结果:
但是当点击按钮Start Simulation
并使用QPlainTextEdit.toPlainText()
恢复输入时,输出无效:
def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText()
print( "%s", textEdit )
此外,当尝试从图像中检索 Delta
δ 时,会抛出此错误:
Traceback (most recent call last):
File "main.py", line 57, in handle_first_input_text
print( "%s", textEdit )
File "F:\Python\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u03b4' in position 11: character maps to <undefined>
如何从QPlainTextEdit
正确检索输入?这是错误的最小代码,只需点击 Start Simulation
并查找控制台输出。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
def main():
app = QtWidgets.QApplication(sys.argv)
programWindow = ProgramWindow()
programWindow.show()
sys.exit(app.exec_())
class ProgramWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setup_main_window()
self.first_input_text()
self.set_window_layout()
def setup_main_window(self):
self.resize( 800, 600 )
self.centralwidget = QWidget()
self.setCentralWidget( self.centralwidget )
def first_input_text(self):
self.textEditWidget1 = QPlainTextEdit( self )
self.startSimulationButton1 = QPushButton( "Start Simulation" )
self.textEditWidget1.document().setPlainText( " # Writing δ some ç é ã õ")
self.startSimulationButton1.clicked.connect( self.handle_first_input_text )
verticalInnerLayout = QVBoxLayout()
verticalInnerLayout.addWidget( self.startSimulationButton1 )
horizontalInnerLayout = QHBoxLayout()
horizontalInnerLayout.addLayout( verticalInnerLayout )
horizontalInnerLayout.addWidget( self.textEditWidget1 )
self.groupBox1 = QGroupBox( "First Group" )
self.groupBox1.setLayout( horizontalInnerLayout )
def set_window_layout(self):
main_vertical_layout = QVBoxLayout( self.centralwidget )
main_vertical_layout.addWidget( self.groupBox1 )
def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText()
print( "%s", textEdit )
if __name__ == "__main__":
main()
回答
这个问题不是UnicodeEncodeError: 'charmap' codec can't encode characters的重复问题
另外,下面的这些答案都不是这个问题的答案。
要解决这个问题,我需要添加
.encode("utf-8").decode('cp1252')
不仅如此
.encode("utf-8")
这是固定版本:
def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText().encode("utf-8").decode('cp1252')
print( "%s", textEdit )
完整代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
def main():
app = QtWidgets.QApplication(sys.argv)
programWindow = ProgramWindow()
programWindow.show()
sys.exit(app.exec_())
class ProgramWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setup_main_window()
self.first_input_text()
self.set_window_layout()
def setup_main_window(self):
self.resize( 800, 600 )
self.centralwidget = QWidget()
self.setCentralWidget( self.centralwidget )
def first_input_text(self):
self.textEditWidget1 = QPlainTextEdit( self )
self.startSimulationButton1 = QPushButton( "Start Simulation" )
self.textEditWidget1.document().setPlainText( " # Writing δ some ç é ã õ")
self.startSimulationButton1.clicked.connect( self.handle_first_input_text )
verticalInnerLayout = QVBoxLayout()
verticalInnerLayout.addWidget( self.startSimulationButton1 )
horizontalInnerLayout = QHBoxLayout()
horizontalInnerLayout.addLayout( verticalInnerLayout )
horizontalInnerLayout.addWidget( self.textEditWidget1 )
self.groupBox1 = QGroupBox( "First Group" )
self.groupBox1.setLayout( horizontalInnerLayout )
def set_window_layout(self):
main_vertical_layout = QVBoxLayout( self.centralwidget )
main_vertical_layout.addWidget( self.groupBox1 )
def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText().encode("utf-8").decode('cp1252')
print( "%s", textEdit )
if __name__ == "__main__":
main()
【问题讨论】:
【参考方案1】:试试看:
import sys
import os
from PyQt5 import QtGui
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
def main():
app = QtWidgets.QApplication(sys.argv)
programWindow = ProgramWindow()
programWindow.show()
sys.exit(app.exec_())
class ProgramWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setup_main_window()
self.first_input_text()
self.set_window_layout()
def setup_main_window(self):
self.resize( 800, 600 )
self.centralwidget = QWidget()
self.setCentralWidget( self.centralwidget )
def first_input_text(self):
self.textEditWidget1 = QPlainTextEdit( self )
self.startSimulationButton1 = QPushButton( "Start Simulation" )
self.textEditWidget1.document().setPlainText( " # Writing δ some ç é ã õ")
self.startSimulationButton1.clicked.connect( self.handle_first_input_text )
verticalInnerLayout = QVBoxLayout()
verticalInnerLayout.addWidget( self.startSimulationButton1 )
horizontalInnerLayout = QHBoxLayout()
horizontalInnerLayout.addLayout( verticalInnerLayout )
horizontalInnerLayout.addWidget( self.textEditWidget1 )
self.groupBox1 = QGroupBox( "First Group" )
self.groupBox1.setLayout( horizontalInnerLayout )
def set_window_layout(self):
main_vertical_layout = QVBoxLayout( self.centralwidget )
main_vertical_layout.addWidget( self.groupBox1 )
def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText()
#print( "%s" % textEdit)
os.system('echo ' + textEdit)
if __name__ == "__main__":
main()
【讨论】:
【参考方案2】:在handle_first_input_text
函数中:
将print( "%s", textEdit )
行更改为print( "0:s".format(textEdit))
这使用 Python 3 的 .format
迷你语言。 Link to Docs
最终代码:
def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText()
print( "0:s".format(textEdit))
【讨论】:
以上是关于如何从 QPlainTextEdit 正确获取 Unicode 文本输入? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
从 QPlainTextEdit 子类化的 Qt 小部件中的代码折叠?
如何知道 QPlainTextEdit 继承类中是不是显示水平滚动条?
如何使 QPlainTextEdit 看起来像一个 .txt 文件?