I'm trying to call a method of a parent class from within a child class. Specifically, my parent class is a PySide.QtGui.QMainWindow object, and my child class is a PySide.QtGui.QWidget object; the latter is set to be the central widget of the former. I'm trying to connect a button within the child to a method in the parent class. This has worked for me in the past using self.parent().method_name, but it doesn't work in the example below and I don't understand why:

import sys

from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

def __init__(self):

super(MainWindow, self).__init__()

self.do_something() #sanity check

self.cw = ChildWidget()

self.setCentralWidget(self.cw)

self.show()

def do_something(self):

print 'doing something!'

class ChildWidget(QtGui.QWidget):

def __init__(self):

super(ChildWidget, self).__init__()

self.button1 = QtGui.QPushButton()

self.button1.clicked.connect(self.do_something_else)

self.button2 = QtGui.QPushButton()

self.button2.clicked.connect(self.parent().do_something)

self.layout = QtGui.QVBoxLayout()

self.layout.addWidget(self.button1)

self.layout.addWidget(self.button2)

self.setLayout(self.layout)

self.show()

def do_something_else(self):

print 'doing something else!'

def main():

app = QtGui.QApplication(sys.argv)

ex = MainWindow()

sys.exit(app.exec_())

if __name__ == '__main__':

main()

Here is the error:

self.button2.clicked.connect(self.parent().do_something)

AttributeError: 'NoneType' object has no attribute 'do_something'

解决方案

You never set your MainWindow as the parent of your ChildWidget.

So self.parent() evaluates to None and therefore has no function do_something.

Try:

import sys

from PySide import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):

def __init__(self):

super(MainWindow, self).__init__()

self.do_something() #sanity check

self.cw = ChildWidget(self)

self.setCentralWidget(self.cw)

self.show()

def do_something(self):

print 'doing something!'

class ChildWidget(QtGui.QWidget):

def __init__(self, parent):

super(ChildWidget, self).__init__(parent)

self.button1 = QtGui.QPushButton()

self.button1.clicked.connect(self.do_something_else)

self.button2 = QtGui.QPushButton()

self.button2.clicked.connect(self.parent().do_something)

self.layout = QtGui.QVBoxLayout()

self.layout.addWidget(self.button1)

self.layout.addWidget(self.button2)

self.setLayout(self.layout)

self.show()

def do_something_else(self):

print 'doing something else!'

def main():

app = QtGui.QApplication(sys.argv)

ex = MainWindow()

sys.exit(app.exec_())

if __name__ == '__main__':

main()

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐