PyQt: 练习 QLayoutPyQt: 练习 QLayoutPyQt: 练习 QLayoutPyQt: 练习 QLayout
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

PyQt: 练习 QLayout

发表 admin at 2022年4月18日
类别
  • Practice
标签
import sys
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("Qt桌面应用程序")
        self.button_1_flag = 1
        self.resize(300, 300)

        self.label_1 = QLabel()
        self.label_1.setText("标签1")
        self.label_1.setStyleSheet("background-color:CornflowerBlue")
        self.label_2 = QLabel()
        self.label_2.setText("标签2")
        self.label_2.setStyleSheet("background-color:DarkSeaGreen")
        self.label_3 = QLabel()
        self.label_3.setText("标签3")
        self.label_3.setStyleSheet("background-color:HotPink")

        self.label_4 = QLabel()
        self.label_4.setText("标签4")
        self.label_4.setStyleSheet("background-color:CornflowerBlue")
        self.label_5 = QLabel()
        self.label_5.setText("标签5")
        self.label_5.setStyleSheet("background-color:DarkSeaGreen")
        self.label_6 = QLabel()
        self.label_6.setText("标签6")
        self.label_6.setStyleSheet("background-color:HotPink")

        self.box_layout_2 = QBoxLayout(QBoxLayout.LeftToRight)
        self.box_layout_2.addWidget(self.label_4)
        self.box_layout_2.addWidget(self.label_5)
        self.box_layout_2.addWidget(self.label_6)

        self.box_layout_1 = QBoxLayout(QBoxLayout.TopToBottom)
        self.box_layout_1.setContentsMargins(10, 20, 30, 40)
        self.box_layout_1.setSpacing(20)
        self.box_layout_1.addWidget(self.label_1)
        self.box_layout_1.addLayout(self.box_layout_2)
        self.box_layout_1.addWidget(self.label_2)
        self.box_layout_1.addWidget(self.label_3)
        self.setLayout(self.box_layout_1)

        self.button_1 = QPushButton()
        self.button_1.setText("隐藏2/6")

        def button_clicked():
            if self.button_1_flag == 1:
                self.label_2.hide()
                self.label_6.hide()
                self.button_1_flag = 2
                self.button_1.setText("显示2/6")
            else:
                self.label_2.show()
                self.label_6.show()
                self.button_1_flag = 1
                self.button_1.setText("隐藏2/6")

        self.button_1.clicked.connect(button_clicked)

        self.box_layout_1.addWidget(self.button_1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

发表回复 取消回复

要发表评论,您必须先登录。

类别

  • Cat
  • Python
  • Django
  • Database
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • Maths/AI
  • PHP/Wordpress
  • Practice
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • SEO
  • English
  • Games
  • Recipes
  • General
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader