class testWidget(QtGui.QWidget): def __init__(self, parent=None): super(testWidget, self).__init__(parent) self.parent = parent self.something() def something(self): self.parent.callme() # self.parent?.... nice? class testClass(): def __init__(self): self.widget = testWidget(parent=self)
test = testClass()python pyqt和父类
在python(pyqt)中处理父类最清洁的方式是什么? 有比直接调用self.parent更好的方法吗?
编辑 - 问题2:
class testWidget(QtGui.QWidget): def __init__(self, parent=None): super(testWidget, self).__init__(parent) self.say_hello() def say_hello(self): self.parent().hello() # <- works fine here def focusInEvent(self, event): self.parent().hello() # gives error in focusInEvent class testClass(QtGui.QWidget): def __init__(self): super(testClass, self).__init__(None) self.widget = testWidget(parent=self) def hello(self): print "hello"
test = testClass()
AttributeError的:“QWidget的”对象有没有属性“你好”
坦率地说,从其子部件调用父窗口小部件方法是一个坏主意。或者把这个方法作为一个普通的函数,或者使用[event filter](http://stackoverflow.com/questions/9442165/pyqt-mouse-events-for-qtabwidget)[catch focusInEvent event](http:// stackoverflow.com/questions/321656/when-a-qt-widget-gets-focus)。 – warvariuc 2012-08-08 11:52:49