PyQt5是基于Python语言的GUI编程工具包。而要实现在鼠标悬停时给选中的单选按钮设置皮肤,可以按照如下步骤进行:
1. 安装PyQt5
在命令行中输入以下命令:
pip install PyQt5
2. 导入相关库
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QButtonGroup
3. 创建GUI界面
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 300, 200)
self.setWindowTitle('选项按钮皮肤设置')
self.btnGroup = QButtonGroup(self)
self.btnGroup.buttonClicked[int].connect(self.on_btn_clicked)
self.blueButton = QRadioButton('蓝色', self)
self.btnGroup.addButton(self.blueButton, 1)
self.blueButton.setGeometry(20, 50, 80, 30)
self.redButton = QRadioButton('红色', self)
self.btnGroup.addButton(self.redButton, 2)
self.redButton.setGeometry(120, 50, 80, 30)
self.yellowButton = QRadioButton('黄色', self)
self.btnGroup.addButton(self.yellowButton, 3)
self.yellowButton.setGeometry(220, 50, 80, 30)
self.show()
该界面包含了三个单选按钮,分别是蓝色、红色和黄色。
4. 编写鼠标悬停事件
def on_btn_clicked(self, id):
if id == 1:
self.blueButton.setStyleSheet("QRadioButton::indicator {background-color : blue;}")
elif id == 2:
self.redButton.setStyleSheet("QRadioButton::indicator {background-color : red;}")
elif id == 3:
self.yellowButton.setStyleSheet("QRadioButton::indicator {background-color : yellow;}")
def enterEvent(self, event):
btnId = self.btnGroup.checkedId()
if btnId == 1:
self.blueButton.setStyleSheet("QRadioButton::indicator {background-color : #a0c1f5;}")
elif btnId == 2:
self.redButton.setStyleSheet("QRadioButton::indicator {background-color : #ff8080;}")
elif btnId == 3:
self.yellowButton.setStyleSheet("QRadioButton::indicator {background-color : #fff68f;}")
在该函数中,首先会判断选中的单选按钮是哪个,从而设置对应的样式。其次,监测到鼠标悬停事件时,会获取当前选中的单选按钮的id,并设置对应的样式。
5. 运行程序
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
完整代码如下:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QButtonGroup
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 300, 200)
self.setWindowTitle('选项按钮皮肤设置')
self.btnGroup = QButtonGroup(self)
self.btnGroup.buttonClicked[int].connect(self.on_btn_clicked)
self.blueButton = QRadioButton('蓝色', self)
self.btnGroup.addButton(self.blueButton, 1)
self.blueButton.setGeometry(20, 50, 80, 30)
self.redButton = QRadioButton('红色', self)
self.btnGroup.addButton(self.redButton, 2)
self.redButton.setGeometry(120, 50, 80, 30)
self.yellowButton = QRadioButton('黄色', self)
self.btnGroup.addButton(self.yellowButton, 3)
self.yellowButton.setGeometry(220, 50, 80, 30)
self.show()
def on_btn_clicked(self, id):
if id == 1:
self.blueButton.setStyleSheet("QRadioButton::indicator {background-color : blue;}")
elif id == 2:
self.redButton.setStyleSheet("QRadioButton::indicator {background-color : red;}")
elif id == 3:
self.yellowButton.setStyleSheet("QRadioButton::indicator {background-color : yellow;}")
def enterEvent(self, event):
btnId = self.btnGroup.checkedId()
if btnId == 1:
self.blueButton.setStyleSheet("QRadioButton::indicator {background-color : #a0c1f5;}")
elif btnId == 2:
self.redButton.setStyleSheet("QRadioButton::indicator {background-color : #ff8080;}")
elif btnId == 3:
self.yellowButton.setStyleSheet("QRadioButton::indicator {background-color : #fff68f;}")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
在该程序中,我们实现了在鼠标悬停时给选中的单选按钮设置不同的皮肤。我们演示了三种皮肤:蓝色、红色和黄色。同时,我们通过继承QWidget类,并使用该类的enterEvent函数,实现了鼠标悬停事件的判断。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyQt5 – 如何在鼠标悬停时给选中的单选按钮设置皮肤 - Python技术站