0

I'm really new to Python and its usage.

I created a Python UI with PyQt.

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1000, 692)
        MainWindow.setStyleSheet(_fromUtf8(""))
        ...
        ...

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "x", None))
        self.pushButton_2.setText(_translate("MainWindow", "x", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

It works well when I run the command python main1.py Now I want the seperate UI class from the file to use multiple UI classes with a single __main__

I removed all parts except the Ui_MainWindow class and created a new main.py file such that:

from main1 import *
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

I can reach the class and create object. But when I run the python main.py command, I see this error.

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from main1 import *
  File "/home/x/main1.py", line 38
SyntaxError: Non-ASCII character '\xc4' in file /home/x/main1.py on line 38, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

I think the problem is wrong usage of the _fromUtf8 variable. How can it be solved or how should I approach the problem? Thank you.

4

1 回答 1

0

这是您在文件 main1.py 中的编辑器中使用的编码问题。

有一个c4大于 128 的十六进制字符,因此不能使用 ascii。

尝试一些将以下编码行之一放入 main1.py 的第一行之一。(我推荐最后一个)并尝试找出该文件的编码并设置它。

      # -*- coding: latin-1 -*-


      #!/usr/bin/python
      # -*- coding: iso-8859-15 -*-


      #!/usr/bin/python
      # -*- coding: utf-8 -*-
      # I recommend utf8!

http://www.python.org/dev/peps/pep-0263/

于 2013-05-03T11:10:28.080 回答