最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyQt6 布局管理器的六種實(shí)現(xiàn)示例

 更新時(shí)間:2025年05月14日 11:13:39   作者:爬無止境  
本文主要介紹了PyQt6 布局管理器的實(shí)現(xiàn)示例,包括QVBoxLayout、QHBoxLayout、QFormLayout、QGridLayout,具有一定的參考價(jià)值,感興趣的可以了解一下

PyQt布局管理器有六種:

  • move()方法布局
  • 垂直布局管理器QVBoxLayout()
  • 水平布局管理器QHBoxLayout()
  • 表單布局管理器QFormLayout()
  • 網(wǎng)格布局管理器QGridLayout()
  • 布局嵌套

move()方法

根據(jù)窗口坐標(biāo)布局,類似Tk的place。

import sys

from PyQt6.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(200,200)

        label1 = QLabel('username:',self)
        entry1 = QLineEdit(self)
        # label2 = QLabel('password', self)
        # entry2 = QLineEdit(self)

        # 布局
        label1.move(40,40)
        entry1.move(80,40)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()

    sys.exit(app.exec())

垂直布局管理器QVBoxLayout()

垂直:Vertical   Horizontal: 水平 

Y 與 V 有點(diǎn)類似,V就是Y 垂直布局.

import sys

from PyQt6.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        label1 = QLabel("username:", self)
        entry1 = QLineEdit(self)
        label2 = QLabel("password:", self)
        entry2 = QLineEdit(self)

        # 布局
        v_layout = QVBoxLayout()
        v_layout.addWidget(label1)
        v_layout.addWidget(entry1)
        v_layout.addWidget(label2)
        v_layout.addWidget(entry2)
        
        self.setLayout(v_layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()

    sys.exit(app.exec())

水平布局管理器QHBoxLayout()

import sys

from PyQt6.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        label1 = QLabel("username:", self)
        entry1 = QLineEdit(self)
        label2 = QLabel("password:", self)
        entry2 = QLineEdit(self)

        # 布局
        h_layout = QHBoxLayout()
        h_layout.addWidget(label1)
        h_layout.addWidget(entry1)
        h_layout.addWidget(label2)
        h_layout.addWidget(entry2)

        self.setLayout(h_layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()

    sys.exit(app.exec())

表單布局管理器QFormLayout()

常用方法

  • addRow(label, widget): 添加一行,包含標(biāo)簽和控件。例如用戶名輸入框或密碼輸入框。

  • setFormAlignment(): 設(shè)置整個(gè)表單的對齊方式。

  • setLabelAlignment(): 設(shè)置標(biāo)簽的對齊方式。

  • addWidget(widget): 添加一個(gè)占用整行的控件,比如按鈕或描述文字。

import sys

from PyQt6.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        label1 = QLabel("username:", self)
        entry1 = QLineEdit(self)
        label2 = QLabel("password:", self)
        entry2 = QLineEdit(self)

        # 布局
        f_layout = QFormLayout()
        f_layout.addRow(label1import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QFormLayout
from PyQt6.QtCore import Qt


class FormExample(QWidget):
    def __init__(self):
        super().__init__()

        # 設(shè)置窗口標(biāo)題和大小
        self.setWindowTitle("Form Alignment Example")
        self.resize(400, 200)

        # 創(chuàng)建表單布局
        form_layout = QFormLayout()

        # 添加標(biāo)簽和輸入框
        form_layout.addRow("Username:", QLineEdit())
        form_layout.addRow("Password:", QLineEdit())

        # 添加按鈕
        login_button = QPushButton("Login")
        form_layout.addWidget(login_button)

        # 設(shè)置表單整體對齊方式
        form_layout.setFormAlignment(Qt.AlignmentFlag.AlignCenter)  # 整體居中
        # 設(shè)置標(biāo)簽對齊方式
        form_layout.setLabelAlignment(Qt.AlignmentFlag.AlignRight)  # 標(biāo)簽右對齊

        # 應(yīng)用布局
        self.setLayout(form_layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = FormExample()
    w.show()
    sys.exit(app.exec())
, entry1)
        f_layout.addRow(label2, entry2)


        self.setLayout(f_layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()

    sys.exit(app.exec())

網(wǎng)格布局管理器QGridLayout()

類似Tk的grid()布局方式

import sys

from PyQt6.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        label1 = QLabel("username:", self)
        entry1 = QLineEdit(self)
        label2 = QLabel("password:", self)
        entry2 = QLineEdit(self)

        # 布局
        grid_layout = QGridLayout()
        grid_layout.addWidget(label1,0,0)
        grid_layout.addWidget(entry1,0,1)
        grid_layout.addWidget(label2,1,0)
        grid_layout.addWidget(entry2,1,1)

        # 應(yīng)用布局
        self.setLayout(grid_layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()

    sys.exit(app.exec())

布局嵌套

布局管理器除了可以添加控件,還可以添加子布局

import sys

from PyQt6.QtWidgets import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        label1 = QLabel("username:", self)
        entry1 = QLineEdit(self)
        label2 = QLabel("password:", self)
        entry2 = QLineEdit(self)

        # 布局
        v_layout = QVBoxLayout()
        h1_layout = QHBoxLayout()
        h1_layout.addWidget(label1)
        h1_layout.addWidget(entry1)

        h2_layout = QHBoxLayout()
        h2_layout.addWidget(label2)
        h2_layout.addWidget(entry2)

        v_layout.addLayout(h1_layout)
        v_layout.addLayout(h2_layout)


        # 應(yīng)用布局
        self.setLayout(v_layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.show()

    sys.exit(app.exec())

到此這篇關(guān)于PyQt6 布局管理器的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyQt6 布局管理器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論

潞城市| 大邑县| 天柱县| 绥化市| 福建省| 宿迁市| 宜宾县| 晴隆县| 富平县| 东海县| 兖州市| 温宿县| 瓦房店市| 巴塘县| 东城区| 荣昌县| 中宁县| 天祝| 南投县| 海城市| 会泽县| 建平县| 荥经县| 长海县| 博罗县| 读书| 广汉市| 黄骅市| 石首市| 嫩江县| 渝中区| 三亚市| 莆田市| 高陵县| 始兴县| 伊宁市| 镶黄旗| 昌乐县| 洪湖市| 偃师市| 顺平县|