pyqt5-main window class references other classes

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QDialog, QVBoxLayout, QLabel


class CustomDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Dialog Window")

        layout = QVBoxLayout()

        self.dialog_button = QPushButton("Show Message in Main Window", self)
        self.dialog_button.clicked.connect(self.show_message_in_main_window)
        layout.addWidget(self.dialog_button)

        self.setLayout(layout)

    def show_message_in_main_window(self):
        if isinstance(self.parent(), MainWindow):
            self.parent().statusBar().showMessage("你好")
            

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")

        self.central_widget = QPushButton("Show Dialog", self)
        self.central_widget.clicked.connect(self.show_dialog)
        self.setCentralWidget(self.central_widget)
        self.statusBar().showMessage("欢迎使用")

    def show_dialog(self):
        self.dialog = CustomDialog(self)
        self.dialog.show()


def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

Guess you like

Origin blog.csdn.net/m0_62653695/article/details/132459281