PyQt动态添加选项卡,按钮布局问题

2024-05-05 21:37:44 发布

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

我试图创建一个系统,它会根据产品类型的多少自动添加选项卡,然后自动在相应的选项卡中为项目添加按钮,但由于某些原因,所有选项卡的按钮与第一个选项卡相同,我很确定这与布局有关,但我不确定具体是什么图像:enter image description here

        typetab = QtGui.QTabWidget(self)
        types = producttypes() ##returns a tuple with type names e.g. [('Drinks',), ('Food',)]

        for name in types:
            tab = QtGui.QWidget()
            typetab.addTab(tab, name[0])
            products = typeitems(name[0]) ## returns items of that product type [('Coke',), ('Pepsi',)]
            typetablayout = QtGui.QGridLayout()
            for length in range(math.floor(len(products)/5) + 1):
                for width in range(5):
                    try:
                        button = QtGui.QPushButton(products[width][0])
                        button.setObjectName(products[width][0])
                        typetablayout.addWidget(button,length, width)
                    except IndexError:
                        break
                    print([length,width])
            typetab.setLayout(typetablayout)

Tags: nameinfortypebuttonwidth按钮length
1条回答
网友
1楼 · 发布于 2024-05-05 21:37:44

看起来您需要将布局添加到选项卡,而不是添加到选项卡小部件:

    for name in types:
        tab = QtGui.QWidget()
        typetab.addTab(tab, name[0])
        typetablayout = QtGui.QGridLayout(tab)
        ...

        # typetab.setLayout(typetablayout)

相关问题 更多 >