0

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

enter image description here

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 )

enter image description here

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()
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

2 Answers2

1

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

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
-1

In the handle_first_input_text function:

  • Change the print( "%s", textEdit ) line to print( "{0:s}".format(textEdit))

  • This uses Python 3's .format Mini-language. Link to Docs

Final Code:

def handle_first_input_text(self):
        textEdit = self.textEditWidget1.toPlainText()
        print( "{0:s}".format(textEdit))
PgTower
  • 26
  • 5