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.

Figure: Calculator skeleton

时间: 2024-11-04 19:49:51

QtGui.QGridLayout的相关文章

PySide中QtGui.QFrame的用法

最近一位同事的出现让我重新正视PySide中designer这个工具的强大之处,通过QtGui.QObject.setGeometry(QtCore.QRect())这个最简单直接的方法可以完成很多复杂的界面绘制,从而摆脱QtGui.QGridLayout(),QVBoxLayout(),QHBoxLayout()三个Layout()对象的束缚. 这次matchmove组需要一个工具能够将maya工程中所选模型输出为abc文件,以该abc文件为基础自动生成一个nuke工程,继而将该nuke工程提

python将下载地址转换成迅雷和qq旋风的下载地址

迅雷和qq旋风下载,有加速和离线功能,很方面,我是在网上看到的原始地址和迅雷地址,qq旋风地址的转化原理,然后用python+pyqt写了一个客户端 原理: 迅雷: 迅雷下载地址="thunder://"+Base64编码("AA"+"真实地址"+"ZZ") QQ旋风: qqdl="qqdl://"+Base64编码("真实地址") import re import base64 fro

python之qt动态时间显示

# -*- coding: cp936 -*- from PyQt4 import QtGui from PyQt4 import QtCore import sys,time,random,os,re from PyQt4.QtGui import * from PyQt4.QtCore import * def Class_Import_Image(path): i=0#为什么函数名称是这样,因为Image它是一个类的对象,因为要导入所以写成这样 #while 1: try: ls=os.l

Python多线程(threading)学习总结

注:此文除了例子和使用心得是自己写的,很多都是Python核心编程中的原文.原文文风应该能看出来,就不每个地方单独表明出处了. 线程(有时被称为轻量级进程)跟进程有些相似,不同的是,所有的线程运行在同一个进程中,共享相同的运行环境.它们可以想像成是在主进程或"主线程"中并行运行的"迷你进程". 线程有开始,顺序执行和结束三部分.它有一个自己的指令指针,记录自己运行到什么地方.线程的运行可能被抢占(中断),或暂时的被挂起(也叫睡眠),让其它的线程运行,这叫做让步.一个

PyQT制作视频播放器

Python应用03 使用PyQT制作视频播放器 作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. 最近研究了Python的两个GUI包,Tkinter和PyQT.这两个GUI包的底层分别是Tcl/Tk和QT.相比之下,我觉得PyQT使用起来更加方便,功能也相对丰富.这一篇用PyQT实现一个视频播放器,并借此来说明PyQT的基本用法. 视频播放器 先把已经完成的代码放出来.代码基于Python 3.5: import time import s

ZetCode PyQt4 tutorial layout management

!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows three labels on a window using absolute positioning. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from P

pyqt之类listviw中查找内容(网友提供)

from PyQt4 import QtCore from PyQt4 import QtGui class SimpleListModel(QtCore.QAbstractListModel): def __init__(self, contents): super(SimpleListModel, self).__init__() self.contents = contents def rowCount(self, parent): return len(self.contents) de

路径1

#!/usr/bin/env python #-*- coding:utf-8 -*- import sip sip.setapi('QString', 2) sip.setapi('QVariant', 2) from PyQt4 import QtCore, QtGui class MyWindow(QtGui.QWidget): def __init__(self, parent=None): super(MyWindow, self).__init__(parent) self.path

py2.7+pyqt4开发端口检测工具

使用工具:python2.7,pyqt4,pyinstaller,pywin32 先贴代码 1 import sys 2 from PyQt4 import QtGui,QtCore 3 4 import threading 5 import thread 6 import os 7 import re 8 import urllib 9 import socket 10 import time 11 12 13 global maxNum 14 global cnt 15 global sig