分隔线学习02

95 阅读2分钟

尝试复现下面的图片

image.png

在进行尝试复现之间,对图片的布局进行分析。有4个水平布局分布在一个垂直布局中。并且水平布局之间使用分隔线进行分隔。

尝试复现结果

Kapture 2025-05-09 at 18.51.37.gif

from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QFrame, QCheckBox, QLineEdit, \
    QGroupBox
import sys
from PyQt6.QtGui import QPixmap, QIcon
from PyQt6.QtCore import Qt


class MainWindows(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui()

    def ui(self):
        self.setWindowTitle("尝试复现")
        self.resize(250, 300)
        self.show()

        ####################################
        # 水平1
        ####################################
        main_layout = QVBoxLayout()
        h_layout1 = QHBoxLayout()
        self.button1 = QPushButton("居左")
        self.button1.setIcon(QIcon(QPixmap(
            "/Users/wangjien/PycharmProjects/pythonGit/PyQt6学习/2_书籍学习/2_2_控件/分隔线/居左.png")))
        self.button1.setStyleSheet("""
                QPushButton {
                    background-color: white;
                    border: none;
                    color: black;
                }
                QPushButton:hover {
                    background-color: lightblue;
                }
                QPushButton:pressed {
                    background-color: lightgreen;
                }
            """)

        self.button2 = QPushButton("居中")
        self.button2.setIcon(QIcon(
            QPixmap("/Users/wangjien/PycharmProjects/pythonGit/PyQt6学习/2_书籍学习/2_2_控件/分隔线/居中.png")))
        self.button2.setStyleSheet("""
                QPushButton {
                    background-color: white;
                    border: none;
                    color: black;
                }
                QPushButton:hover {
                    background-color: lightblue;
                }
                QPushButton:pressed {
                    background-color: lightgreen;
                }
            """)

        self.button3 = QPushButton("居右")
        self.button3.setIcon(QIcon(
            QPixmap("/Users/wangjien/PycharmProjects/pythonGit/PyQt6学习/2_书籍学习/2_2_控件/分隔线/居右对齐.png")))
        self.button3.setStyleSheet("""
                QPushButton {
                    background-color: white;
                    border: none;
                    color: black;
                }
                QPushButton:hover {
                    background-color: lightblue;
                }
                QPushButton:pressed {
                    background-color: lightgreen;
                }
            """)
        h_layout1.addWidget(self.button1)
        h_layout1.addWidget(self.button2)
        h_layout1.addWidget(self.button3)
        # 设置分隔线
        self.fram1 = QFrame()
        self.fram1.setFrameShape(QFrame.Shape.HLine)
        self.fram1.setFrameShadow(QFrame.Shadow.Raised)
        v_layout1 = QVBoxLayout()
        v_layout1.addLayout(h_layout1)
        v_layout1.addWidget(self.fram1)

        ####################################
        # 水平2
        ####################################
        self.button4 = QPushButton("粗体")
        self.button4.setIcon(
            QIcon(QPixmap("/Users/wangjien/PycharmProjects/pythonGit/PyQt6学习/2_书籍学习/2_2_控件/分隔线/粗体.png")))

        self.button5 = QPushButton("斜体")
        self.button5.setIcon(
            QIcon(QPixmap("/Users/wangjien/PycharmProjects/pythonGit/PyQt6学习/2_书籍学习/2_2_控件/分隔线/斜体.png")))

        self.button6 = QPushButton("下划线")
        self.button6.setIcon(
            QIcon(QPixmap("/Users/wangjien/PycharmProjects/pythonGit/PyQt6学习/2_书籍学习/2_2_控件/分隔线/下划线.png")))
        self.fram2 = QFrame()
        self.fram2.setFrameShape(QFrame.Shape.HLine)
        self.fram2.setFrameShadow(QFrame.Shadow.Raised)
        h_layout2 = QHBoxLayout()
        v_layout2 = QVBoxLayout()
        h_layout2.addWidget(self.button4)
        h_layout2.addWidget(self.button5)
        h_layout2.addWidget(self.button6)
        v_layout2.addLayout(h_layout2)
        v_layout2.addWidget(self.fram2)

        ####################################
        # 水平3
        ####################################
        self.checkbox1 = QCheckBox("Readonly")
        self.checkbox1.setCheckable(True)
        self.checkbox2 = QCheckBox("Enabled")
        self.checkbox2.setCheckable(True)
        self.checkbox3 = QCheckBox("ClearButtonEnabled")
        self.checkbox3.setCheckable(True)
        h_layout3 = QHBoxLayout()
        h_layout3.addWidget(self.checkbox1)
        h_layout3.addWidget(self.checkbox2)
        h_layout3.addWidget(self.checkbox3)

        ####################################
        # 水平4
        ####################################
        self.edit1 = QLineEdit()
        self.edit1.setMinimumHeight(120)
        self.edit1.setPlaceholderText("请输入字符串")

        main_layout.addLayout(v_layout1)
        main_layout.addLayout(v_layout2)
        main_layout.addLayout(h_layout3)
        main_layout.addWidget(self.edit1)
        self.group_box = QGroupBox("测试")
        self.group_box.setLayout(main_layout)
        V1_layout = QVBoxLayout()
        V1_layout.addWidget(self.group_box)
        self.setLayout(V1_layout)
        ####################################
        # 信号和槽函数
        ####################################
        self.button1.clicked.connect(self.button1_function)
        self.button2.clicked.connect(self.button2_function)
        self.button3.clicked.connect(self.button3_function)

        self.button4.clicked.connect(self.button4_function)
        self.button5.clicked.connect(self.button5_function)
        self.button6.clicked.connect(self.button6_function)

        self.checkbox1.clicked.connect(self.checkbox1_function)
        self.checkbox2.clicked.connect(self.checkbox2_function)
        self.checkbox3.clicked.connect(self.checkbox3_function)

    def button1_function(self):
        self.edit1.setAlignment(Qt.AlignmentFlag.AlignLeft)

    def button2_function(self):
        self.edit1.setAlignment(Qt.AlignmentFlag.AlignCenter)

    def button3_function(self):
        self.edit1.setAlignment(Qt.AlignmentFlag.AlignRight)

    def button4_function(self):
        current_style = self.edit1.styleSheet()
        new_style = "QLineEdit { font-weight: bold; }"
        if "font-weight: bold;" not in current_style:
            self.edit1.setStyleSheet(current_style + new_style)

    def button5_function(self):
        current_style = self.edit1.styleSheet()
        new_style = "QLineEdit { font-style: italic; }"
        if "font-style: italic;" not in current_style:
            self.edit1.setStyleSheet(current_style + new_style)

    def button6_function(self):
        current_style = self.edit1.styleSheet()
        new_style = "QLineEdit { text-decoration: underline; }"
        if "text-decoration: underline;" not in current_style:
            self.edit1.setStyleSheet(current_style + new_style)

    def checkbox1_function(self):
        self.edit1.setReadOnly(True)

    def checkbox2_function(self):
        self.edit1.setEnabled(True)

    def checkbox3_function(self):
        self.edit1.setStyleSheet("QLineEdit{}")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindows()
    sys.exit(app.exec())