凌的博客

您现在的位置是:首页  > 学无止境 > python

python

PyQt5 窗口移动

2019-10-03python0
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
class Login(QWidget):
    __dragWin = False
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # self.setStyleSheet("QWidget{background:#ffffff;}")
        # 设置窗体无边框
        self.setWindowFlags(Qt.FramelessWindowHint)
        # 设置背景透明
        #self.setAttribute(Qt.WA_TranslucentBackground)

        self.setGeometry(300, 300, 400, 300)
        self.setWindowTitle("登录")
        self.center()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def mousePressEvent(self, e):
        self.__dragWin = True
        self.__dragWin_x = e.x()
        self.__dragWin_y = e.y()
        self.setCursor(QCursor(Qt.OpenHandCursor))  # 更改鼠标图标

    def mouseMoveEvent(self, e):
        if self.__dragWin == True:
            pos = e.globalPos()
            self.move(pos.x()-self.__dragWin_x,pos.y()-self.__dragWin_y)

    def mouseReleaseEvent(self, e):
        self.__dragWin = False
        self.setCursor(QCursor(Qt.ArrowCursor))



if __name__ == "__main__":
    app = QApplication(sys.argv)
    login = Login()
    login.show()
    sys.exit(app.exec_())


如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论