Layout management with layout classes is much more flexible and practical. It is the preferred way to place widgets on a window. The QtGui.QHBoxLayout and QtGui.QVBoxLayout are basic layout classes that line up widgets horizontally and vertically.

Imagine that we wanted to place two buttons in the right bottom corner. To create such a layout, we will use one horizontal, and one vertical box. To create the necessary space, we will add a stretch factor.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we position two push
buttons in the bottom-right corner
of the window. author: Jan Bodnar
website: zetcode.com
last edited: October 2011
""" import sys
from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel") hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton) vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox) self.setLayout(vbox) self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Buttons')
self.show() def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

The example places two buttons in the bottom-right corner of the window. They stay there when we resize the application window. We use both a QtGui.HBoxLayout and a QtGui.QVBoxLayout.

okButton = QtGui.QPushButton("OK")
cancelButton = QtGui.QPushButton("Cancel")

Here we create two push buttons.

hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)

We create a horizontal box layout and add a stretch factor and both buttons. The stretch adds a stretchable space before the two buttons. This will push them to the right of the window.

vbox = QtGui.QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox)

To create the necessary layout, we put a horizontal layout into a vertical one. The stretch factor in the vertical box will push the horizontal box with the buttons to the bottom of the window.

self.setLayout(vbox)

Finally, we set the main layout of the window.

Figure: Buttons

Box layout的更多相关文章

  1. Layout( 布局)

    一. 加载方式//class 加载方式<div id="box" class="easyui-layout"style="width:600px ...

  2. Box布局

    import sys from PyQt4 import QtCore, QtGui class MainWindow(QtGui.QWidget): def __init__(self, paren ...

  3. PyQt4 Box布局

    使用布局类别方式的布局管理器比绝对方式的布局管理器更加灵活实用.它是窗口部件的首选布局管理方式.最基本的布局类别是QHBoxLayout和QVBoxLayout布局管理方式,分别将窗口部件水平和垂直排 ...

  4. ZetCode PyQt4 tutorial layout management

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...

  5. PyQt的Layout的比例化分块。

    一. QGridLayout: // 列比 第0列与第1列之比为 1:2 layout2p1 -> setColumnStretch(0, 1); layout2p1 -> setColu ...

  6. 第二百零二节,jQuery EasyUI,Layout(布局)组件

    jQuery EasyUI,Layout(布局)组件 学习要点: 1.加载方式 2.布局属性 3.区域面板属性 4.方法列表 本节课重点了解 EasyUI 中 Layout(布局)组件的使用方法,这个 ...

  7. layout(布局)组件

    一.依赖于 Panel(面 板)组件和 resizable(调整大小)组件. 二.class加载方式    <div id="box" class="easyui- ...

  8. CSS3 弹性盒子(Flex Box)

    1 CSS3 弹性盒子(Flex Box) 1 http://caniuse.com/#search=flex%20box https://www.w3.org/TR/css-flexbox-1/ C ...

  9. CSS ? Layout Module : CSS 布局模型

    1 1 1 https://www.w3.org/TR/css-grid-1/ CSS Grid Layout Module Level 1 W3C Working Draft, 19 May 201 ...

随机推荐

  1. 韩梦飞沙-屏幕录像专家 win10 含注册机

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 韩梦飞沙-屏幕录像专家 win10 含注册机 百度云盘下载地址:https://pan. ...

  2. [BZOJ4784][ZJOI2017]仙人掌(树形DP)

    4784: [Zjoi2017]仙人掌 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 312  Solved: 181[Submit][Status] ...

  3. [转]Android 中fill_parent与wrap_content的区别

        在Android中,对于组件的属性“layout_width”和“layout_height”, 其值总是设置为“wrap_content”或“fill_parent”. 那么,这两个值有什么 ...

  4. UESTCACM 每周算法讲堂 延迟标记+bfs dfs搜索入门

    http://www.bilibili.com/video/av4163472/ 地址在上面~

  5. ExtJs 3 自定义combotree

    ExtJs 3 自定义combotree /** * 自定义下拉树,支持初始化值时自动定位树节点. * 还没有考虑性能问题.继承自Ext.form.ComboBox也很浪费. * 代码中的cu.get ...

  6. CMSIS-SVD Reference

    http://www.keil.com/pack/doc/cmsis/svd/html/modules.html SVD File Schema Levels Device Level Periphe ...

  7. undefined详解

    [对于<JS高级程序设计>的理解] “即使未初始化的变量会自动被赋值undefined值,但显式地初始化变量依然是明智的选择.如果能够做到这一点,那么当typeof操作符返回‘undefi ...

  8. 进程上下文VS中断上下文

    转载:http://www.cnblogs.com/zzx1045917067/archive/2012/12/19/2824552.html 内核空间和用户空间是现代操作系统的两种工作模式,内核模块 ...

  9. jq设置样式

    单个样式: $(this).css("color","red"); 多个样式: $(this).css({color:"red",backg ...

  10. KindEditor编辑器常用操作

    创建编辑器: ar editor=KindEditor.create('#nr'); 设置编辑器内容: editor.html('编辑器内容'); 移除编辑器: editor.remove();