QtGui.QGridLayout

The most universal layout class is the grid layout. This layout divides the space into rows and columns. To create a grid layout, we use the QtGui.QGridLayout class.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui

"""
ZetCode PyQt4 tutorial 

In this example, we create a skeleton
of a calculator using a QtGui.QGridLayout.

author: Jan Bodnar
website: zetcode.com 
last edited: July 2014
"""

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):
        
        grid = QtGui.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 = QtGui.QPushButton(name)
            grid.addWidget(button, *position)
            
        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()

In our example, we create a grid of buttons. To fill one gap, we add one QtGui.QLabel widget.

grid = QtGui.QGridLayout()
self.setLayout(grid)

The instance of a QtGui.QGridLayout is created and set to be the layout for the application window.

names = ['Cls', 'Bck', '', 'Close',
            '7', '8', '9', '/',
        '4', '5', '6', '*',
            '1', '2', '3', '-',
        '0', '.', '=', '+']

These are the labels used later for buttons.

positions = [(i,j) for i in range(5) for j in range(4)]

We create a list of positions in the grid.

for position, name in zip(positions, names):
    
    if name == '':
        continue
    button = QtGui.QPushButton(name)
    grid.addWidget(button, *position)

Buttons are created and added to the layout with the addWidget() method.

Calculator skeletonFigure: Calculator skeleton

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一段python代码,请根据这段代码基于python_opencv实现点击self.pushButton时打开已搜到的相机列表并实现鼠标点击选择打开相应相机并显示在self.label,当点击self.pushButton_2时抓取当时帧显示在self.label_2 from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(1044, 729) self.gridLayoutWidget = QtWidgets.QWidget(Form) self.gridLayoutWidget.setGeometry(QtCore.QRect(19, 9, 991, 551)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.label = QtWidgets.QLabel(self.gridLayoutWidget) font = QtGui.QFont() font.setFamily("Adobe Arabic") font.setPointSize(26) self.label.setFont(font) self.label.setStyleSheet("background-color: rgb(255, 255, 127);") self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.label_2 = QtWidgets.QLabel(self.gridLayoutWidget) font = QtGui.QFont() font.setFamily("Adobe Arabic") font.setPointSize(26) self.label_2.setFont(font) self.label_2.setStyleSheet("background-color: rgb(170, 255, 255);") self.label_2.setAlignment(QtCore.Qt.AlignCenter) self.label_2.setObjectName("label_2") self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(130, 640, 161, 51)) font = QtGui.QFont() font.setFamily("Adobe Arabic") font.setPointSize(18) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(660, 640, 161, 51)) font = QtGui.QFont() font.setFamily("Adobe Arabic") font.setPointSize(18) self.pushButton_2.setFont(font) self.pushButton_2.setObjectName("pushButton_2") self.retranslateUi(Form) self.pushButton.clicked.connect(Form.Action) # type: ignore self.pushButton_2.clicked.connect(Form.UserNow) # type: ignore QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "实时图像")) self.label_2.setText(_translate("Form", "抓取图像")) self.pushButton.setText(_translate("Form", "打开相机")) self.pushButton_2.setText(_translate("Form", "抓取图像"))
05-18

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值