我正在编写一些 GUI,然后我想将文本添加到 listView。现在我只想添加“你好”来弄清楚,然后我会从那里成长。
这是我的代码示例:
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(584, 461)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.lineEdit_6 = QtGui.QLineEdit(self.centralwidget)
self.lineEdit_6.setGeometry(QtCore.QRect(72, 210, 171, 20))
....
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.addEntry)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
....
def addEntry(self):
listView.Items.Add("Hello")
然后打开它:
from PyQt4 import *
import signal
import sys
from PyQt4.QtGui import QApplication, QMessageBox
from inv_window import *
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
当试图运行它时,我得到:
AttributeError: 'QMainWindow' object has no attribute 'addEntry'
关于如何纠正这个问题的任何想法?我试过谷歌搜索,但我很难确定一个解决方案