如何在 PyQt5 中单击鼠标时更改图标 [英] How to change icon while mouse is clicking in PyQt5

查看:311
本文介绍了如何在 PyQt5 中单击鼠标时更改图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PyQt5 上使用 python 3.6.我希望按钮默认为 off.png,然后当我点击它时,图标变为 click.png,当我释放点击时,它变为 On.png目前默认是off.png,但是点击它并没有将图标变为click.png,释放成功将图标变为on.png

I'm using python 3.6 on PyQt5. I want the button to be off.png by default, then when I'm clicking on it the icon becomes clicking.png, and when I release the click it becomes On.png Currently, it is off.png by default, but clicking on it does not change the icon to clicking.png, and releasing does successfully change the icon to on.png

但奇怪的是,当我点击窗口(而不是实际按钮)时,图标切换到 click.png 并在我释放它后返回 off.png.如何修复中间三行代码,以便在我按住按钮时图标变为 click.png,然后在我释放它时变为 on.png?谢谢

Strangely enough though, when I'm clicking on the window (instead of the actual button), the icon switches to clicking.png and goes back to off.png once I release it. How can I fix the middle three lines of code so that the icon becomes clicking.png when I'm holding the click on the button, and then become on.png once I release it? Thanks

注意:如果您的计算机上没有保存任何图像,我认为代码不会按原样运行.

Note: I don't think the code would run as is if you don't have any of the images saved on your computer.

我尝试过处理不同的模式/状态,但仍然无法让它工作

I've tried messing around with the different modes/states but still couldn't get it to work

self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 180, 271, 121))
self.pushButton.setStyleSheet("")
self.pushButton.setText("")
icon = QtGui.QIcon()

icon.addPixmap(QtGui.QPixmap("images/Clicking.png"))
icon.addPixmap(QtGui.QPixmap("images/Off.png"), QtGui.QIcon.Active)
icon.addPixmap(QtGui.QPixmap("images/On.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)


self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(300, 300))
self.pushButton.setCheckable(True)
self.pushButton.setAutoRepeatDelay(400)
self.pushButton.setDefault(False)
self.pushButton.setObjectName("pushButton")

推荐答案

您将小部件的状态与按钮的点击状态混淆了.以QIcon使用widget的状态为例,正常状态是用户不与widget交互时,选中状态是用户选择widget时,disabled状态是widget被禁用时,以及活动状态是用户与小部件交互时的状态.另一方面,根据您的逻辑,无法知道按下之前和释放之后的状态.

You are confusing the states of the widget with the states of the button with respect to the clicked. In the case of QIcon uses the status of the widget for example the normal state is when the user does not interact with the widget, the selected state is when the user selects the widget, the disabled state is when the widget is disabled, and the active state is when the user interacts with the widget. On the other hand with your logic it is not possible to know the status before the pressed and after the release.

解决方案是实现自定义按钮:

The solution is to implement a custom button:

# ...

class PushButton(QtWidgets.QPushButton):
    def __init__(self, parent=None):
        super(PushButton, self).__init__(parent)
        self.setIcon(QtGui.QIcon("images/Off.png"))

    def mousePressEvent(self, event):
        super(PushButton, self).mousePressEvent(event)
        self.setIcon(QtGui.QIcon("images/Clicking.png"))

    def mouseReleaseEvent(self, event):
        super(PushButton, self).mouseReleaseEvent(event)
        self.setIcon(
            QtGui.QIcon("images/On.png" if self.isChecked() else "images/Off.png")
        )

# ...
self.pushButton = PushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(160, 180, 271, 121))
self.pushButton.setStyleSheet("")
self.pushButton.setText("")
self.pushButton.setIconSize(QtCore.QSize(300, 300))
self.pushButton.setCheckable(True)
self.pushButton.setAutoRepeatDelay(400)
self.pushButton.setDefault(False)
self.pushButton.setObjectName("pushButton")
# ...

这篇关于如何在 PyQt5 中单击鼠标时更改图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆