如何使用PyQt4的隐藏卧式箱

问题描述 投票:1回答:1

在这里,在我的示例程序,我想隐藏hbox.but我无法找到隐藏在pyqt4.Can hbox中的任何一个,请帮助我如何隐藏水平box.Thank你提前任何方法。

下面给出的是我的代码:

import sys
from PyQt4 import QtGui
global payments
payments = False
class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        self.grid = QtGui.QGridLayout()
        self.hbox = QtGui.QHBoxLayout()

        self.cash = QtGui.QPushButton("cash")
        self.card = QtGui.QPushButton("card")
        self.wallet = QtGui.QPushButton("wallet")
        self.hbox.addWidget(self.cash)
        self.hbox.addWidget(self.card)
        self.hbox.addWidget(self.wallet)
        self.paybtn = QtGui.QPushButton("pay")
        self.paybtn.clicked.connect(self.show_payments)
        self.grid.addWidget(self.paybtn,1,0)
        self.setLayout(self.grid)

        self.setGeometry(300, 300, 500,500)
        self.show()

    def show_payments(self):
        global payments
        payments = not payments
        print payments
        if payments:
            self.paybtn.setText('Edit Order')
            self.grid.addLayout(self.hbox,0,0)

        else:
            self.paybtn.setText('Pay')
            #here i want to hide the self.hbox


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
python python-2.7 pyqt pyqt4
1个回答
2
投票

布局的功能是管理岗位和其他部件的尺寸,你的任务不是去掩盖。相反,你必须创建一个小部件,其中横向盒与按钮和窗口小部件设置在网格布局,所以只需要隐藏或显示新的widget。

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500,500)

        grid = QtGui.QGridLayout(self)
        self.foo_widget = QtGui.QWidget(visible=False)
        self.foo_widget.setSizePolicy(QtGui.QSizePolicy.Preferred, 
            QtGui.QSizePolicy.Maximum)
        hbox = QtGui.QHBoxLayout(self.foo_widget)
        hbox.setContentsMargins(0, 0, 0, 0)

        self.cash = QtGui.QPushButton("cash")
        self.card = QtGui.QPushButton("card")
        self.wallet = QtGui.QPushButton("wallet")

        hbox.addWidget(self.cash)
        hbox.addWidget(self.card)
        hbox.addWidget(self.wallet)

        self.paybtn = QtGui.QPushButton("Pay", clicked=self.show_payments)

        grid.addWidget(self.foo_widget, 0, 0)
        grid.addWidget(self.paybtn, 1, 0)

    def show_payments(self):
        global payments
        payments = not payments
        self.paybtn.setText('Edit Order' if payments else 'Pay')
        self.foo_widget.setVisible(payments)
© www.soinside.com 2019 - 2024. All rights reserved.