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.
!/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 PyQt4 import QtGui

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):
        
        # The label widget is positioned at x=15 and y=10.
        lbl1 = QtGui.QLabel('ZetCode', self)
        lbl1.move(15, 10)

        lbl2 = QtGui.QLabel('tutorials', self)
        lbl2.move(35, 40)
        
        lbl3 = QtGui.QLabel('for programmers', self)
        lbl3.move(55, 70)        
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Absolute')    
        self.show()
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

--------------------------------------------------------------------------------

#!/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):
        
        # Here we create two push buttons.
        okButton = QtGui.QPushButton("OK")
        cancelButton = QtGui.QPushButton("Cancel")

        # 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.
        hbox = QtGui.QHBoxLayout()
        # Adds a stretchable space (a QSpacerItem) with zero minimum size and stretch factor stretch to the end of this box layout.
        # 添加一个弹簧,填充空间
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)

        # 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.
        vbox = QtGui.QVBoxLayout()
        # 添加一个弹簧,填充空间
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        
        # Finally, we set the main layout of the window.
        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()

--------------------------------------------------------------------------------

#!/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):
        
        # The instance of a QtGui.QGridLayout is created and set to be the layout for the application window.
        grid = QtGui.QGridLayout()
        self.setLayout(grid)
 
        # These are the labels used later for buttons.
        names = ['Cls', 'Bck', '', 'Close',
                 '7', '8', '9', '/',
                '4', '5', '6', '*',
                 '1', '2', '3', '-',
                '0', '.', '=', '+']
        
        # We create a list of positions in the grid.
        positions = [(i,j) for i in range(5) for j in range(4)]
        
        # Buttons are created and added to the layout with the addWidget() method.
        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()

--------------------------------------------------------------------------------

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

"""
ZetCode PyQt4 tutorial 

In this example, we create a bit
more complicated window layout using
the QtGui.QGridLayout manager. 

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):
        
        title = QtGui.QLabel('Title')
        author = QtGui.QLabel('Author')
        review = QtGui.QLabel('Review')

        titleEdit = QtGui.QLineEdit()
        authorEdit = QtGui.QLineEdit()
        reviewEdit = QtGui.QTextEdit()

        # We create a grid layout and set spacing between widgets.
        grid = QtGui.QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(title, 1, 0)
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(author, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

        grid.addWidget(review, 3, 0)
        # If we add a widget to a grid, we can provide row span and column span of the widget. In our case, we make the reviewEdit widget span 5 rows.
        grid.addWidget(reviewEdit, 3, 1, 5, 1)
        
        self.setLayout(grid) 
        
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Review')    
        self.show()
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

目录
相关文章
|
存储 缓存 固态存储
服务器高效硬盘还是要SSD固态硬盘?
服务器高效硬盘还是要SSD固态硬盘? 数据库放在 WDC WD3000BLFS (1万转) 跟放在 INTEL SSDSA2BW080G3   上的速度,有质的飞跃,不是同一个数量级的。 数据库必须得放到SSD上。
9644 0
Bootstrap5 侧边栏导航(Offcanvas)3
通过设置 `data-bs-scroll` 和 `data-bs-backdrop` 属性,可以控制侧边栏弹出时元素的滚动行为和背景画布的显示。示例中展示了不同配置下的滚动效果和背景画布的使用方法。
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
|
人工智能 供应链 大数据
1688跨境寻源通代采系统
1688跨境寻源通代采系统助力全球买家高效采购,2023年1月跨境注册买家增长76%,超594万。系统提供多语言支持,AI技术优化搜索,集成API接口扩展销售,支持自动化下单和支付。源头厂商合作,1亿+货盘开放,还有定制化解决方案,构建一站式智能采购平台,促进跨境贸易发展。[c0b.cc/R4rbK2](API测试账号)
|
Java Maven
【开源视频联动物联网平台】vertx写一个mqtt客户端
【开源视频联动物联网平台】vertx写一个mqtt客户端
440 1
|
机器学习/深度学习 数据挖掘
R语言逻辑回归模型的移动通信客户流失预测与分析
R语言逻辑回归模型的移动通信客户流失预测与分析
|
存储 缓存 索引
数据结构——顺序表的概念和基本操作(超全超详细)
数据结构——顺序表的概念和基本操作(超全超详细)
|
Java 数据库连接 Spring
Spring6 数据校验 Validation
Spring6 数据校验 Validation
|
前端开发 数据可视化 机器人
基于YAPI的接口工作流
基于YAPI的接口工作流
738 0
基于YAPI的接口工作流