pyside, Qt Designer, encapsulated code, and AttributeError: 'MainWindow' object has no attribute 'QtGui'

pyside, Qt Designer, encapsulated code, and AttributeError: 'MainWindow' object has no attribute 'QtGui'

我使用 Qt Designer 创建了一个 .ui 文件,然后 pyside-uic 转换为 .py 文件(ui_mainWindow.py 和 class Ui_MainWindow).我注意到不要编辑 .ui 或 .py 的警告,因为在 Qt Designer 中保存更新时,那里的任何更改都将被覆盖。所以我有自己的独立代码,应该使用 python 的 super 功能从它继承。

class MainWindow(QMainWindow, Ui_MainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    self.setupUi(self)
    self.assignWidgets()
    self.show()

我可以更新标签和响应按钮等,但我无法使用本地化翻译内容。上面class的一部分是这个函数:

def connecetSerialPushed(self):
  self.label_connected.setText(self.QtGui.QApplication.translate(self, "Connected: Yes", None, self.QtGui.QApplication.UnicodeUTF8))

如果我只做一个纯 setText"Connected: Yes" 字符串,我不会出错。但是执行该转换会导致错误:AttributeError: 'MainWindow' object has no attribute 'QtGui'。我不明白.. 我以为我继承了 Ui_MainWindow 的所有内容,包括它对 QtGui 的导入。我错过了什么?

在我单独的代码中,我做了

from PySide import QtGui

然后将翻译行更改为

self.label_connected.setText(QtGui.QApplication.translate("MainWindow", "Connected: Yes", None, QtGui.QApplication.UnicodeUTF8))

感谢 ray 消除了我的困惑。