在两个类“QWidget”之间共享属性

我正在尝试将属性从 QWidget 类“发送”到另一个 QWidget 类。


在下面的示例中,我尝试将属于“Widget1”类的 QLineEdit“self.edit”的文本设置为属于“Widget2”类的 QLabel“self.label”的文本。


此尝试是在函数“setLabel”中进行的。我无法弄清楚的部分是“Widget2.label.setText(text)”


在函数的类中拥有一个类...我有点困惑如何实现这一点...


import sys

from PySide2.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QLineEdit)


class Main_UI(QWidget):


    def __init__(self):

        super().__init__()

        self.initUI()


    def initUI(self):

        layout = QVBoxLayout()

        widget1 = Widget1()

        widget2 = Widget2()

        layout.addWidget(widget1)

        layout.addWidget(widget2)

        self.setLayout(layout)

        self.show()


class Widget1(QWidget):

    def __init__(self):

        super().__init__()

        self.initUI()


    def initUI(self):

        layout = QHBoxLayout()

        self.edit = QLineEdit("")

        button = QPushButton("Set value")

        button.clicked.connect(self.setLabel)

        layout.addWidget(self.edit)

        layout.addWidget(button)

        self.setLayout(layout)

    

    def setLabel(self):

        text = self.edit.text()

        Widget2.label.setText(text)


class Widget2(QWidget):

    def __init__(self):

        super().__init__()

        self.initUI()


    def initUI(self):

        layout = QHBoxLayout()

        self.label = QLabel("")

        layout.addWidget(self.label)

        self.setLayout(layout)


def main():

    app = QApplication(sys.argv)

    ex = Main_UI()

    sys.exit(app.exec_())


if __name__ == '__main__':

    main()

任何帮助将不胜感激,如果我的示例或解释不清楚,我将提供进一步的解释。


温温酱
浏览 46回答 1
1回答

叮当猫咪

您可以使用自定义信号来完成此操作。import sysfrom PyQt5.QtWidgets import (QApplication, QHBoxLayout, QVBoxLayout, QWidget, QPushButton, QLabel, QLineEdit)from PyQt5 import QtCoreclass Main_UI(QWidget):    def __init__(self, parent=None):        super(Main_UI, self).__init__(parent)        self.initUI()        def initUI(self):        layout = QVBoxLayout()        widget1 = Widget1()        widget2 = Widget2()        layout.addWidget(widget1)        layout.addWidget(widget2)        self.setLayout(layout)                widget1.button_signal.connect(widget2.label.setText)  # Connecting the label to the custom signal.                self.show()class Widget1(QWidget):    button_signal = QtCore.pyqtSignal(str)  # Creating a signal.        def __init__(self, parent=None):        super(Widget1, self).__init__(parent)        self.initUI()        def initUI(self):        layout = QHBoxLayout()        self.edit = QLineEdit("")        button = QPushButton("Set value")        button.clicked.connect(self.setLabel)        layout.addWidget(self.edit)        layout.addWidget(button)        self.setLayout(layout)        def setLabel(self):        """Emit button signal with text.                This could have been solved with a lambda.                """                self.button_signal.emit(self.edit.text())  # Emitting Signal.class Widget2(QWidget):    def __init__(self, parent=None):        super(Widget2, self).__init__(parent)        self.initUI()        def initUI(self):        layout = QHBoxLayout()        self.label = QLabel("")        layout.addWidget(self.label)        self.setLayout(layout)def main():    app = QApplication(sys.argv)    ex = Main_UI()    sys.exit(app.exec_())if __name__ == '__main__':    main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python