Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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
PythonQtlineEdit奇怪的编码_Python_Encoding_Pyqt_Qlineedit - Fatal编程技术网

PythonQtlineEdit奇怪的编码

PythonQtlineEdit奇怪的编码,python,encoding,pyqt,qlineedit,Python,Encoding,Pyqt,Qlineedit,我有一个让我困惑的情况。我的界面中有一个QLineEdit,所以当我用以下文本填充它时 将其添加到源标题: #!/usr/bin/env python # -*- coding: utf-8 -*- 您可以阅读此线程: 在阿拉姆语字符串中,将“u”添加到上述Python字符串中,以便在unicode模式下使用,如下所示: u' ܐܪܡܝܐ‎ ' text = u'ܐܪܡܝܐ‎' testQLineEdit = QtGui.QLineEdit(self) testQLineEdit.setTe

我有一个让我困惑的情况。我的界面中有一个QLineEdit,所以当我用以下文本填充它时
  • 将其添加到源标题:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    您可以阅读此线程:

  • 在阿拉姆语字符串中,将“u”添加到上述Python字符串中,以便在unicode模式下使用,如下所示:

    u' ܐܪܡܝܐ‎ '
    
    text = u'ܐܪܡܝܐ‎'
    testQLineEdit = QtGui.QLineEdit(self)
    testQLineEdit.setText(text)
    print testQLineEdit.text()
    

  • 完成代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtCore, QtGui
    class exampleQMainWindow (QtGui.QMainWindow):
        def __init__ (self):
            super(exampleQMainWindow, self).__init__()
            testQLineEdit = QtGui.QLineEdit(self)
            testQLineEdit.setText(u'ܐܪܡܝܐ‎')
            print testQLineEdit.text()
            self.setCentralWidget(testQLineEdit)
    
    app = QtGui.QApplication([])
    window = exampleQMainWindow()
    window.show()
    sys.exit(app.exec_())
    
    您可以从其他地方获取变量,如下所示:

    u' ܐܪܡܝܐ‎ '
    
    text = u'ܐܪܡܝܐ‎'
    testQLineEdit = QtGui.QLineEdit(self)
    testQLineEdit.setText(text)
    print testQLineEdit.text()
    
    要强制字符串为Unicode,可以使用
    Unicode(字符串)

    这是返回文本的输出的测试类型:

    >> print type(unicode(testQLineEdit.text()))
    <type 'unicode'>
    
    >> print type(testQLineEdit.text())
    <class 'PyQt4.QtCore.QString'>
    
    >> print type(testQLineEdit.text().toUtf8())
    <class 'PyQt4.QtCore.QByteArray'>
    
    打印类型(unicode(testQLineEdit.text()) >>打印类型(testQLineEdit.text()) >>打印类型(testQLineEdit.text().toUtf8()) 所有条件都可以在控制台中打印。

    我所要做的就是

    print unicode(testQLineEdit.text())
    

    谢谢,我已经有了源标题,但是对于你的答案2,如果我有text='ܐܪܡܝܐ,怎么做‎' testQLineEdit.setText(utext)u text给出错误,我不能像你说的那样直接放入字符串,因为它作为变量来自其他地方请在声明变量中使用“u”,如下所示;text=u'ܐܪܡܝܐ‎'; testQLineEdit.setText(utext);对不起,我表达错了,我实际上没有声明变量var=u'ܐܪܡܝܐ‎' 我从另一个方法var=testQLineEdit.text()获取文本。创建字符串后,如何添加u?类似于var2=u+var或var2=u+str(var)的东西实际上,如果您添加源头编码:utf-8,它可以读取您的阿拉姆字符。如果无法读取,您可以像这样强制Unicode字符串中的字符串;var=unicode(testQLineEdit.text());非常感谢,但不幸的是,它不起作用,在QLineEdit中填充字符串之前,字符串打印良好,在填充QLineEdit并从QLineEdit中提取后,字符串在翻译过程中丢失。此外,QLineEdit不尊重文本方向(从右到左)