Pyqt在不同的布局中添加相同的小部件对象

2024-09-30 01:19:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在想用Pyqt来设计这样一组逻辑,在两个布局中添加两个不同的小部件,这两个不同的小部件使用相同的小部件,因为我想在不同的布局中共享相同小部件的数据,但不幸的是,我的设计失败了,在这种情况下,我无法在显示器上显示两个Pyqt,有人能帮我吗


from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Series(QWidget):
    def __init__(self):
        super(Series, self).__init__()
        self.lb = QLabel('PYQT')


class SeriesHBox1(QWidget):
    def __init__(self, series):
        super(SeriesHBox1, self).__init__()
        self.vbox = QVBoxLayout()
        self.setLayout(self.vbox)
        self.vbox.addWidget(series.lb)


class SeriesHBox2(QWidget):
    def __init__(self, series):
        super(SeriesHBox2, self).__init__()
        self.hbox = QHBoxLayout()
        self.setLayout(self.hbox)
        self.hbox.addWidget(series.lb)


class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 300)
        box = QHBoxLayout()
        self.setLayout(box)

        box1 = QHBoxLayout()
        box2 = QHBoxLayout()
        box.addLayout(box1)
        box.addLayout(box2)

        series = Series()
        box1.addWidget(SeriesHBox1(series))
        box2.addWidget(SeriesHBox2(series))
        # box2.addWidget(SeriesHBox2(Series()))

        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Tags: selfboxinit部件defclassseriessuper
3条回答

一个小部件不能同时位于两个不同的小部件中。因此,您必须创建两个Series实例(每个SeriesHBox一个)

共享数据(比如标签的内容)最简单的方法是提取另一个对象中的状态(文本),该对象将由Series实例共享,并在内容更改时更新它们

一个简单的例子:

class SeriesModel(QObject):
    def __init__(self, parent=None):
        super(SeriesModel, self).__init__(parent)
        self._content = "PYQT"

    contentChanged = pyqtSignal(str)

    @pyqtProperty(str, notify=contentChanged)
    def content(self):
        return self._content

    @content.setter
    def content(self, value):
        self._content = value

class Series(QWidget):
    def __init__(self, model):
        super(Series, self).__init__()
        self.model = model
        self.lb = QLabel(model.content)

        self.model.contentChanged.connect(self.lb.setText)



class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 300)
        box = QHBoxLayout()
        self.setLayout(box)

        box1 = QHBoxLayout()
        box2 = QHBoxLayout()
        box.addLayout(box1)
        box.addLayout(box2)


        model = SeriesModel(self)

        series1 = Series(model)
        series2 = Series(model)

        box1.addWidget(SeriesHBox1(series1))
        box2.addWidget(SeriesHBox2(series2))

        self.show()

如果更改SeriesModel中的内容,这两个标签也将更新

对不起,我有PyQt5。 创建Series类的两个实例

#from PyQt4.QtGui import *
#from PyQt4.QtCore import *
from PyQt5.Qt import *
import random


class Series(QWidget):
    def __init__(self):
        super(Series, self).__init__()
        self.lb = QLabel('PYQT')
        self.lb.setStyleSheet("""background-color: {};""".format(
            QColor(*random.sample(range(255), 3)).name()
        )
    )


class SeriesHBox1(QWidget):
    def __init__(self, series):
        super(SeriesHBox1, self).__init__()
        self.vbox = QVBoxLayout()
        self.setLayout(self.vbox)
        self.vbox.addWidget(series.lb)


class SeriesHBox2(QWidget):
    def __init__(self, series):
        super(SeriesHBox2, self).__init__()
        self.hbox = QHBoxLayout()
        self.setLayout(self.hbox)
        self.hbox.addWidget(series.lb)


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 300)
        box = QHBoxLayout()
        self.setLayout(box)

        box1 = QHBoxLayout()
        box2 = QHBoxLayout()
        box.addLayout(box1)
        box.addLayout(box2)

#        series = Series()
        series1 = Series()                                # +
        series2 = Series()                                # +

        box1.addWidget(SeriesHBox1(series1))
        box2.addWidget(SeriesHBox2(series2))
        # box2.addWidget(SeriesHBox2(Series()))


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

enter image description here

这就是我想要的答案


from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Series(QWidget):
    def __init__(self, parent=None):
        super(Series, self).__init__(parent)
        self.lb = QLabel('PYQT')


class SeriesHBox1(QWidget):
    def __init__(self, series):
        super(SeriesHBox1, self).__init__()
        self.vbox = QVBoxLayout()
        self.setLayout(self.vbox)
        self.vbox.addWidget(series.lb)


class SeriesHBox2(QWidget):
    def __init__(self, series):
        super(SeriesHBox2, self).__init__()
        self.hbox = QHBoxLayout()
        self.setLayout(self.hbox)
        self.hbox.addWidget(series.lb)


class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 300)
        box = QHBoxLayout()
        self.setLayout(box)

        box1 = QHBoxLayout()
        box2 = QHBoxLayout()
        box.addLayout(box1)
        box.addLayout(box2)
        s = Series()
        h = SeriesHBox1(s)
        s.__init__(h)
        print(s)
        box1.addWidget(h)
        b = SeriesHBox2(s)
        s.__init__(b)
        print(s)
        box2.addWidget(b)

        c = QPushButton()
        c.clicked.connect(self.close_a)

        d = QPushButton()
        d.clicked.connect(self.close_b)

        box.addWidget(c)
        box.addWidget(d)
        self.show()

    def close_a(self):
        self.a.hide()

    def close_b(self):
        self.b.hide()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

相关问题 更多 >

    热门问题