How to add a tab in PySide

2019-06-05 03:48发布

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

I think

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

1条回答
We Are One
2楼-- · 2019-06-05 04:31

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_()
查看更多
登录 后发表回答