当我运行如下的演示程序时,主窗口将调用子窗口,然后子窗口将调用窗口小部件。我想得到这个小部件的一些属性,例如" messageLabel"但它会报告一个错误,即TypeError:' QLabel'对象不可调用。那么如何修改这个错误呢?谢谢,
import sys
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setWindowTitle("MainWindow Window!")
self.setGeometry(400, 400, 100, 100)
self.centerWidget = QtGui.QWidget()
self.setCentralWidget(self.centerWidget)
self.pushButton = QtGui.QPushButton("&Button")
layout = QtGui.QVBoxLayout()
layout.addWidget(self.pushButton)
self.centerWidget.setLayout(layout)
class ChildWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ChildWindow, self).__init__(parent)
#QtGui.QMainWindow.__init__(self, parent)
self.setWindowTitle("Child Window!")
self.centerWidget = Widget(parent)
self.setCentralWidget(self.centerWidget)
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__()
self.messageLabel = QtGui.QLabel()
self.messageLabel.setText("How to get this message label?")
layout = QtGui.QVBoxLayout()
layout.addWidget(self.messageLabel)
self.setLayout(layout)
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
def showChildWindow():
child_win = ChildWindow(myapp)
child_win.show()
childLabel = child_win.centerWidget.messageLabel().text().__str__().__str__() #how to call the messageLabel?
print childLabel
myapp.show()
QtCore.QObject.connect(myapp.pushButton,QtCore.SIGNAL("clicked()"),showChildWindow)
sys.exit(app.exec_())