import sys
from PyQt5.Qt import *
class Mwindow(QWidget):
def __init__(self):
super().__init__()
self.resize(500,500)
self.move(250,150)
self.setup_Ui()
def setup_Ui(self):
self.btn = QPushButton(self)
self.btn.setText("点击我")
self.btn.move(230,150)
def mousePressEvent(self, evt) :
self.mousex = evt.globalX()
self.mousey = evt.globalY()
self.btn_x = self.btn.x()
self.btn_y = self.btn.y()
def mouseMoveEvent(self, evt2):
print(self.mousex,self.mousey)
def mouseReleaseEvent(self, evt3) :
# print(self.mouse_x,self.mouse_y)
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Mwindow()
win.setMouseTracking(True)
win.show()
sys.exit(app.exec_())
上面的代码运行后,mouseMoveEvent方法崩溃,Debug:'\'Mwindow\' object has no attribute \'mouse_x\''
把print(self.mouse_x,self.mouse_y)放到 mouseReleaseEvent中可以正常输出self.mouse_x,self.mouse_y。。。。
菜鸡百思不得其姐,求大佬们帮忙看看