如何制作没有标题栏的选项卡窗格?

时间:2019-02-25 00:41:18

标签: python python-3.x pyqt pyqt5

我想使用PyQt5使GUI像下面一样,但是我找不到帮助我的示例。 我搜索了“在qwidget上更改布局”和“没有标题栏的选项卡窗格”和“卡片布局”,但是没有运气。如何使用PyQt5做到这一点?



1 个答案:

答案 0 :(得分:2)

您必须使用QStackedLayout(或QStackedWidget)来在按下按钮时更改页面。第一页应该有按钮。我还实现了back()方法,该方法返回到初始页面,必须在按下“更改”按钮时调用该插槽:

from functools import partial
from PyQt5 import QtCore, QtWidgets

class CardWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(CardWidget, self).__init__(parent)
        self._layout = QtWidgets.QStackedLayout(self)
        button_widget = QtWidgets.QWidget()
        self.btn_lay = QtWidgets.QFormLayout(button_widget)

        self._layout.addWidget(button_widget)

    def add_widget(self, text, widget):
        self._layout.addWidget(widget)
        btn = QtWidgets.QPushButton(text)
        self.btn_lay.addRow(btn)
        btn.clicked.connect(partial(self._layout.setCurrentWidget, widget))

    @QtCore.pyqtSlot()
    def back(self):
        self._layout.setCurrentIndex(0)


class Widget(QtWidgets.QWidget):
    backSignal = QtCore.pyqtSignal()

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

        self.le1 = QtWidgets.QLineEdit()
        self.le2 = QtWidgets.QLineEdit()

        button = QtWidgets.QPushButton("Change")
        button.clicked.connect(self.backSignal)

        flay = QtWidgets.QFormLayout()
        flay.addRow("Value 1:", self.le1)
        flay.addRow("Value 2:", self.le2)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addLayout(flay)
        lay.addWidget(button)


def create_label():
    label = QtWidgets.QLabel(
        "Some Other Components", 
        alignment=QtCore.Qt.AlignCenter
    )
    label.setStyleSheet("background-color:blue;")
    return label


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)

    c = CardWidget()
    for i in range(3):
        w = Widget()
        w.backSignal.connect(c.back)
        c.add_widget("Want to Change value {}".format(i+1), w)

    p = QtWidgets.QWidget()
    lay = QtWidgets.QGridLayout(p)
    lay.addWidget(create_label(), 0, 0, 1, 2)
    lay.addWidget(c, 1, 0)
    lay.addWidget(create_label(), 1, 1)
    lay.setColumnStretch(0, 1)
    lay.setColumnStretch(1, 1)
    lay.setRowStretch(0, 1)
    lay.setRowStretch(1, 1)
    p.resize(640, 480)
    p.show()
    sys.exit(app.exec_())

enter image description here

enter image description here