• PyQT5 普通按钮互斥选中


    需求:实现按钮互斥选中

    效果

    未点击状态:
    在这里插入图片描述
    点击BT1:
    在这里插入图片描述
    点击BT2:
    在这里插入图片描述

    文件:untitled.py
    代码:

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'untitled.ui'
    #
    # Created by: PyQt5 UI code generator 5.15.9
    #
    # WARNING: Any manual changes made to this file will be lost when pyuic5 is
    # run again.  Do not edit this file unless you know what you are doing.
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(532, 232)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.pushButton = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton.setGeometry(QtCore.QRect(60, 70, 121, 41))
            self.pushButton.setObjectName("pushButton")
            self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_2.setGeometry(QtCore.QRect(200, 70, 121, 41))
            self.pushButton_2.setObjectName("pushButton_2")
            self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_3.setGeometry(QtCore.QRect(350, 70, 121, 41))
            self.pushButton_3.setObjectName("pushButton_3")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 532, 23))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.pushButton.setText(_translate("MainWindow", "BT1"))
            self.pushButton_2.setText(_translate("MainWindow", "BT2"))
            self.pushButton_3.setText(_translate("MainWindow", "BT3"))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    文件:test.py
    代码:

    
    from untitled import Ui_MainWindow
    from PyQt5.QtWidgets import QMainWindow, QApplication
    import sys
    
    
    
    
    class MainFun(Ui_MainWindow, QMainWindow):
        def __init__(self):
            super(MainFun, self).__init__()
            self.setupUi(self)
            # PyQT5 按钮默认颜色为浅灰
            self.button_default_css = "background-color: light gray;"
            self.button_selected_css = "background-color: yellow;"
            # 用于记录上一个被点击的按钮
            self.check_btn = None
            self.pushButton.clicked.connect(self.checked)
            self.pushButton_2.clicked.connect(self.checked)
            self.pushButton_3.clicked.connect(self.checked)
    
    
        def checked(self):
            sender = self.sender()
            if sender == self.check_btn:
                return
            if self.check_btn:
                self.check_btn.setStyleSheet(self.button_default_css)
            sender.setStyleSheet(self.button_selected_css)
            # 记录当前被点击的按钮
            self.check_btn = sender
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ui = MainFun()
        ui.show()
        sys.exit(app.exec_())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    参考博客:
    https://gidsou.com/questions/72628364/how-to-cancel-the-action-of-a-selected-button-qpushbutton-when-a-new-button-is-clicked-in-pyqt5

  • 相关阅读:
    建模杂谈系列175 论交付与共赢
    ChatGPT 帮你写作
    2023届双非计算机硕士算法岗秋招总结
    统计学研硕大数据统计练手11
    selenium转到新页面操作以及使用execute_script执行js代码获取页面元素
    SaaSBase:什么是易客管家SCRM?
    OpenAI董事会秒反悔!奥特曼被求重返CEO职位
    期末复习【微机原理】
    你所不知道的WMS
    CentOS7 设置 nacos 开机启动
  • 原文地址:https://blog.csdn.net/qq_28311921/article/details/136193714