我无法从主程序更改GUI表中的值。我收到错误
属性错误:'功能' object没有属性" 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 code
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))
很抱歉,最后一部分格式不正确
答案 0 :(得分:2)
您尝试使用commandTable
方法拨打setupUi()
。这是一种方法,所以它没有任何这样的属性。而是尝试:
self.ui.commandTable.setItem(0,0, QtGui.QTableWidgetItem('1'))
在您的MyForm.__init__()
方法中。包含对元素的引用的对象是self.ui
,而不是它的方法。