当前位置:网站首页>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_())
边栏推荐
- 基于W5500实现的考勤系统
- Learn NLP with Transformer (Chapter 1)
- Implementation of recommendation system collaborative filtering in spark
- JVM性能调优方法
- 教你如何通过MCU将S2E配置为UDP的工作模式
- [tree] 100. Same tree
- 苹果美国宣布符合销售免税假期的各州产品清单细节
- Shell Chapter 5 homework
- SQL language (4)
- Talking about Devops monitoring, how does the team choose monitoring tools?
猜你喜欢

教你如何通过MCU将S2E配置为UDP的工作模式

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

SQL注入 Less17(报错注入+子查询)

leetcode 剑指 Offer 27. 二叉树的镜像

Emmet syntax quick query syntax basic syntax part

MIIdock简述

玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧

Learn Luzhi PHP -- tp5.0 uses Chinese as an alias and reports "unsupported data expression"

SQL language (6)

第一个C语言程序(从Hello World开始)
随机推荐
Leetcode sword finger offer 27. image of binary tree
第4章线性方程组
将字符串转换为数字
小区蔬菜配送的小程序
The most efficient note taking method in the world (change your old version of note taking method)
贪心问题01_活动安排代码分析
Small program of vegetable distribution in community
Smart cloud IOT platform STM32 esp8266-01s simple wireless light control
基于cornerstone.js的dicom医学影像查看浏览功能
Nowcodertop12-16 - continuous updating
The B2B2C multi merchant system has rich functions and is very easy to open!!!
SQL注入 Less18(头部注入+报错注入)
Reflection reflection
数据库设计-简化字典表[通俗易懂]
ArcMap cannot start the solution
玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
shell-第五章作业
黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
贪心问题01_活动安排问题