from PyQt5.Qt import *
import sys


class Window(QWidget):
def __init__(self):
super().__init__()
self.Flag=False
self.setWindowTitle("窗口移动学习")
self.resize(500,500)
self.setup_ui()

def setup_ui(self):
pass

def mousePressEvent(self, evt):
#确定两个点(鼠标第一次按下的点)
if evt.button()==Qt.LeftButton:#判断是否为左键执行
self.Flag=True
self.mouse_x=evt.globalX()
self.mouse_y=evt.globalY()

self.origin_x=self.x()
self.origin_y=self.y()

def mouseMoveEvent(self, evt):
if self.Flag==True:
move_x=evt.globalX()-self.mouse_x
move_y=evt.globalY()-self.mouse_y
dest_x=self.origin_x+move_x
dest_y=self.origin_y+move_y
self.move(dest_x,dest_y)
def mouseReleaseEvent(self, QMouseEvent):
self.Flag=False

if __name__=='__main__':
import sys
app=QApplication(sys.argv)\

window=Window()
window.show()
window.setMouseTracking(True)#鼠标跟踪
sys.exit(app.exec_())