PyQt5:MainWindow.move() 采用 1 个位置参数,但给出了 2 个

问题描述 投票:0回答:1

试图在我的窗口顶部有一个自定义标题(QFrame)。我希望能够单击并拖动所述标题,就像您在屏幕上拖动普通窗口一样。当我尝试通过拖动标题实际移动窗口时,错误

MainWindow.move() takes 1 positional argument but 2 were given
出现在控制台中并且程序崩溃。我只向函数传递了一个参数,但它一直在标记错误。

这是导致错误的代码。

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QMainWindow, QHBoxLayout, QVBoxLayout, QSizeGrip
from PyQt5.QtGui import QPainter, QMovie
from PyQt5.QtCore import Qt, QPoint
from PyQt5 import uic, QtWidgets
from untitled import Ui_MainWindow

# Slightly edited with basic names
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        
        ############################################################
        # Window configuration
        ############################################################
        self.setWindowTitle("Project")
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        uic.loadUi("src/ui/untitle.ui", self) # Get ui file
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        ############################################################
        self.borderFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.borderFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.borderFrame.setObjectName("borderFrame")

        # Move window
        def moveWindow(event):
            
            # IF LEFT CLICK MOVE WINDOW
            if event.buttons() == Qt.LeftButton:
                pos = self.pos() + event.globalPos() - self.dragPos
                self.move(pos)
                self.dragPos = event.globalPos()
                event.accept()

        self.ui.borderFrame.mouseMoveEvent = moveWindow

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

if __name__ == "__main__":
    import sys
    app    = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

如果我要添加

self
作为
moveWindow()
的参数,它将给出
event
未通过的错误。

这是标题的代码:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QLineEdit, QMainWindow, QHBoxLayout, QVBoxLayout, QSizeGrip
from PyQt5.QtGui import QPainter, QMovie
from PyQt5.QtCore import Qt, QPoint
from PyQt5 import uic, QtWidgets
from untitled import Ui_MainWindow

class Ui_MainWindow(object):
    def setupUi(self, Window):
        # --- Border frame QFrame
        self.borderFrame = QtWidgets.QFrame(self.centralwidget)
        self.borderFrame.setGeometry(QtCore.QRect(-10, 0, 1261, 36))
        self.borderFrame.setStyleSheet("background-color: rgba(0, 0, 0, 0.65")
python user-interface pyqt pyqt5
1个回答
0
投票

尽管评论者认为您没有提供您应该提供的内容并且您拒绝这样做对获得答案不是很有帮助,但讨论的迂腐和普遍无益的性质是一个很好的例子,说明为什么 StackOverflow 可能很快就会大部分被某些版本的 ChatGPT 取代。

话虽如此,以下是人们要求的 MRI 类型的示例,它有一个可行的解决方案。如果以下内容不能帮助回答您的问题,请询问有关它的具体问题以及它不能回答您的问题的原因:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QFrame


class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # Create the QFrame element and set its properties
        frame = QFrame(self)
        frame.setFrameShape(QFrame.StyledPanel)
        frame.setFrameShadow(QFrame.Raised)
        frame.setFixedHeight(20)
        frame.setStyleSheet("background-color: red")

        # Create a layout and add the frame
        vbox = QVBoxLayout()
        vbox.addWidget(frame)
        vbox.addStretch(1)

        # Set the layout for the window
        self.setLayout(vbox)

        # Set window properties
        self.setWindowTitle('Example')
        self.setGeometry(300, 300, 250, 150)
        self.setWindowFlags(Qt.FramelessWindowHint)

        # Connect the mouse events to the moveWindow function
        frame.mouseMoveEvent = self.moveWindow
        frame.mousePressEvent = self.mousePressEvent

    def mousePressEvent(self, event):
        # Save the mouse position when clicking, as an offset for the movement
        if event.button() == Qt.LeftButton:
            self.offset = event.pos()

    def moveWindow(self, event):
        # Note how this is a method of the Example class, not the frame, nor a function in the constructor
        if event.buttons() == Qt.LeftButton:
            x = event.globalX()
            y = event.globalY()
            x_w = self.offset.x()
            y_w = self.offset.y()
            self.move(x - x_w, y - y_w)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # create the example window with the draggable QFrame
    Example().show()
    sys.exit(app.exec_())

这将创建一个窗口,其中包含一个 QFrame 元素,可以抓住该元素来拖动窗口。

© www.soinside.com 2019 - 2024. All rights reserved.