我现在想用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_())
一个小部件不能同时位于两个不同的小部件中。因此,您必须创建两个
Series
实例(每个SeriesHBox
一个)共享数据(比如标签的内容)最简单的方法是提取另一个对象中的状态(文本),该对象将由
Series
实例共享,并在内容更改时更新它们一个简单的例子:
如果更改
SeriesModel
中的内容,这两个标签也将更新对不起,我有PyQt5。 创建
Series
类的两个实例这就是我想要的答案
相关问题 更多 >
编程相关推荐