向应用程序的用户提供视觉反馈

时间:2019-10-01 02:48:40

标签: python pyqt pyqt5

我正在制作一个包含Qlistwidget的应用程序。用户可以双击列表中的一个条目,或者在已经选择一个项目时按Enter键。当用户执行其中的任何一项操作时,就会运行一个执行操作的函数,实际上什么都没关系。

我想向用户提供视觉反馈,表明确实已执行了预期的操作,但要尽可能简单。现在,我不是UI / UX使用者,但是我能想到的直观地向用户显示操作已发生的最佳方法是使项目选择阴影闪烁/闪烁。

我不想显示一条短信,因为它会占用应用程序中的空间,我正在专门设计它来占用尽可能少的屏幕空间。

这可能吗?如果没有的话,还有其他我没想到的方法会一样好。

如标题所述,我正在使用PyQt5并在Windows上进行开发。

我已经浏览了PyQt文档并对其进行了Google搜索,但找不到任何内容。

1 个答案:

答案 0 :(得分:2)

您可以创建一个QVariantAnimation来更改项目的颜色:

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.m_list_widget = QtWidgets.QListWidget()
        self.m_list_widget.itemDoubleClicked.connect(self.on_itemDoubleClicked)
        self.setCentralWidget(self.m_list_widget)

        for i in range(10):
            it = QtWidgets.QListWidgetItem(f"Item-{i}")
            self.m_list_widget.addItem(it)

    @QtCore.pyqtSlot(QtWidgets.QListWidgetItem)
    def on_itemDoubleClicked(self, it):
        self.m_list_widget.clearSelection()
        animation = QtCore.QVariantAnimation(self, duration=5 * 1000)
        animation.setProperty("item", it)

        color1 = QtGui.QColor("white")
        color2 = QtGui.QColor("red")

        colors = []
        number = 5

        color = color1 
        for _ in range(2*number+1):
            colors.append(color)
            color = color1 if color == color2 else color2

        numbers_of_colors = len(colors)
        for i, color in enumerate(colors):
            step = i / numbers_of_colors
            animation.setKeyValueAt(step, color)

        animation.valueChanged.connect(self.on_valueChanged)
        animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)

    @QtCore.pyqtSlot("QVariant")
    def on_valueChanged(self, value):
        animation = self.sender()
        it = animation.property("item")
        if isinstance(value, QtGui.QColor):
            it.setBackground(value)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    w.show()

    sys.exit(app.exec_())
相关问题