天天看点

PyQt5:表格布局(15)

学习《PyQt4入门指南 PDF中文版.pdf 》笔记

PyQt5:表格布局(15)

最容易遗漏的QFormLayout

         通常我们想到布局就是水平、垂直和网格布局,并且这三种布局基本可以实现所有的界面部件的放置。但是并不代表每次都是最适合的。如实现电话本界面:

联系人:XXX

电话:  XXX

地址:  XXX

邮箱:  XXX

这个时候我们使用QFormLayout表格布局,标签和值的空间,一对对插入。

<span style="font-size:14px;">#!/usr/bin/python
# formlayout.py
from PyQt5.QtWidgets import QApplication, QLineEdit, QLabel, QFormLayout
from PyQt5 import QtWidgets

class FormLayout(QtWidgets.QWidget):
    def __init__(self, parent= None):
        QtWidgets.QWidget.__init__(self)
  
        self.setWindowTitle('grid layout')
        
        title = QLabel('Title')
        author = QLabel('Author')
        review = QLabel('Review')
        
        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        reviewEdit = QLineEdit()
        
        layout = QFormLayout()
        layout.addRow(title, titleEdit)
        layout.addRow(author, authorEdit)
        layout.addRow(review, reviewEdit)

        self.setLayout(layout)
        self.resize(350,  300)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    qb = FormLayout()
    qb.show()
    sys.exit(app.exec_())</span>
           

        layout.addRow(title, titleEdit)

         可以看得出来这里使用的事addRow()方法一行一行的添加。从代码简洁看也不错的选择。