临时更改标签样式(模拟按钮)

2024-09-27 20:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我要征求你们的意见,因为我没什么主意了。我在做可点击标签。我已经完成了一些“ClickableLabel”类并处理了mouseover事件—它会更改标签边框,并在鼠标离开时返回到正常状态。你知道吗

现在我希望它在标签上有一个自定义的发光效果,但我希望它在点击后返回到正常状态,比如说0.5秒。你知道吗

我希望我的标签上有一个模仿按钮的图像。time.sleep工作不太好,尤其是在垃圾邮件点击时,它会冻结应用程序主线程。你知道吗

希望我不是在重新发明方向盘,但据我所知,这是一条路要走。你知道吗

这是示例代码,请回答。你知道吗

from PySide2.QtWidgets import QLabel, QSizePolicy, QGraphicsDropShadowEffect
from PySide2.QtGui import QPixmap
from PySide2.QtCore import (Signal, QEvent, QObject, QRect)


class ClickableLabel(QLabel):
    def __init__(self, pic_path, width, height, border_color, click_function):
        super(ClickableLabel, self).__init__()

        # Setting the picture path and setting pixmap to label
        self.pic_path = pic_path
        self.pixmap = QPixmap(self.pic_path)
        self.setPixmap(self.pixmap)

        # Set the size
        self.setFixedSize(width, height)

        # Enable tracking and assign function
        self.setMouseTracking(True)
        self.click_function = click_function

        # Set default 
        if border_color is None:
            self.border_color = 'lightblue'
        else:
            self.border_color = border_color

    def mouseMoveEvent(self, event):
        # event.pos().x(), event.pos().y()
        self.setStyleSheet("border: 1px solid " + str(self.border_color) + ";")

    def leaveEvent(self, event):
        # event.pos().x(), event.pos().y()
        self.setStyleSheet("border: None")

    def mousePressEvent(self, event):
        self.click_function()

        effect = QGraphicsDropShadowEffect(self)
        effect.setOffset(0, 0)
        effect.setBlurRadius(20)
        effect.setColor(self.border_color)
        self.setGraphicsEffect(effect)

Tags: pathfromposselfeventdeffunction标签
1条回答
网友
1楼 · 发布于 2024-09-27 20:18:31

是的,这是可以实现的,因为您有:

private final HotelsServiceInt hotelsServiceInt;

您可以在测试类中模拟hotelsServiceInt,并返回模拟结果。在调用doSomeBusinessLogic之前,只需返回一个模拟列表,就可以开始了。 您还需要更改@Mock@InjectMock的变量类型,如下所示:

   @Mock
   var hotelsServiceInitMock: HotelsServiceInt = _
   @InjectMocks
   var hotelsService: HotelsService = _
   //then do this:
   doReturn(list).when(hotelsServiceInitMock).getHotels(
                 Matchers.any(), Matchers.any())

现在,您应该能够获得所需的结果

相关问题 更多 >

    热门问题