发布时间:2019-09-12 08:02:20编辑:auto阅读(2533)
Python GUI 教程三:布局
摘要:这篇文章是Python GUI教程系列的第三篇,将介绍Qt编程中的布局概念及其在Python环境下的实现
如果你英文较好,可以参考这里的文章:http://zetcode.com/gui/pyqt5/
作者:yooongchun 
微信公众号:yooongchun小屋 

STEP 1:认识布局
STEP 2:绝对定位布局
# -*- coding: utf-8 -*-
"""
该程序实现一个绝对定位布局器
Author: yooongchun
Time: 2018-05-07
"""
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication,QWidget,QLabel
from PyQt5.QtGui import QIcon
# 绝对定位布局
class AbsoLoca(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        lbl1 = QLabel('Hello,it\'s an absolute layout.', self)
        lbl1.move(50, 50)
        self.setGeometry(500, 500, 500, 300)
        self.setWindowTitle('Absolute')
        self.show()
if __name__=="__main__":
    app = QApplication(sys.argv)
    ex = AbsoLoca()
    sys.exit(app.exec_())
STEP 3:框布局
# -*- coding: utf-8 -*-
"""
该程序实现一个框布局器
Author: yooongchun
Time: 2018-05-02
"""
import sys
from PyQt5.QtWidgets import QPushButton, QAction, qApp, QApplication,QWidget,QLabel,QHBoxLayout,QVBoxLayout
from PyQt5.QtGui import QIcon
# 框布局
class BoxLoca(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        self.setGeometry(500, 500, 500, 400)
        self.setWindowTitle('Buttons')
        self.show()
if __name__=="__main__":
    app = QApplication(sys.argv)
    ex = BoxLoca()
    sys.exit(app.exec_())
STEP 4:网格布局
# -*- coding: utf-8 -*-
"""
该程序实现一个网格布局器
Author: yooongchun
Time: 2018-05-07
"""
import sys
from PyQt5.QtWidgets import QPushButton, QAction, qApp, QApplication,QWidget,QLabel,QGridLayout
from PyQt5.QtGui import QIcon
# 网格布局
class GridLoca(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)
        names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=',
                 '+']
        positions = [(i, j) for i in range(5) for j in range(4)]
        for position, name in zip(positions, names):
            if name == '':
                continue
            button = QPushButton(name)
            grid.addWidget(button, *position)
        self.move(1200, 750)
        self.setWindowTitle('Calculator')
        self.show()
if __name__=="__main__":
    app = QApplication(sys.argv)
    ex = GridLoca()
    sys.exit(app.exec_())
STEP 5:布局复合使用:以上几种布局器可以综合起来一起使用,以设计出更加复杂的界面逻辑
# -*- coding: utf-8 -*-
"""
该程序实现布局器的复合使用
Author: yooongchun
Time: 2018-05-02
"""
import sys
from PyQt5.QtWidgets import QPushButton,QWidget,QLineEdit,QTextEdit, QAction, qApp, QApplication,QWidget,QLabel,QGridLayout
from PyQt5.QtGui import QIcon
# 控件复合使用
class MoreLoca(QWidget):
  def __init__(self):
      super().__init__()
      self.initUI()
  def initUI(self):
      title = QLabel('Title')
      author = QLabel('Author')
      review = QLabel('Review')
      titleEdit = QLineEdit()
      authorEdit = QLineEdit()
      reviewEdit = QTextEdit()
      grid = QGridLayout()
      grid.setSpacing(10)
      grid.addWidget(title, 1, 0)
      grid.addWidget(titleEdit, 1, 1)
      grid.addWidget(author, 2, 0)
      grid.addWidget(authorEdit, 2, 1)
      grid.addWidget(review, 3, 0)
      grid.addWidget(reviewEdit, 3, 1, 5, 1)
      self.setLayout(grid)
      self.setGeometry(500, 500, 1500, 1200)
      self.setWindowTitle('Review')
      self.show()
if __name__=="__main__":
  app = QApplication(sys.argv)
  ex = MoreLoca()
  sys.exit(app.exec_())
上一篇: python大批量读写.doc文件的解决
下一篇: Python Thrift示例
 51192
 50604
 41229
 38048
 32511
 29417
 28278
 23132
 23092
 21425
 1481°
 2197°
 1820°
 1749°
 2062°
 1811°
 2494°
 4187°
 4049°
 2891°