如何使用pyqt4隐藏水平框

2024-04-25 02:21:40 发布

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

在我的示例程序中,我想隐藏hbox。但是我找不到任何方法在pyqt4中隐藏hbox。请任何人帮助我如何隐藏水平框。提前谢谢

下面是我的代码:

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()

Tags: selfexampledefshowsyscashcardgrid
1条回答
网友
1楼 · 发布于 2024-04-25 02:21:40

布局的功能是管理其他小部件的位置和大小,您的任务不是隐藏。相反,您必须创建一个小部件,其中hbox带有按钮,并且该小部件在网格布局中设置它,因此只需要隐藏或显示新的小部件

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)

相关问题 更多 >