如何正确地从QPlainTextEdit获取Unicode文本输入? [英] How to correctly get Unicode text input from QPlainTextEdit?

查看:80
本文介绍了如何正确地从QPlainTextEdit获取Unicode文本输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅运行应用程序,我就在屏幕上的QPlainTextEdit区域上获得了正确的结果:





但是,当单击按钮开始仿真并使用 QPlainTextEdit.toPlainText()恢复输入时, ),输出将无效:

  def handle_first_input_text(self):
textEdit = self.textEditWidget1.toPlainText()
print(%s,textEdit)

< a href = https://i.stack.imgur.com/XXDVk.png rel = nofollow noreferrer>



此外,当尝试从以下位置获取 Delta δ时,图像,抛出此错误:

 跟踪(最近一次调用最近):
文件 main.p y,第57行,在handle_first_input_text
print(%s,textEdit)
文件 F:\Python\lib\encodings\cp1252.py,第19行,编码
返回codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: charmap编解码器无法对位置11处的字符 \u03b4进行编码:字符映射为< undefined> ;

如何从 QPlainTextEdit ?这是错误的最小代码,只需点击开始仿真并查找控制台输出。

 #! / usr / bin / env python 
#-*-编码:utf-8-*-

import sys

from PyQt5 import QtGui
from从PyQt5.QtGui导入PyQt5的QtWidgets

*从PyQt5.QtCore导入的
*从PyQt5.QtWidgets导入的
*

def main():
app = QtWidgets.QApplication(sys.argv)
programWindow = ProgramWindow()

programWindow.show()
sys.exit(app.exec_())


类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.startSimulationBu tton1 = QPushButton(开始模拟)

self.textEditWidget1.document()。setPlainText(#编写δçéção)
self.startSimulationButton1.clicked.connect( self.handle_first_input_text)

verticalInnerLayout = QVBoxLayout()
verticalInnerLayout.addWidget(self.startSimulationButton1)

horizo​​ntalInnerLayout = QHBoxLayout()
horizo​​ntalInnerLayout.addLayout( verticalInnerLayout)
horizo​​ntalInnerLayout.addWidget(self.textEditWidget1)

self.groupBox1 = QGroupBox( First Group)
self.groupBox1.setLayout(horizo​​ntalInnerLayout)

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()






答案



此问题不是


Just running the application I got the correct results on the QPlainTextEdit area on the screen:

But when clicking on the button Start Simulation and recovering the input from it with QPlainTextEdit.toPlainText(), the output goes invalid:

def handle_first_input_text(self):
    textEdit = self.textEditWidget1.toPlainText()
    print( "%s", textEdit )

Also, when trying to retrieve the Delta δ from the image, this error is throw up:

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>

How can I retrieve the input the correctly from the QPlainTextEdit? This is the minimal code for the error, just hit Start Simulation and look for the console output.

#! /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()


Answer

This question is not a duplicate of UnicodeEncodeError: 'charmap' codec can't encode characters

Also, none of these answers bellow are the answer to this question.

To fix this, I need to add

.encode("utf-8").decode('cp1252') 

and not only

.encode("utf-8")

This is the fixed version:

def handle_first_input_text(self):
    textEdit = self.textEditWidget1.toPlainText().encode("utf-8").decode('cp1252')
    print( "%s", textEdit )

Full code:

#! /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()

解决方案

Try it:

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()

这篇关于如何正确地从QPlainTextEdit获取Unicode文本输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆