天天看點

PyQt4 Layout 布局管理學習筆記

1. Absolute Layout 絕對定位

類:無,預設。使用 move 方法進行定位。

2. Box Layout 盒子布局

類:QHBoxLayout(水準) 和 QVBoxLayout(垂直)
概述

通過 layout 類管理會更加靈活和實用。這是優先考慮的方法。基本的 layout 類是 QtGui.QHBoxLayout 和 QtGui.QVBoxLayout 。它們分别以水準和垂直方式對 widget 進行布局。

例子代碼:
<!-- lang: python -->
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()
    def initUI(self):
        names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/',
                    '4', '5', '6', '*', '1', '2', '3', '-',
                    '0', '.', '=', '+']
        grid = QtGui.QGridLayout()
        j = 0
        pos = [(0, 0), (0, 1), (0, 2), (0, 3),
                (1, 0), (1, 1), (1, 2), (1, 3),
                (2, 0), (2, 1), (2, 2), (2, 3),
                (3, 0), (3, 1), (3, 2), (3, 3 ),
                (4, 0), (4, 1), (4, 2), (4, 3)]
        for i in names:
            button = QtGui.QPushButton(i)
            if j == 2:
               grid.addWidget(QtGui.QLabel(''), 0, 2)
            else: grid.addWidget(button, pos[j][0], pos[j][1])
            j = j + 1
    
        self.setLayout(grid)       
        self.move(300, 150)
        self.setWindowTitle('Calculator')    
        self.show()
           
def main(): 
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
    
if __name__ == '__main__':
    main()
           

3. Grid Layout 網格布局

類:QGridLayout
例子代碼

版權聲明:本文為CSDN部落客「weixin_33682790」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_33682790/article/details/92062734