PyQt: 复选框PyQt: 复选框PyQt: 复选框PyQt: 复选框
  • 首页
  • 博客
  • 文件
  • 书签
  • 分析
  • 登录
Search
Generic filters

PyQt: 复选框

Published by admin at 2022年4月3日
Categories
  • Practice
Tags
import sys
from PyQt5.Qt import *


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

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

        # 设置信息显示标签
        self.label_1 = QLabel(self)
        self.label_1.move(20, 30)
        self.label_2 = QLabel(self)
        self.label_2.move(20, 60)

        # 添加复选框
        for i in range(2):
            # 定义组和组名
            self.check_box_button_group = QButtonGroup(self)
            self.check_box_button_group.setObjectName(f"{i + 1}")
            self.check_box_button_group.setExclusive(False)
            # 定义复选框
            for j in range(3):
                self.check_box_button = QCheckBox(f"单选{i + 1}组{j + 1}", self)
                self.check_box_button.setTristate(True)
                self.check_box_button.setObjectName(f"{i + 1}_{j + 1}")
                self.check_box_button.move(20 + j * 80, 100 + i * 50)
                # 将复选框加入组中,编号前一位代表组,后一位代表复选框
                self.check_box_button_group.addButton(self.check_box_button, int(f"{i + 1}{j + 1}"))

    def user_action(self):
        def button_clicked(button_id):
            # 点击的复选框内容
            button_text = self.findChild(QButtonGroup, str(button_id)[0]).button(button_id).text()
            self.label_1.setText("点击了 " + button_text)
            self.label_1.adjustSize()

        def button_state_changed(a):
            # 所有复选框的状态
            k = dict()
            for i in self.findChildren(QButtonGroup):
                list = []
                for j in i.buttons():
                    list.append(j.checkState())
                k[i.objectName()] = list
            self.label_2.setText("复选框状态:" + str(k))
            self.label_2.adjustSize()

        # 连接组被点击的信号与槽,指定调用槽函数时传入复选框ID
        for i in self.findChildren(QButtonGroup):
            i.buttonClicked[int].connect(button_clicked)
            for j in i.buttons():
                j.stateChanged.connect(button_state_changed)


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

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Categories

  • 猫
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2022 Alaica Blog support@alaica.com