PyQT5 普通按钮互斥选中

需求:实现按钮互斥选中

效果

未点击状态:

点击BT1:

点击BT2:

文件:untitled.py

代码:

python 复制代码
# -*- 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"))

文件:test.py

代码:

python 复制代码
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_())

参考博客:

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

相关推荐
Ajiang282473530410 分钟前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
幽兰的天空14 分钟前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024065 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it6 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康6 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神6 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
宅小海7 小时前
scala String
大数据·开发语言·scala