Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5保留QLineEdit或其他Qt小部件中的十六进制字符_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python PyQt5保留QLineEdit或其他Qt小部件中的十六进制字符

Python PyQt5保留QLineEdit或其他Qt小部件中的十六进制字符,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,在Python3中,如果我需要将不可打印的字符放入字符串或字节字符串中,我可以使用 mystring = '\x00\x01' mybytestring = b'\x00\x01' 其中,\x00对应于ASCII 0,\x01是ASCII 1 >>> mystring = '\x00\x01' >>> print(mystring) ☺ >>> ord(mystring[0]) 0 >>> ord(mystri

在Python3中,如果我需要将不可打印的字符放入字符串或字节字符串中,我可以使用

mystring     =  '\x00\x01'
mybytestring = b'\x00\x01'
其中,
\x00
对应于ASCII 0,
\x01
是ASCII 1

>>> mystring = '\x00\x01'
>>> print(mystring)
 ☺
>>> ord(mystring[0])
0
>>> ord(mystring[1])
1
>>> mybytestring = b'\x00\x01'
>>> mybytestring[0]
0
>>> mybytestring[1]
1
如果我试图通过从QLineEdit抓取文本来实现这一点,则前斜杠似乎会被转义,并且我无法找到一个好方法来“取消显示”它们。最小PyQt示例:

from PyQt5.QtWidgets import (QWidget, QApplication, QLineEdit)
import sys

class Example(QWidget):    
    def __init__(self):
        super().__init__()

        self.myedit = QLineEdit(self)
        self.myedit.move(10,10)
        self.myedit.returnPressed.connect(self.on_myedit_returnPressed)
        self.setGeometry(500, 500, 200, 50)
        self.show()

    def on_myedit_returnPressed(self):
        text = self.myedit.text()
        print('text: ', text)
        for i in range(len(text)):
            print(text[i])

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

控制台输出为:

文本:\x00\x01 \ x 0 0 \ x 0 1. 因此,它的行为就好像我输入了一个字符串
'\\x00\\x01'
,并跳过了正斜杠


我正在尝试制作一个串行监视器应用程序,在这个应用程序中,我可以通过串行端口向Arduino发送字节。但我无法将这些字节输入到Qt输入小部件中。

您可以这样使用它:

text = self.myedit.text().encode().decode('unicode-escape')

希望对你有用

太棒了。我永远也猜不到这个。谢谢,谢谢。很高兴能帮你^^。用这种方式解码是一种享受。但是,当您通过串行端口发送
text
时,您需要使用“拉丁1”重新编码,例如
ser.write(text.encode('latin1'))