python - PyQt5可拖动无框窗口

标签 python windows python-3.4 pyqt5 qmainwindow

我找到了一个在无框窗口上设置边框的示例,但是它不可拖动。如何使无框窗口可拖动?特别是如果我能看到一个例子,那就太棒了。这是我的示例代码(通常代码较长,这就是为什么有很多库只是不介意它们);

from PyQt5.QtWidgets import (QMessageBox,QApplication, QWidget, QToolTip, QPushButton,
                             QDesktopWidget, QMainWindow, QAction, qApp, QToolBar, QVBoxLayout,
                             QComboBox,QLabel,QLineEdit,QGridLayout,QMenuBar,QMenu,QStatusBar,
                             QTextEdit,QDialog,QFrame,QProgressBar
                             )
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QIcon,QFont,QPixmap,QPalette
from PyQt5.QtCore import QCoreApplication, Qt,QBasicTimer

import sys

class cssden(QMainWindow):
    def __init__(self):
        super().__init__()


        self.mwidget = QMainWindow(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)


        #size
        self.setFixedSize(320, 450)
        self.center


        #label
        self.lbl = QLabel(self)
        self.lbl.setText("test")
        self.lbl.setStyleSheet("background-color: rgb(0,0,0);"
                               "border: 1px solid red;"
                               "color: rgb(255,255,255);"
                               "font: bold italic 20pt 'Times New Roman';")
        self.lbl.setGeometry(5,5,60,40)

        self.show()

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

app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: darkgray;border: 1px solid black}")

ex = cssden()
sys.exit(app.exec_())

最佳答案

您需要自己处理鼠标事件。

  • 我们需要在 mousePressEvent 上添加一个事件,该事件将保留我们上次单击窗口的位置
  • 然后,我们将添加一个mouseMoveEvent,它将计算上次单击的点与当前鼠标位置之间的距离。我们将根据这个距离移动窗口。

这是固定的代码:

import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel


class cssden(QMainWindow):
    def __init__(self):
        super().__init__()

        # <MainWindow Properties>
        self.setFixedSize(320, 450)
        self.setStyleSheet("QMainWindow{background-color: darkgray;border: 1px solid black}")
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.center()
        # </MainWindow Properties>

        # <Label Properties>
        self.lbl = QLabel(self)
        self.lbl.setText("test")
        self.lbl.setStyleSheet("QLabel{background-color: rgb(0,0,0); border: 1px solid red; color: rgb(255,255,255); font: bold italic 20pt 'Times New Roman';}")
        self.lbl.setGeometry(5, 5, 60, 40)
        # </Label Properties>

        self.oldPos = self.pos()
        self.show()

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

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = cssden()
    sys.exit(app.exec_())

关于python - PyQt5可拖动无框窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718329/

相关文章:

C++ 将 FILE * 作为参数传递给函数

python - 更改一个字典会更改字典列表中的所有字典

python - 数据框子集中的字符串索引 - Pandas

python - 如何使用 python 查看目录中每个文件的第一行

windows - Windows 附带的字体许可证?

python - 获取最终重定向的 URL

Python 异步 : reader callback and coroutine communication

python - 在 __str__ 下调用 print(self) 会抛出 RecursionError

python - scikit-optimize 中 cv_results_ 和 best_score_ 中的测试分数是如何计算的?

windows - 在批处理脚本中隐藏命令结果