【问题标题】:Provide visual feedback to user of application向应用程序用户提供视觉反馈
【发布时间】:2019-10-01 02:48:40
【问题描述】:

我正在制作一个包含 Qlistwidget 的应用程序。用户可以双击列表中的条目,或在已选择项目时按 Enter 键。当用户执行其中任何一项操作时,都会运行一个执行操作的函数,这并不重要。

我想向用户提供视觉反馈,即预期的操作确实已经执行,但要尽可能简单。现在,我不是 UI/UX 人员,但我能想到的直观地向用户显示操作已发生的最佳方法是使 item selection shadow 闪烁/闪烁。 p>

我不想显示文本消息,因为这会占用应用程序的空间,我专门设计它以尽可能少地占用屏幕空间。

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

我正在使用 PyQt5 并在 Windows 上进行开发。

我浏览了 PyQt 文档并用 Google 搜索,但找不到任何东西。

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    你可以创建一个 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_())
    

    【讨论】:

    • 您的示例运行良好,我可以轻松地对其进行编辑,使其更符合我的想象。我还不能让它在我自己的代码中工作,但这很可能是因为我的代码不是最好的。我最终会让它工作的:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    相关资源
    最近更新 更多