本文介绍了如何在 PySide 中添加选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 2 个网格布局添加选项卡,但是当我运行代码时,似乎没有选项卡.

I am trying to add tab for 2 grid layout, but when I ran the code it seems like there is no tab.

我觉得

wid_inner.tab = QtGui.QTabWidget()

未将选项卡正确添加到网格

is not adding the tab correctly to the grid

import sys
from PySide import QtGui


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    # setting the outter widget and layout
    wid = QtGui.QWidget()
    grid = QtGui.QGridLayout()
    wid.setLayout(grid)

    # setting the inner widget and layout
    grid_inner = QtGui.QGridLayout()
    wid_inner = QtGui.QWidget()
    wid_inner.setLayout(grid_inner)

    # add the inner widget to the outer layout
    grid.addWidget(wid_inner)

    # add tab frame to widget
    wid_inner.tab = QtGui.QTabWidget()

    # create tab
    new_tab = QtGui.QWidget()
    grid_tab = QtGui.QGridLayout()
    grid_tab.setSpacing(10)
    new_tab.setLayout(grid_tab)
    new_tab.tab_name_private = "test1"
    wid_inner.tab.addTab(new_tab, "test1")

    # create tab 2
    new_tab2 = QtGui.QWidget()
    new_tab2.setLayout(grid_tab)
    wid_inner.tab.addTab(new_tab2, "test2")

    wid.show()
    sys.exit(app.exec_())

任何帮助将不胜感激谢谢

Any help would be appreciated thanks

推荐答案

您需要为每个内部小部件提供父级,并且标签小部件 wid_inner.tab 没有被添加到任何布局中.这个布局好像有点复杂……你考虑过用QtDesigner吗?

You need to provide the parent to each inner widget, and the tab widget wid_inner.tab was not being added to any layout. This seems a little complicated to establish the layout... Have you considered using QtDesigner?

wid = QtGui.QWidget()
grid = QtGui.QGridLayout(wid)
wid.setLayout(grid)

# setting the inner widget and layout
grid_inner = QtGui.QGridLayout(wid)
wid_inner = QtGui.QWidget(wid)
wid_inner.setLayout(grid_inner)

# add the inner widget to the outer layout
grid.addWidget(wid_inner)

# add tab frame to widget
wid_inner.tab = QtGui.QTabWidget(wid_inner)
grid_inner.addWidget(wid_inner.tab)

# create tab
new_tab = QtGui.QWidget(wid_inner.tab)
grid_tab = QtGui.QGridLayout(new_tab)
grid_tab.setSpacing(10)
new_tab.setLayout(grid_tab)
new_tab.tab_name_private = "test1"
wid_inner.tab.addTab(new_tab, "test1")

# create tab 2
new_tab2 = QtGui.QWidget(wid_inner.tab)
new_tab2.setLayout(grid_tab)
wid_inner.tab.addTab(new_tab2, "test2")

wid.show()
app.exec_()

这篇关于如何在 PySide 中添加选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:43