pyqt5 鼠标悬停按钮变色
时间: 2023-12-18 11:27:22 浏览: 335
Python中PyQt5/PySide2的按钮控件使用实例
以下是一个pyqt5鼠标悬停按钮变色的例子:
```python
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
from PyQt5.QtGui import QPalette, QColor
class HoverButton(QPushButton):
def __init__(self, parent=None):
super(HoverButton, self).__init__(parent)
self.setMouseTracking(True)
def enterEvent(self, event):
self.setStyleSheet("background-color: yellow;")
def leaveEvent(self, event):
self.setStyleSheet("background-color: none;")
if __name__ == '__main__':
app = QApplication([])
widget = QWidget()
layout = QVBoxLayout(widget)
button = HoverButton(widget)
button.setText("Hover over me!")
layout.addWidget(button)
widget.show()
app.exec_()
```
当鼠标悬停在按钮上时,按钮的背景颜色将变为黄色。当鼠标离开按钮时,按钮的背景颜色将恢复为默认值。
阅读全文