简体   繁体   中英

How to send a pressed event to a QPushButton PyQT5?

I need help with something. I'm using PyQT to make a game in Python. So, I'm trying to change the background image of the self.botaoVermelho for 1 second (basically, the button "activates" and after 1 second it "deactivates"), but I don't know how to do it because I need to do this WITHOUT clicking on the button.

This is my code:

class Interface(Mediado):
    def __init__(self, janela):
        self.janela = janela
        self.centralwidget = QtWidgets.QWidget(self.janela)
        self.botaoVermelho = QtWidgets.QPushButton(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(self.janela)
        self.statusbar = QtWidgets.QStatusBar(self.janela)

        self.configuraUi()

    def configuraUi(self):
        self.janela.setObjectName("Genius")
        self.janela.resize(451, 498)
        self.janela.setStyleSheet("background-color:#1d1d1d;")
        self.centralwidget.setObjectName("centralwidget")
        self.botaoVermelho.setGeometry(QtCore.QRect(230, 20, 200, 200))
        self.botaoVermelho.setStyleSheet("#botaoVermelho{\n"
                                         "background-color:transparent;\n"
                                         "border-image: url(\"src/images/bt_vermelho.png\");\n"
                                         "}\n"
                                         "\n"
                                         "#botaoVermelho:pressed{\n"
                                         "background-color:transparent;\n"
                                         "border-image: url(\"src/images/bt_vermelhoAct.png\");\n"
                                         "}")
        self.botaoVermelho.setObjectName("botaoVermelho")
        self.botaoVermelho.raise_()
        self.janela.setCentralWidget(self.centralwidget)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 451, 21))
        self.menubar.setObjectName("menubar")
        self.janela.setMenuBar(self.menubar)
        self.statusbar.setObjectName("statusbar")
        self.janela.setStatusBar(self.statusbar)
        _translate = QtCore.QCoreApplication.translate
        self.janela.setWindowTitle(_translate("Genius", "MainWindow"))
        QtCore.QMetaObject.connectSlotsByName(self.janela)

I'm using QtDesigner/QtCreator to create the design of interface and using a terminal command to convert .ui -> .py

Thanks if you can help me.

You have to use setDown() and a QTimer .

import sys

from PyQt5 import QtCore, QtWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        lay = QtWidgets.QVBoxLayout(self)
        self.botaoVermelho = QtWidgets.QPushButton()
        self.botaoVermelho.setObjectName("botaoVermelho")
        self.botaoVermelho.setStyleSheet("#botaoVermelho{\n"
                                         "border-image: url(\"src/images/bt_vermelho.png\");\n"
                                         "}\n"
                                         "\n"
                                         "#botaoVermelho:pressed{\n"
                                         "border-image: url(\"src/images/bt_vermelhoAct.png\");\n"
                                         "}")
        lay.addWidget(self.botaoVermelho)
        self.emulate_clicked(self.botaoVermelho, 1000)

    def emulate_clicked(self, button, ms):
        button.setDown(True)
        QtCore.QTimer.singleShot(ms, lambda: button.setDown(False))

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM