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

相关推荐
c1s2d3n4cs9 分钟前
Qt使用QZipWriter和QZipReader来解压、压缩文件
开发语言·qt
huipeng9269 分钟前
第三章线性表+第四章ArrayList与顺序表
java·开发语言
酷讯网络_24087016022 分钟前
【全开源】Java多语言tiktok跨境商城TikTok内嵌商城送搭建教程
java·开发语言·开源
海威的技术博客1 小时前
JS中的原型与原型链
开发语言·javascript·原型模式
WPG大大通1 小时前
基于DIODES AP43781+PI3USB31531+PI3DPX1207C的USB-C PD& Video 之全功能显示器连接端口方案
c语言·开发语言·计算机外设·开发板·电源·大大通
从以前1 小时前
【算法题解】Bindian 山丘信号问题(E. Bindian Signaling)
开发语言·python·算法
high20112 小时前
【Java 基础】-- ArrayList 和 Linkedlist
java·开发语言
1nullptr2 小时前
lua和C API库一些记录
开发语言·lua
Jerry Nan2 小时前
Lua元表
开发语言·lua