当前位置:网站首页>Layout management ==pyqt5
Layout management ==pyqt5
2022-07-25 11:39:00 【Brother Dong's cultivation diary】
Box layout Boxlayout
- We use QHBoxLayout and QVBoxLayout, To create horizontal layout and vertical layout respectively .
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
QHBoxLayout, QVBoxLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(okButton)
hbox.addWidget(cancelButton)
vbox = QVBoxLayout()
vbox.addStretch(1)# stretch factor
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 150)
self.setWindowTitle('Buttons')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Table layout
- Divide the space into rows and columns . We use QGridLayout Class to create a grid layout .
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
QPushButton, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)# Let's create a grid button .
names = ['Cls', 'Bck', '', 'Close',# These button labels
'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):# We create a list of locations in the grid
if name == '':
continue
button = QPushButton(name)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
# Control can span multiple rows and columns in the grid
import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
QTextEdit, QGridLayout, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
title = QLabel('Title')# Label text
author = QLabel('Author')
review = QLabel('Review')
titleEdit = QLineEdit()# Text editing Office
authorEdit = QLineEdit()
reviewEdit = QTextEdit()
grid = QGridLayout()# Table layout
grid.setSpacing(10)
grid.addWidget(title, 1, 0)
grid.addWidget(titleEdit, 1, 1)# Create a grid layout and set the spacing between components .
grid.addWidget(author, 2, 0)
grid.addWidget(authorEdit, 2, 1)
grid.addWidget(review, 3, 0)
grid.addWidget(reviewEdit, 3, 1, 5, 1)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Absolute positioning
- The program specifies the location and size of each control ( In pixels )
- Absolute positioning has the following limitations :
- If we adjust the window , The size and position of the control will not change
- Applications will look different on various platforms
- If you change the font , The layout of our application will change
- If we decide to change our layout , We have to completely redo our layout
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
lbl1 = QLabel('Zetcode', self)
lbl1.move(15, 10)# We use move() Method to control the position of the control
lbl2 = QLabel('tutorials', self)
lbl2.move(35, 40)
lbl3 = QLabel('for programmers', self)
lbl3.move(55, 70)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Absolute')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
边栏推荐
- Redis之压缩列表ziplist
- Web mobile terminal: touchmove realizes local scrolling
- C# Newtonsoft. Jason advanced usage
- Why should the hashcode () method be rewritten when rewriting the equals () method
- Some errors of tensorflow calling multiple GPUs
- Nowcodertop1-6 - continuous updating
- RedisUtil
- LVS load balancing lvs-nat building Web Cluster
- WIZnet嵌入式以太网技术培训公开课(免费!!!)
- Nowcodertop7-11 - continuous updating
猜你喜欢

SQL injection less23 (filter comment)

城市雕塑典型作品信息管理系统(图片分享系统SSM)

Some usages of beautifulsoup

How to judge the performance of static code quality analysis tools? These five factors must be considered

Filter过滤器解决request请求参数乱码的原理解析

Let sports happen naturally, and fire creates a new lifestyle

Review recitation finishing version

Activity registration | play with kubernetes container service improvement class officially opened!

SQL注入 Less18(头部注入+报错注入)

Small and micro enterprise smart business card management applet
随机推荐
Common linear modulation methods based on MATLAB
Shell fourth day homework
常见WEB攻击与防御
新能源销冠宏光MINIEV,有着怎样的产品力?
The most efficient note taking method in the world (change your old version of note taking method)
Redis之压缩列表ziplist
【leetcode刷题】
Why should the hashcode () method be rewritten when rewriting the equals () method
Definition of information entropy
一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测
圆角大杀器,使用滤镜构建圆角及波浪效果!
2022 年中回顾|一文看懂预训练模型最新进展
ESP8266 使用 DRV8833驱动板驱动N20电机
数据库完整性——六大约束学习
Multiply Floyd "suggestions collection"
LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
SQL language (4)
Smart cloud IOT platform STM32 esp8266-01s simple wireless light control
Reflection reflection
LVS负载均衡之LVS-NAT搭建Web群集