当前位置:网站首页>布局管理==PYQT5
布局管理==PYQT5
2022-07-25 10:48:00 【栋哥修炼日记】
框布局Boxlayout
- 我们使用QHBoxLayout和QVBoxLayout,来分别创建横向布局和纵向布局。
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)#伸展因子
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_())
表格布局
- 将空间划分为行和列。我们使用QGridLayout类创建一个网格布局。
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)#我们创建一个网格的按钮。
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 = QPushButton(name)
grid.addWidget(button, *position)
self.move(300, 150)
self.setWindowTitle('Calculator')
self.show()
#控件可以在网格中跨越多个行列
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')#标签文本
author = QLabel('Author')
review = QLabel('Review')
titleEdit = QLineEdit()#文本编辑处
authorEdit = QLineEdit()
reviewEdit = QTextEdit()
grid = 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)
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_())
绝对定位
- 程序指定每个控件的位置和大小(以像素为单位)
- 绝对定位有以下限制:
- 如果我们调整窗口,控件的大小和位置不会改变
- 在各种平台上应用程序看起来会不一样
- 如果改变字体,我们的应用程序的布局就会改变
- 如果我们决定改变我们的布局,我们必须完全重做我们的布局
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)#我们使用move()方法来控制控件的位置
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_())
边栏推荐
- 使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
- Emmet syntax quick query syntax basic syntax part
- C# Newtonsoft. Jason advanced usage
- 同事看了我的代码惊呼:居然是这么在Unity中用单例的
- 从宏观到微观 零基础 详解bert
- SQL注入 Less23(过滤注释符)
- 用Unity不会几个插件怎么能行?Unity各类插件及教程推荐
- LVS load balancing lvs-dr builds Web Clusters and LVS combines with kept to build highly available Web Clusters
- 推荐系统-协同过滤在Spark中的实现
- SQL injection LESS18 (header injection + error injection)
猜你喜欢

复习背诵整理版

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

游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)

Small and micro enterprise smart business card management applet

MySQL | GROUP_ The concat function concatenates the values of a column with commas

B2B2C多商户系统功能丰富,极易二开!!!

同事看了我的代码惊呼:居然是这么在Unity中用单例的

谣言检测文献阅读十一—Preventing rumor spread with deep learning

全网显示 IP 归属地,是怎么实现的?

新能源销冠宏光MINIEV,有着怎样的产品力?
随机推荐
Definition of information entropy
Detailed explanation of the implementation method of DNS separation and resolution
shell-第四天作业
Why should the hashcode () method be rewritten when rewriting the equals () method
只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
爬虫基础一
Syncronized lock upgrade process
Redis 入门
Linked list related (design linked list and ring linked list)
flinksql client 连接kafka select * from table没有数据报错,如何解决?
苹果美国宣布符合销售免税假期的各州产品清单细节
C# Newtonsoft. Jason advanced usage
Dynamic planning question 05_ Missile interception
ESP8266 使用 DRV8833驱动板驱动N20电机
Small and micro enterprise smart business card management applet
[dynamic planning] 70. Climbing stairs
There is a newline problem when passing shell script parameters \r
【mysql学习08】
大话DevOps监控,团队如何选择监控工具?