PyQt5 鼠标跟踪不起作用
PyQt5 mousetracking not working
我找到了一个例子,当鼠标悬停在 QPushButton 上时,它会改变它的图标。我试图将其转换为我的代码,但存在一些问题。首先检查我找到的例子,它真的很短。 http://paste.ubuntu.com/17302717/
如果鼠标在按钮上,这些代码会更改按钮的图标。这是我引发错误的代码
return QPushButton.mouseMoveEvent(QPushButton, event)
TypeError: QPushButton.mouseMoveEvent(QMouseEvent): first argument of unbound method must have type '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)
,现在没有错误,但图标并没有改变。为什么图标没有变化?
您的代码有一些问题,即导入问题。但主要问题是你不需要这个: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?(我的解决方案是基于这个方法)
Pyqt Mouse hovering on a QPushButton
我找到了一个例子,当鼠标悬停在 QPushButton 上时,它会改变它的图标。我试图将其转换为我的代码,但存在一些问题。首先检查我找到的例子,它真的很短。 http://paste.ubuntu.com/17302717/
如果鼠标在按钮上,这些代码会更改按钮的图标。这是我引发错误的代码
return QPushButton.mouseMoveEvent(QPushButton, event) TypeError: QPushButton.mouseMoveEvent(QMouseEvent): first argument of unbound method must have type '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)
,现在没有错误,但图标并没有改变。为什么图标没有变化?
您的代码有一些问题,即导入问题。但主要问题是你不需要这个: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?(我的解决方案是基于这个方法)
Pyqt Mouse hovering on a QPushButton