首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用vbox布局PyQt错误的小部件顺序

使用vbox布局PyQt错误的小部件顺序
EN

Stack Overflow用户
提问于 2015-06-12 22:46:02
回答 1查看 512关注 0票数 0

我正在尝试将一个QLabel小部件放在(在此之前) QLineEdit小部件编辑的顶部。

但是它一直出现在QLineEdit小部件之后。我的密码

代码语言:javascript
复制
class CentralWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(CentralWidget, self).__init__(parent)

        # set layouts
        self.layout = QtGui.QVBoxLayout(self)

        # Flags
        self.randFlag = False        
        self.sphereFlag = False
        self.waterFlag = False

        # Poly names
        self.pNames = QtGui.QLabel("Import file name", self) # label concerned
        self.polyNameInput = QtGui.QLineEdit(self)           # line edit concerned

        # Polytype selection
        self.polyTypeName = QtGui.QLabel("Particle type", self)
        polyType = QtGui.QComboBox(self)
        polyType.addItem("")
        polyType.addItem("Random polyhedra")
        polyType.addItem("Spheres")
        polyType.addItem("Waterman polyhedra")
        polyType.activated[str].connect(self.onActivated)

        self.layout.addWidget(self.pNames)
        self.layout.addWidget(self.polyNameInput)
        self.layout.addWidget(self.pNames)
        self.layout.addWidget(self.polyTypeName)
        self.layout.addWidget(polyType)
        self.layout.addStretch()

    def onActivated(self, text):
        # Do loads of clever stuff that I'm not at liberty to share with you

class Polyhedra(QtGui.QMainWindow):

    def __init__(self):

        super(Polyhedra, self).__init__()

        self.central_widget = CentralWidget(self)
        self.setCentralWidget(self.central_widget)

        # Set up window        
        self.setGeometry(500, 500, 300, 300)
        self.setWindowTitle('Pyticle')
        self.show()

    # Combo box
    def onActivated(self, text):

        self.central_widget.onActivated(text)

def main():

    app = QtGui.QApplication(sys.argv)
    poly = Polyhedra()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我看到的窗户在下面。

我遗漏了什么?我认为QVbox允许按照将项添加到主小部件的顺序垂直地堆叠事物。(这些子小部件对象称为小部件吗?)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-13 11:57:50

问题在于您要向布局中添加两次self.pNames标签。

代码语言:javascript
复制
#portion of your code
... 
self.layout.addWidget(self.pNames)        # here 
self.layout.addWidget(self.polyNameInput)
self.layout.addWidget(self.pNames)         # and here
self.layout.addWidget(self.polyTypeName)
self.layout.addWidget(polyType)
self.layout.addStretch()
...

第一次添加QLabel时,它会在LineEdit之前添加,当您第二次添加它时,它只会移动到LineEdit的底部。之所以会发生这种情况,是因为QLabel只有一个对象,即self.pNames。它只能添加到一个位置。如果要使用两个标签,请考虑创建QLabel的两个单独的对象

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30813238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档