Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 鼠标按下按钮不工作_Python_Python 3.x_Pyqt5 - Fatal编程技术网

Python 鼠标按下按钮不工作

Python 鼠标按下按钮不工作,python,python-3.x,pyqt5,Python,Python 3.x,Pyqt5,因此,在我的qtgui中,我想知道何时有人单击(任何QObject或窗口上), 所以我用的是MousePressEvent。 但是,当我单击QPushbuttons按钮时,事件无法识别 我的代码有一个更简单的版本 from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import * from PyQt5.QtWidgets import * import os from PyQt5.QtWidgets import QMainW

因此,在我的qtgui中,我想知道何时有人单击(任何QObject或窗口上), 所以我用的是MousePressEvent。 但是,当我单击QPushbuttons按钮时,事件无法识别

我的代码有一个更简单的版本

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import os
from PyQt5.QtWidgets import QMainWindow

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super(Ui_MainWindow,self).__init__()

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.resize(1920, 1080)
        self.setStyleSheet("\n"
"")
        self.centralwidget = QtWidgets.QWidget(self)
        #self.centralwidget.setStyleSheet("#centralwidget{background-image: url(:/images/assets/back1.jpg);}")
        
        self.centralwidget.setObjectName("centralwidget")
        self.timer = 5000
        
        self.gridLayout_3 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")

        self.but1 = QtWidgets.QPushButton(self.centralwidget)
        self.but1.setGeometry(QtCore.QRect(1500, 500, 301, 51))
        self.but1.setMouseTracking(True)
        self.but1.setStyleSheet("QPushButton{\n"
                "border-style: outset;""\n"
                " border-radius: 10px;"
                "border-width: 2px;"
                "border-color: gray;"
                "font: 18pt \"Adobe Pi Std\";\n"
                "color: rgb(255, 255, 255);}\n"
                "QPushButton:pressed { border-style : inset; border-color:black}")
        self.but1.setObjectName("but1")
        self.but1.clicked.connect(lambda : self.addtimer())
        self.setCentralWidget(self.centralwidget)
        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)


  def mousePressEvent(self,event):
        #super(Ui_MainWindow,self).mousePressEvent(event) i tried this but change nothing
        print("Clicked")

  def addtimer(self):
       self.timer+=5000
       print(self.timer)

def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.but1.setText(_translate("MainWindow", "Explore!"))



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    
    ui = Ui_MainWindow()
    ui.setupUi()
    ui.show()
    sys.exit(app.exec_())


我高度相信这个问题是由不良的先天性引起的,但我不知道如何解决它

如果你想知道为什么我想知道什么时候有人点击,那么主要目的是设置一个Qtimer,如果在x时间之后没有点击,它将关闭窗口


提前感谢

您需要做的是确定要检查哪种鼠标事件,然后转到pyqt5文档并搜索该事件的作用(这就是我所做的),然后编写逻辑代码以检测该事件

#Below your other self.but1... u add this one to call the function each time u click
self.but1.clicked.connect(lambda: self.mousePressEvent(QMouseEvent.MouseButtonPress))

def mousePressEvent(self, event):
    #Here you check if the event u receive its the one you want to track
    if event == QMouseEvent.MouseButtonPress:
        print("Clicked")

希望此解决方案对您有所帮助。

很好,非常有效,谢谢!然而,这个方法看起来有点“沉重”。事实上,我可能有很多按钮。我会等一会儿,如果没有人给出另一个解决方案,我会把你的答案标记为接受。再次感谢当按下按钮时,它与QMouseEvent.MouseButtonPress匹配,如果您查看文档,它与数字“2”相同。根据你的问题快速回答。