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_())