我找到了一个示例,当鼠标悬停在QPushButton图标上时会更改它。我试图将它转换为我的代码,但有一些问题。首先检查我发现的例子,它真的很短。 http://paste.ubuntu.com/17302717/
如果鼠标在按钮上,这些代码会更改按钮的图标。这是我的代码引发错误
返回QPushButton.mouseMoveEvent(QPushButton,event) TypeError:QPushButton.mouseMoveEvent(QMouseEvent):未绑定方法的第一个参数必须具有类型'QPushButton'
from PyQt5.QtWidgets import QApplication,QPushButton,QWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QSize
import PyQt5.QtWidgets,PyQt5.QtCore,sys
class cssden(QMainWindow):
def __init__(self):
super().__init__()
self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setMouseTracking(True)
self.setFixedSize(1400,923)
#Button
self.mbutton = QPushButton(self)
self.mbutton.setStyleSheet("background-color: rgb(30,30,30);"
"background-image: url('resources/twitter-logo.png');"
"border: 3px solid black;"
"background-position: center;"
)
self.mbutton.setGeometry(2,300,110,60)
self.mbutton.clicked.connect(self.yaz)
self.show()
def mouseMoveEvent(self, event):
if event.pos().x()>self.mbutton.width()-10 or event.pos().y()>self.mbutton.height()-10\
or event.pos().x() < 10 or event.pos().y()< 10:
bmp = QIcon("1.png")
self.mbutton.setIcon(bmp)
else:
bmp = QIcon('2.png')
self.mbutton.setIcon(bmp)
self.mbutton.setIconSize(QSize(200,200))
return QPushButton.mouseMoveEvent(self, event)
def yaz(self):
print ("button pressed")
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 2px solid rgb(20,20,20)}")
ex = cssden()
sys.exit(app.exec_())
我不明白问题出在哪里。我尝试将return QPushButton.mouseMoveEvent(self, event)
更改为return QPushButton.mouseMoveEvent(QPushButton, event)
和其他版本,但没有奏效。我缺少什么,我该如何解决这个问题?
编辑:我将self.setMouseTracking(True)
更改为self.mbutton.setMouseTracking(True)
并且现在没有错误,但图标无论如何都没有改变。为什么图标没有改变?
答案 0 :(得分:2)
您的代码存在一些问题,即使用导入。但主要问题是您不需要:return QPushButton.mouseMoveEvent(self, event)
请尝试对您的代码进行以下更正:
from PyQt5.QtWidgets import QApplication,QPushButton,QWidget from
PyQt5.QtGui import QIcon from PyQt5.QtCore import QSize from PyQt5
import QtCore, QtWidgets, QtGui
import PyQt5.QtWidgets,PyQt5.QtCore,sys
class cssden(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setMouseTracking(True)
self.setFixedSize(1400,923)
#Button
self.mbutton = QPushButton(self)
self.mbutton.setStyleSheet("background-color: rgb(30,30,30);"
"background-image: url('resources/twitter-logo.png');"
"border: 3px solid black;"
"background-position: center;"
)
self.mbutton.setGeometry(2,300,110,60)
self.mbutton.clicked.connect(self.yaz)
self.show()
def mouseMoveEvent(self, event):
if event.pos().x()>self.mbutton.width()-10 or event.pos().y()>self.mbutton.height()-10\
or event.pos().x() < 10 or event.pos().y()< 10:
bmp = QIcon("1.png")
self.mbutton.setIcon(bmp)
else:
print(1)
bmp = QIcon('2.png')
self.mbutton.setIcon(bmp)
self.mbutton.setIconSize(QSize(200,200))
# return self.mbutton.mouseMoveEvent(event)
def yaz(self):
print ("button pressed")
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color:
rgb(30,30,30);border: 2px solid rgb(20,20,20)}")
ex = cssden() sys.exit(app.exec_())
无论如何,我并不真正理解你想要达到的目标。如果您需要为按钮创建某种悬停效果,还有其他更好的方法。例如这一个:
from PyQt5.QtWidgets import QApplication,QPushButton,QWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QSize
from PyQt5 import QtCore, QtWidgets, QtGui
import PyQt5.QtWidgets,PyQt5.QtCore,sys
class HoverButton(QPushButton):
mouseHover = QtCore.pyqtSignal(bool)
def __init__(self, parent=None):
QPushButton.__init__(self, parent)
self.setMouseTracking(True)
def enterEvent(self, event):
self.mouseHover.emit(True)
bmp = QIcon("1.png")
self.setIcon(bmp)
self.setIconSize(QSize(200,200))
def leaveEvent(self, event):
self.mouseHover.emit(False)
bmp = QIcon("2.png")
self.setIcon(bmp)
self.setIconSize(QSize(200,200))
class cssden(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# self.mwidget = QMainWindow(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setMouseTracking(True)
self.setFixedSize(1400, 923)
#Button
self.mbutton = HoverButton(self)
self.mbutton.setStyleSheet("background-color: rgb(30,30,30);"
"background-image: url('resources/twitter-logo.png');"
"border: 3px solid black;"
"background-position: center;"
)
self.mbutton.setGeometry(2,300,110,60)
self.mbutton.clicked.connect(self.yaz)
self.show()
def yaz(self):
print("button pressed")
app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 2px solid rgb(20,20,20)}")
ex = cssden()
sys.exit(app.exec_())
我会建议以下答案:
PyQT how to make a QEvent.Enter on QPushbutton?(我的解决方案基于此方法)