datetime:2019/5/20 14:27
author:nzb

栅格布局

import sys
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QVBoxLayout, QGroupBox, QGridLayout
from PyQt5 import QtGui
from PyQt5 import QtCore


class UI_demo(QDialog):
    """用户界面"""
    def __init__(self):
        super().__init__()

        # 窗口信息
        self.title = 'PyQt5 Grid Layout '
        self.left = 600
        self.top = 200
        self.width = 300
        self.height = 100

        self.initWindow()

    def initWindow(self):

        # 窗口信息
        self.setWindowIcon(QtGui.QIcon('../img/home.ico'))  # 图标设置
        self.setGeometry(self.left, self.top, self.width, self.height)  # 大小位置设置
        self.setWindowTitle(self.title)  # 窗口标题

        # 栅格布局
        self.createLayout()
        vbox = QVBoxLayout()
        vbox.addWidget(self.groupBox)
        self.setLayout(vbox)

        # 展示窗口
        self.show()

    def createLayout(self):
        """栅格布局"""
        self.groupBox = QGroupBox('What is your favorite programming language?')
        gridLayout = QGridLayout()
        btn = QPushButton('Python', self)
        btn.setIcon(QtGui.QIcon('../img/python.ico'))
        btn.setIconSize(QtCore.QSize(40, 40))
        btn.setMinimumHeight((40))
        gridLayout.addWidget(btn, 0, 0)

        btn1 = QPushButton('java', self)
        btn1.setIcon(QtGui.QIcon('../img/java.ico'))
        btn1.setIconSize(QtCore.QSize(40, 40))
        btn1.setMinimumHeight((40))
        gridLayout.addWidget(btn1, 0, 1)

        btn2 = QPushButton('php', self)
        btn2.setIcon(QtGui.QIcon('../img/php.ico'))
        btn2.setIconSize(QtCore.QSize(40, 40))
        btn2.setMinimumHeight((40))
        gridLayout.addWidget(btn2, 1, 0)

        btn3 = QPushButton('c++', self)
        btn3.setIcon(QtGui.QIcon('./img/C++.ico'))
        btn3.setIconSize(QtCore.QSize(40, 40))
        btn3.setMinimumHeight((40))
        gridLayout.addWidget(btn3, 1, 1)
        self.groupBox.setLayout(gridLayout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = UI_demo()
    sys.exit(app.exec_())

results matching ""

    No results matching ""