【问题标题】:PyQt class inheritancePyQt 类继承
【发布时间】:2014-02-11 13:15:26
【问题描述】:

我很难理解 Python/PyQt 的类继承。我有一个MainWindow 和一个弹出窗口QWidget。在弹出窗口中打开QWidget 后,我想与MainWindowself.label1 进行交互,但我不知道该怎么做。我只知道反过来,从MainWindow 内的弹出窗口访问所有小部件,反之亦然。

这里是一个例子,MainWindowself.label1 应该在MyPopup 在新窗口中打开之后得到另一个文本:

import sys
from PyQt4.Qt import *

class MyPopup(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        # I want to change the lable1 of MainWindow
        self.cw.label1.setText('hello')


class MainWindow(QMainWindow):
    def __init__(self, *args):
        QMainWindow.__init__(self, *args)
        self.cw = QWidget(self)
        self.setCentralWidget(self.cw)
        self.btn1 = QPushButton("Click me", self.cw)
        self.btn1.setGeometry(QRect(50, 50, 100, 30))
        self.label1 = QLabel("No Commands running", self.cw)
        self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
        self.w = None

    def doit(self):
        self.w = MyPopup()
        self.w.setGeometry(QRect(100, 100, 400, 200))
        self.w.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python class inheritance pyqt


    【解决方案1】:

    你需要将主窗口作为参数传递给MyPopup的构造函数,试试这个:

    import sys
    from PyQt4.Qt import *
    
    class MyPopup(QWidget):
        def __init__(self, mainWindow):
            QWidget.__init__(self)
    
            # use the mainWindow passed as parameter
            mainWindow.label1.setText('hello')
    
    
    class MainWindow(QMainWindow):
        def __init__(self, *args):
            QMainWindow.__init__(self, *args)
            self.cw = QWidget(self)
            self.setCentralWidget(self.cw)
            self.btn1 = QPushButton("Click me", self.cw)
            self.btn1.setGeometry(QRect(50, 50, 100, 30))
            self.label1 = QLabel("No Commands running", self.cw)
            self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
            self.w = None
    
        def doit(self):
            self.w = MyPopup(self) #when creating the popup pass in the main window
            self.w.setGeometry(QRect(100, 100, 400, 200))
            self.w.show()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        myapp = MainWindow()
        myapp.show()
        sys.exit(app.exec_())
    

    【讨论】:

    【解决方案2】:

    这是 Alvaro Fuentes 的先前回答,其中包含 PyQt5 所需的小更新。

    import sys
    from PyQt5.Qt import *
    
    class MyPopup(QWidget):
        def __init__(self, mainwin):
            QWidget.__init__(self)
    
            # I want to change the lable1 of MainWindow
            mainwin.label1.setText('hello')
    
    
    class MainWindow(QMainWindow):
        def __init__(self, *args):
            QMainWindow.__init__(self, *args)
            self.cw = QWidget(self)
            self.setCentralWidget(self.cw)
            self.btn1 = QPushButton("Click me", self.cw)
            self.btn1.setGeometry(QRect(50, 50, 100, 30))
            self.label1 = QLabel("No Commands running", self.cw)
            self.btn1.clicked.connect(self.doit)
            self.w = None
    
        def doit(self):
            self.w = MyPopup(self)
            self.w.setGeometry(QRect(100, 100, 400, 200))
            self.w.show()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        myapp = MainWindow()
        myapp.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-21
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2018-05-23
      相关资源
      最近更新 更多