mouseMoveEvent奇怪的崩溃

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''

但是在mouseReleaseEvent中self.mouse_x,self.mouse_y就能正常输出

小白求解!

回答

找到问题了,mousepressenent里面加个判断,避免moveevent事件触发时,mousex和mousey变量未赋值引起程序崩溃

以上是 mouseMoveEvent奇怪的崩溃 的全部内容, 来源链接: www.h5w3.com/113850.html

回到顶部