0

So there is a lines of code that if I run it in 'script editor' it whould work fine, but if it get the same text from PySide2 it wouldn't work.

lets say you put 'a#↑' into your input. it would print 'a' and '#' but not '↑' and instead it print 'Not recognized'. this only happen in Maya and not windows. I'm confused.

# -*- coding: utf-8 -*-
from PySide2 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(165, 125, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setGeometry(QtCore.QRect(30, 10, 341, 96))
        self.plainTextEdit.setInputMethodHints(QtCore.Qt.ImhNone)
        self.plainTextEdit.setObjectName("plainTextEdit")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

        self.pushButton.clicked.connect(self.printIt)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "Test"))

    def printIt(self):
        text = self.plainTextEdit.toPlainText()
        for i in text:
            if i == "a":
                print ('a is printed')
            elif i == "#":
                print ('# is printed')
            elif i == "↑":
                print ('↑ is printed')
            else:
                print ('not recognized')


if __name__ == "__main__":
    import sys
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()

but in this form it works fine:

text = 'a#↑'
for i in text:
    if i == "a":
        print ('a is printed')
    elif i == "#":
        print ('# is printed')
    elif i == "↑":
        print ('↑ is printed')
    else:
        print ('not recognized')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
zazuvahe
  • 33
  • 7
  • 1
    what is output: `print("a#↑".encode(), self.plainTextEdit.toPlainText().encode())` in `printIt`? – eyllanesc Jul 14 '19 at 07:20
  • making it 'str or encode()' wouldn't help if you mean that. but thanks for your comment... – zazuvahe Jul 14 '19 at 08:00
  • I have never pointed out that it is the solution, but we need information since it does not use maya but if PySide2, could you answer me? – eyllanesc Jul 14 '19 at 08:02
  • excuse me, the output will be this if I don't enter anything,otherwise i get UnicodeEncodeError >>> **('a#?', '')** – zazuvahe Jul 14 '19 at 11:04
  • Okay, I understand, and what is the output of `print("a#↑", self.plainTextEdit.toPlainText())`? – eyllanesc Jul 14 '19 at 11:09
  • whould be>>> **('a#?', u'')** – zazuvahe Jul 14 '19 at 11:57
  • 1
    Interesting enough if you run `ord()` on your non gui code the arrow will return you `63`, but running it from your `printIt` method returns `8593`. `toPlainText()` must be changing it to some unexpected character. – Green Cell Jul 14 '19 at 15:10

1 Answers1

0

So turns out this is the "script editor" problem. Thanks to @zewt in Autodesk forum, he introduce a solution for this. If you import a file that contain the code instead of just running the code directly in script editor, then it works.

here is the link to his solution

zazuvahe
  • 33
  • 7