PyQt4 AttributeError: 'function' object has no attribute 'self'

PyQt4 AttributeError: 'function' object has no attribute 'self'

我无法从主程序更改 GUI table 中的值。我收到一个错误

AttributeError: 'function' object has no attribute 'commandTable'

我已经阅读并尝试了这个问题中显示的解决方案,但没有奏效。

AttributeError: 'function' object has no attribute 'self'

我应该在我的代码中更改什么才能使代码正常工作?

主要代码:

import sys
from PyQt4 import QtCore, QtGui

from GUI_Window import *


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
       QtGui.QWidget.__init__(self, parent)
       self.ui = Ui_MainWindow()
       self.ui.setupUi(self)
       self.ui.setupUi.commandTable.setItem(0,0, QtGui.QTableWidgetItem('1'))

if __name__ == "__main__":
   app = QtGui.QApplication(sys.argv)
   myapp = MyForm()
   myapp.show()
   sys.exit(app.exec_())

gui代码

from PyQt4 import QtCore, QtGui
import sys

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(821, 544)
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.gridLayout = QtGui.QGridLayout(self.centralwidget)
    self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
    self.table = QtGui.QTableWidget(self.centralwidget)
    self.table.setRowCount(0)
    self.table.setObjectName(_fromUtf8("table"))
    self.table.setColumnCount(0)
    self.gridLayout.addWidget(self.table, 0, 0, 1, 1)
    self.label = QtGui.QLabel(self.centralwidget)
    self.label.setObjectName(_fromUtf8("label"))
    self.gridLayout.addWidget(self.label, 2, 0, 1, 1)
    self.progressBar = QtGui.QProgressBar(self.centralwidget)
    self.progressBar.setProperty("value", 24)
    self.progressBar.setObjectName(_fromUtf8("progressBar"))
    self.gridLayout.addWidget(self.progressBar, 2, 1, 1, 1)
    self.commandTable = QtGui.QTableWidget(self.centralwidget)
    self.commandTable.setEnabled(True)
    self.commandTable.setAlternatingRowColors(False)
    self.commandTable.setRowCount(2)
    self.commandTable.setColumnCount(10)
    self.commandTable.setObjectName(_fromUtf8("commandTable"))
    self.commandTable.setItem(0,0, QtGui.QTableWidgetItem('hi'))
    self.commandTable.horizontalHeader().setCascadingSectionResizes(False)
    self.commandTable.horizontalHeader().setSortIndicatorShown(False)
    self.commandTable.horizontalHeader().setStretchLastSection(False)
    self.commandTable.verticalHeader().setCascadingSectionResizes(False)
    self.gridLayout.addWidget(self.commandTable, 1, 0, 1, 2)
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 821, 18))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(MainWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    MainWindow.setStatusBar(self.statusbar)
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    self.label.setText(_translate("MainWindow", "TextLabel", None))

抱歉,最后一部分格式不正确

您正在尝试在 setupUi() 方法上调用 commandTable。这是一种方法,所以它没有任何这样的 属性。而是尝试:

self.ui.commandTable.setItem(0,0, QtGui.QTableWidgetItem('1'))

在您的 MyForm.__init__() 方法中。包含对您的元素的引用的对象是 self.ui,而不是它的方法。