PyQt4布局管理实例

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))


class LayoutDialog(QDialog):
    def __init__(self, parent=None):
        super(LayoutDialog, self).__init__(parent)

        self.setWindowTitle(self.tr("User information")) # Window title

        label1 = QLabel(self.tr("User:"))
        label2 = QLabel(self.tr("Name:"))
        label3 = QLabel(self.tr("Gender:"))
        label4 = QLabel(self.tr("Department:"))
        label5 = QLabel(self.tr("Age:"))
        otherLabel = QLabel(self.tr("Note:"))
        otherLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)

        userLineEdit = QLineEdit()
        nameLineEdit = QLineEdit()
        sexComboBox = QComboBox()
        sexComboBox.insertItem(0, self.tr("Male"))
        sexComboBox.insertItem(1, self.tr("Female"))
        departmentTextEdit = QTextEdit()
        ageLineEdit = QLineEdit()

        labelCol = 0
        contentCol = 1

        leftLayout = QGridLayout() # left area takes gridlayout
        leftLayout.addWidget(label1, 0, labelCol)
        leftLayout.addWidget(userLineEdit, 0, contentCol)
        leftLayout.addWidget(label2, 1, labelCol)
        leftLayout.addWidget(nameLineEdit, 1, contentCol)
        leftLayout.addWidget(label3, 2, labelCol)
        leftLayout.addWidget(sexComboBox, 2, contentCol)
        leftLayout.addWidget(label4, 3, labelCol)
        leftLayout.addWidget(departmentTextEdit, 3, contentCol)
        leftLayout.addWidget(label5, 4, labelCol)
        leftLayout.addWidget(ageLineEdit, 4, contentCol)
        leftLayout.addWidget(otherLabel, 5, labelCol, 1, 2) # Span 1 row and 2 cols

        leftLayout.setColumnStretch(0, 1)
        leftLayout.setColumnStretch(1, 3) # the proportion of col 0 and col 1 is 1:3

        label6 = QLabel(self.tr("Portrait:"))
        iconLabel = QLabel()

        icon = QPixmap("C:\pythonlearning\pyqtGUI\por.jpg")
        iconLabel.setPixmap(icon)
        iconLabel.setFrameStyle(QFrame.Panel | QFrame.Sunken)
        iconLabel.resize(icon.width(), icon.height()) # icon label will change according to the size of the picture
        iconPushButton = QPushButton(self.tr("Change"))

        hLayout = QHBoxLayout() # portrait, picture, and change button is applied HBoxLayout
        hLayout.setSpacing(20) # portrait, picture, and change button have 20 spacing
        hLayout.addWidget(label6)
        hLayout.addWidget(iconLabel)
        hLayout.addWidget(iconPushButton)

        label7 = QLabel(self.tr("Personal introduction:"))
        descTextEdit = QTextEdit()

        rightLayout = QVBoxLayout()
        rightLayout.setMargin(10)
        rightLayout.addLayout(hLayout)
        rightLayout.addWidget(label7)
        rightLayout.addWidget(descTextEdit)

        OKPushButton = QPushButton(self.tr("Ok"))
        cancelPushButton = QPushButton(self.tr("Cancel"))
        bottomLayout = QHBoxLayout()
        bottomLayout.addStretch() #?
        bottomLayout.addWidget(OKPushButton)
        bottomLayout.addWidget(cancelPushButton)

        mainLayout = QGridLayout(self)
        mainLayout.setMargin(15)
        mainLayout.setSpacing(10)
        mainLayout.addLayout(leftLayout, 0, 0)
        mainLayout.addLayout(rightLayout, 0, 1)
        mainLayout.addLayout(bottomLayout, 1, 0, 1, 2)
        mainLayout.setSizeConstraint(QLayout.SetFixedSize) # setSizeConstraint: constraint the window size. stretchable?

app = QApplication(sys.argv)
dialog = LayoutDialog()
dialog.show()
app.exec_()

转载至 https://blog.csdn.net/liangyuannao/article/details/11812877


猜你喜欢

转载自blog.csdn.net/asszpdhpg/article/details/80663177