Python PyQt中的悬停问题

Python PyQt中的悬停问题,python,hover,pyqt,pyqt4,Python,Hover,Pyqt,Pyqt4,我想做悬停。我看了一个例子,然后写了一个脚本,将作为我的程序使用。我面临的一个问题是,只有将鼠标放在按钮的左角,才会出现悬停。我希望它会发生在所有的按钮上,如果我在按钮上移动光标,那么它应该会改变 这是我的密码: from PyQt4 import QtGui, QtCore from PyQt4.QtCore import pyqtSignal import os,sys class HoverButton(QtGui.QToolButton): def enterEvent(sel

我想做悬停。我看了一个例子,然后写了一个脚本,将作为我的程序使用。我面临的一个问题是,只有将鼠标放在按钮的左角,才会出现悬停。我希望它会发生在所有的按钮上,如果我在按钮上移动光标,那么它应该会改变

这是我的密码:

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
import os,sys

class HoverButton(QtGui.QToolButton):
    def enterEvent(self,event):
        print("Enter")
        button.setStyleSheet("background-color:#45b545;")

    def leaveEvent(self,event):
        button.setStyleSheet("background-color:yellow;")
        print("Leave")

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
button = QtGui.QToolButton(widget)
button.setMouseTracking(True)
buttonss =  HoverButton(button)
button.setIconSize(QtCore.QSize(200,200))
widget.show()
sys.exit(app.exec_())

您可能希望
聚焦
模糊
,而不是
进入
离开
只会在鼠标实际进入或离开按钮边界时触发,可能只是短暂的脉冲,而不是切换<代码>焦点和模糊将通过悬停切换。

这就是您要找的吗

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
import os,sys


class Main(QtGui.QWidget):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout = QtGui.QVBoxLayout(self) # layout of main widget

        button =  HoverButton(self) 
        button.setIconSize(QtCore.QSize(200,200))

        layout.addWidget(button) # set your button to the widgets layout
                                 # this will size the button nicely


class HoverButton(QtGui.QToolButton):

    def __init__(self, parent=None):
        super(HoverButton, self).__init__(parent)
        self.setMouseTracking(True)

    def enterEvent(self,event):
        print("Enter")
        self.setStyleSheet("background-color:#45b545;")

    def leaveEvent(self,event):
        self.setStyleSheet("background-color:yellow;")
        print("Leave")

app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

在代码中,按钮中有一个按钮,而嵌套的按钮没有分配给
QLayout
小部件。不过,我不知道为什么要在按钮中添加按钮。我从使用GUI中学到的一件事是,如果您将代码模块化,这将非常容易。现在,您可以使用此自定义按钮并将其应用于其他地方。

您应该将样式表用作

QToolButton:hover
{
        background-color: rgb(175,175,175);
}

我想在鼠标启动时更改按钮的颜色。通过使用QSS样式表并为
:hover
属性应用不同的样式,您可以更轻松地执行此操作。查看并感谢您的回复,但我如何使用我尝试过的。setStyleSheet(“hover:blue;”)但它对Mehanks不起作用Jeff这正是我想要的,非常感谢