news 2026/7/22 9:40:50

Python PyQt6教程三-布局管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python PyQt6教程三-布局管理

这是PyQt6教程。本教程适合初学者和中级程序员。阅读本教程后,您将能够编写非平凡的PyQt6应用程序。

代码示例可在本站下载:教程源代码

目录

  • 引言
  • 日期和时间
  • 第一个工程
  • 菜单与工具栏
  • 布局管理
  • 事件和信号
  • 对话框
  • 小部件
  • 小工具II
  • 拖放
  • 绘画

PyQt6中的布局管理

布局管理是我们在应用程序窗口上放置小部件的方式。我们可以使用绝对定位或布局类来放置我们的小部件。使用布局管理器管理布局是组织小部件的首选方式。

绝对位置

程序员以像素为单位指定每个小部件的位置和大小。当您使用绝对定位时,我们必须了解以下限制:

  • 如果我们调整窗口大小,小部件的大小和位置不会改变
  • 不同平台上的应用程序可能看起来不同
  • 更改应用程序中的字体可能会破坏布局
  • 如果我们决定改变布局,我们必须完全重做布局,这既乏味又耗时

以下示例将小部件定位在绝对坐标系中。

#!/usr/bin/python """ ZetCode PyQt6 tutorial This example shows three labels on a window using absolute positioning. Author: Jan Bodnar Website: zetcode.com """ import sys from PyQt6.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) lbl2 = QLabel('tutorials', self) lbl2.move(35, 40) lbl3 = QLabel('for programmers', self) lbl3.move(55, 70) self.setGeometry(300, 300, 350, 250) self.setWindowTitle('Absolute') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) if __name__ == '__main__': main()

我们使用这种方法来定位我们的小部件。在我们的例子中,这些是标签。我们通过提供x和y坐标来定位它们。坐标系的起点位于左上角。x值从左向右增长。y值从上到下增长。

lbl1 = QLabel('ZetCode', self) lbl1.move(15, 10)

标签小部件位于和处。x=15y=10

PyQt6 QHBox布局

QHBoxLayout和是水平和垂直排列小部件的基本布局类。QVBox布局
想象一下,我们想在右下角放置两个按钮。为了创建这样的布局,我们使用一个水平框和一个垂直框。为了创建必要的空间,我们添加了一个拉伸因子。

#!/usr/bin/python """ ZetCode PyQt6 tutorial In this example, we position two push buttons in the bottom-right corner of the window. Author: Jan Bodnar Website: zetcode.com """ import sys from PyQt6.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, 350, 250) self.setWindowTitle('Buttons') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) if __name__ == '__main__': main()

该示例在窗口的右下角放置了两个按钮。当我们调整应用程序窗口的大小时,它们会留在那里。我们同时使用a和a.QHBox布局QVBox布局

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)

最后,我们设置窗口的主布局。

PyQt6 QGrid布局

QGridLayout是最通用的布局类。它将空间划分为行和列。

#!/usr/bin/python """ ZetCode PyQt6 tutorial In this example, we create a skeleton of a calculator using QGridLayout. Author: Jan Bodnar Website: zetcode.com """ import sys from PyQt6.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() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) if __name__ == '__main__': main()

在我们的示例中,我们创建了一个按钮网格。

grid = QGridLayout() self.setLayout(grid)

创建一个实例,并将其设置为应用程序窗口的布局。QRidLayout

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)

使用该方法创建按钮并将其添加到布局中。addWidget

复习示例


小部件可以跨越网格中的多列或行。在下一个示例中,我们将对此进行说明。

#!/usr/bin/python """ ZetCode PyQt6 tutorial In this example, we create a bit more complicated window layout using the QGridLayout manager. Author: Jan Bodnar Website: zetcode.com """ import sys from PyQt6.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() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) if __name__ == '__main__': main()

我们创建了一个窗口,其中有三个标签、两行编辑和一个文本编辑小部件。布局是用完成的。QRidLayout

grid = QGridLayout() grid.setSpacing(10)

我们创建网格布局并设置小部件之间的间距。

grid.addWidget(reviewEdit, 3, 1, 5, 1)

若我们将小部件添加到网格中,我们可以提供小部件的行跨度和列跨度。在我们的例子中,我们让小部件跨越5行。reviewEdit

PyQt6教程的这一部分专门介绍布局管理。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/19 19:27:54

AMD架构的云服务器和Intel架构在性能上有哪些实际差异?

AMD(如EPYC系列)与Intel(如Xeon Scalable系列)架构的云服务器在性能上并无绝对的“谁更强”,而是存在场景依赖、代际差异和优化侧重的实际差异。以下是基于当前主流云平台(AWS EC2、Azure VM、阿里云ECS等&…

作者头像 李华
网站建设 2026/7/22 5:14:09

掌握Python数据分析核心技能:从数据洞察到业务决策的完整指南

掌握Python数据分析核心技能:从数据洞察到业务决策的完整指南 【免费下载链接】pyda-2e-zh :book: [译] 利用 Python 进行数据分析 第 2 版 项目地址: https://gitcode.com/gh_mirrors/py/pyda-2e-zh Python数据分析是当今数据驱动决策的关键技术&#xff0…

作者头像 李华
网站建设 2026/7/21 7:54:24

昇腾嵌入式大模型推理加速全攻略:从入门到精通实战指南

开篇破局:嵌入式AI的性能瓶颈与突破路径 【免费下载链接】openPangu-Embedded-1B-V1.1 昇腾原生的开源盘古 Embedded-1B-V1.1 语言模型 项目地址: https://ai.gitcode.com/ascend-tribe/openPangu-Embedded-1B-V1.1 在边缘计算场景中部署大语言模型时&#x…

作者头像 李华
网站建设 2026/7/22 6:10:22

RepRapFirmware开源固件完整安装使用指南

RepRapFirmware开源固件完整安装使用指南 【免费下载链接】RepRapFirmware OO C RepRap Firmware 项目地址: https://gitcode.com/gh_mirrors/re/RepRapFirmware RepRapFirmware是一款专为3D打印机设计的开源固件,采用面向对象的C语言编写,为现代…

作者头像 李华
网站建设 2026/7/21 5:09:26

岩石纹理设计资源包:提升专业设计效率的必备素材库

岩石纹理设计资源包:提升专业设计效率的必备素材库 【免费下载链接】岩石花纹及符号资源包介绍 本资源包「岩石花纹及符号CDR.zip」汇集了丰富的岩石纹理设计元素与符号,专为设计与绘图工作打造。内含多样化的岩石花纹,适用于背景、纹理填充等…

作者头像 李华
网站建设 2026/7/21 15:43:25

前端AI图像智能裁剪终极指南:3步实现精准视觉优化

前端AI图像智能裁剪终极指南:3步实现精准视觉优化 【免费下载链接】frontend-stuff 📝 A continuously expanded list of frameworks, libraries and tools I used/want to use for building things on the web. Mostly JavaScript. 项目地址: https:/…

作者头像 李华