如何在PyQt4中的元素周围添加框架?

时间:2016-05-18 14:23:06

标签: python user-interface pyqt4

我有一些按钮,我希望与小部件中的其他元素分开。我想在它们周围放一个框架,但我不确定如何。

import sys
from PyQt4 import QtGui, QtCore

class PasswordPrompt(QtGui.QWidget):

    def __init__(self):
        super(PasswordPrompt, self).__init__()
        self.initUi()

    def initUi(self):
        self.setFixedSize(1000, 500)
        self.setWindowTitle('Please enter the password...')

        hbox = QtGui.QHBoxLayout()
        vbox = QtGui.QVBoxLayout()
        btn1 = QtGui.QPushButton("1")
        btn2 = QtGui.QPushButton("2")
        btn3 = QtGui.QPushButton("3")
        vbox.addWidget(btn1)
        vbox.addWidget(btn2)
        vbox.addWidget(btn3)
        vbox.setSpacing(0)


        hbox.addLayout(vbox)


        self.setLayout(hbox)

        self.center()
        self.show()


    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


def main():
    application = QtGui.QApplication(sys.argv)
    p = PasswordPrompt()
    sys.exit(application.exec())

if __name__=='__main__':
    main()

举个例子,我如何在这些按钮周围添加黑框?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

enter image description here

QGroupBox可用于设置轮廓。

import sys
from PyQt4 import QtGui, QtCore

class PasswordPrompt(QtGui.QWidget):
    def __init__(self):
        super(PasswordPrompt, self).__init__()
        self.initUi()

    def initUi(self):
        layout = QtGui.QVBoxLayout()
        self.setLayout(layout)
        groupBox1 = QtGui.QGroupBox('Button 1')
        groupBox1Layout=QtGui.QVBoxLayout()
        groupBox1.setLayout(groupBox1Layout)

        btn1 = QtGui.QPushButton("1")
        groupBox1Layout.addWidget(btn1)

        groupBox2 = QtGui.QGroupBox('Button 2')
        groupBox2Layout=QtGui.QVBoxLayout()
        groupBox2.setLayout(groupBox2Layout)

        btn2 = QtGui.QPushButton("2")
        groupBox2Layout.addWidget(btn2)

        groupBox3 = QtGui.QGroupBox('Button 3')
        groupBox3Layout=QtGui.QVBoxLayout()
        groupBox3.setLayout(groupBox3Layout)

        btn3 = QtGui.QPushButton("3")
        groupBox3Layout.addWidget(btn3)

        layout.addWidget(groupBox1)
        layout.addWidget(groupBox2)
        layout.addWidget(groupBox3)
        self.resize(300, 100)
        self.show()

def main():
    application = QtGui.QApplication(sys.argv)
    p = PasswordPrompt()
    sys.exit(application.exec_())

if __name__=='__main__':
    main()