gpt4 book ai didi

python - PyQt5鼠标悬停功能

转载 作者:行者123 更新时间:2023-12-01 01:37:08 24 4
gpt4 key购买 nike

嗨,我是 Python 和 PyQt5 的新手。我想为 DVR 客户端制作一个 GUI。我遇到了一个无法解决的问题,我需要通过将鼠标悬停在标签图片上来更改标签图片,我该怎么做?

from PyQt5 import QtCore, QtWidgets

class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)

lay = QtWidgets.QVBoxLayout(self)

self.btn = QtWidgets.QPushButton("Press me")
self.btn.installEventFilter(self)

lay.addWidget(self.btn)
lay.addWidget(QtWidgets.QLineEdit())

def eventFilter(self, obj, event):
if obj == self.btn and event.type() == QtCore.QEvent.HoverEnter:
self.onHovered()
return super(Widget, self).eventFilter(obj, event)

def onHovered(self):
print("hovered")


if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

最佳答案

使用样式表语法

from PyQt5 import QtCore, QtWidgets

StyleSheet = '''
QPushButton#BlueButton {
background-color: #2196f3;
/* Ограничьте минимальный размер */
min-width: 96px;
max-width: 96px;
min-height: 96px;
max-height: 96px;
border-radius: 48px; /* круглый */
}

QPushButton#BlueButton:hover {
background-color: #64b5f6;
color: #fff;
}

QPushButton#BlueButton:pressed {
background-color: #bbdefb;
}
'''


class Window2(QtWidgets.QDialog):
def __init__(self, value, parent=None):
super().__init__(parent)
self.setGeometry(750, 100, 300, 300)
self.parent = parent
self.setWindowTitle('Window2')
self.setWindowIcon(self.style().standardIcon(QtWidgets.QStyle.SP_FileDialogInfoView))

label1 = QtWidgets.QLabel(value)
self.button = QtWidgets.QPushButton()
self.button.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_ArrowLeft))
self.button.setIconSize(QtCore.QSize(200, 200))

layoutV = QtWidgets.QVBoxLayout()
self.pushButton = QtWidgets.QPushButton(self)
self.pushButton.setStyleSheet('background-color: rgb(0,0,255); color: #fff')
self.pushButton.setText('Click me!')
self.pushButton.clicked.connect(self.goMainWindow)
layoutV.addWidget(self.pushButton)

layoutH = QtWidgets.QHBoxLayout()
layoutH.addWidget(label1)
layoutH.addWidget(self.button)
layoutV.addLayout(layoutH)
self.setLayout(layoutV)

def goMainWindow(self):
self.parent.show()
self.close()


class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setGeometry(400, 100, 300, 300)

hlay = QtWidgets.QHBoxLayout()
hlay.addStretch(1)

vlay = QtWidgets.QVBoxLayout(self)
vlay.addStretch(1)

self.btn = QtWidgets.QPushButton("Press me", objectName="BlueButton")
self.btn.clicked.connect(self.btn_onClick)
self.btn.installEventFilter(self)
hlay.addWidget(self.btn)
hlay.addStretch(1)

vlay.addLayout(hlay)
self.lineEdit = QtWidgets.QLineEdit("Type here what you want to transfer for [Window2].")
vlay.addWidget(self.lineEdit)
vlay.addStretch(1)
self.setLayout(vlay)

def eventFilter(self, obj, event):
if obj == self.btn and event.type() == QtCore.QEvent.HoverEnter:
self.onHovered()
return super(Widget, self).eventFilter(obj, event)

def onHovered(self):
#print("hovered")
self.btn.setText("Ok, \nbutton onHovered") # <---

def leaveEvent(self, e):
self.btn.setText("Press me") # <---

@QtCore.pyqtSlot()
def btn_onClick(self):
self.cams = Window2(self.lineEdit.text(), self)
self.cams.show()

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)

app.setStyleSheet(StyleSheet) # <---

w = Widget()
w.show()
sys.exit(app.exec_())

enter image description here

关于python - PyQt5鼠标悬停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52291734/

24 4 0
文章推荐: python - Django TestCase 在测试完成运行后不会销毁测试数据
文章推荐: jquery - 单击时增加/减小图像尺寸并保留纵横比
文章推荐: jQuery Spy(垂直滚动条): after 4th iteration, 列表项动画向下,增加容器的高度
文章推荐: jquery - 如何使用 ? 强制关闭 jQuery UI Accordion
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com