Помощь с передвижением безрамочного окна в PySide6

Использую код для передвижения окна:

 class MoveWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.old_pos = None

    # вызывается при нажатии кнопки мыши
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.old_pos = event.pos()

    # вызывается при отпускании кнопки мыши
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.old_pos = None

    # вызывается всякий раз, когда мышь перемещается
    def mouseMoveEvent(self, event):
        if not self.old_pos:
            return
        delta = event.pos() - self.old_pos
        self.move(self.pos() + delta)

и далее подключая его к программе:

    # Create the Qt Application
    app = QApplication(sys.argv)
    w = MoveWindow()
    w.show()
#    Form = QWidget()
    # Create and show the form
#   login = Ui_Form()
#    login.setupUi(Form)
#    Form.show()
    # Run the main Qt loop
    sys.exit(app.exec())

получаю ошибку:

Process finished with exit code -1073740791 (0xC0000409)

Что это может значить и как это возможно пофиксить?


Upd: минимальный воспроизводимый пример:

class Ui_Form(object):
    def setupUi(self, Form):

        if not Form.objectName():
            Form.setObjectName(u"Form")
        centerPoint = QScreen.availableGeometry(QApplication.primaryScreen()).center()
        self.width = 862
        self.height = 660
        Form.resize(self.width, self.height)
        Form.move(centerPoint.x() - (self.width / 2), centerPoint.y() - (self.height / 2))
        Form.setWindowFlags(Qt.FramelessWindowHint)
        Form.setAttribute(Qt.WA_TranslucentBackground)
        self.widget = QWidget(Form)
        self.widget.setObjectName(u"widget")
        self.widget.setGeometry(QRect(70, 50, 771, 591))
        self.left_back = QLabel(self.widget)
        self.left_back.setObjectName(u"left_back")
        self.left_back.setGeometry(QRect(90, 40, 351, 461))
        self.left_back.setStyleSheet(u"border: 1px solid white;")
        self.left_back.setPixmap(QPixmap(u"img/background-login.jpg"))
        self.left_back.setScaledContents(True)
        self.right_back = QLabel(self.widget)
        self.right_back.setObjectName(u"right_back")
        self.right_back.setGeometry(QRect(410, 50, 321, 441))
        font = QFont()
        font.setPointSize(20)
        self.right_back.setFont(font)
        self.right_back.setStyleSheet(u"background-color: rgb(255, 204, 153);\n"
                                      "border: 2px solid transparent;\n"
                                      "border-radius: 14px;\n"
                                      "")
        self.enter_your_label = QLabel(self.widget)
        self.enter_your_label.setObjectName(u"enter_your_label")
        self.enter_your_label.setGeometry(QRect(460, 160, 251, 81))

        font1 = QFont()
        font1.setFamilies([u"Alef"])
        font1.setPointSize(17)
        font1.setItalic(False)
        self.enter_your_label.setFont(font1)
        self.enter_your_label.setAlignment(Qt.AlignCenter)
        self.input_key = QLineEdit(self.widget)
        self.input_key.setObjectName(u"input_key")
        self.input_key.setGeometry(QRect(460, 240, 251, 21))
        font2 = QFont()
        font2.setFamilies([u"Alef"])
        font2.setPointSize(11)
        self.input_key.setFont(font2)
        self.input_key.setMouseTracking(True)
        self.input_key.setFocusPolicy(Qt.ClickFocus)
        self.input_key.setStyleSheet(u"background-color: transparent;\n"
                                     "border: 2px solid black;\n"
                                     "border-radius: 3px;\n"
                                     "color: rgb(59, 59, 59);")
        self.input_key.setFrame(False)
        self.input_key.setAlignment(Qt.AlignCenter)
        self.loginButton = QPushButton(self.widget)
        self.loginButton.setObjectName(u"loginButton")
        self.loginButton.setGeometry(QRect(460, 300, 251, 61))
        font3 = QFont()
        font3.setPointSize(15)
        self.loginButton.setFont(font3)
        self.loginButton.setAutoFillBackground(False)
        self.loginButton.setStyleSheet(u"QPushButton#loginButton {\n"
                                       "    background-color: rgb(254, 255, 241);\n"
                                       "    \n"
                                       "    \n"
                                       "    border: 2px solid black;\n"
                                       "    border-radius: 5px;\n"
                                       "}\n"
                                       "QPushButton#loginButton:hover {\n"
                                       "    background-color: rgb(255, 220, 185);\n"
                                       "    border: 2px solid black;\n"
                                       "    border-radius: 5px;\n"
                                       "}\n"
                                       "")
        self.exit_button = QPushButton(self.widget)
        self.exit_button.setObjectName(u"exit_button")
        self.exit_button.setGeometry(QRect(694, 60, 21, 24))
        self.exit_button.setStyleSheet(u"QPushButton#exit_button {\n"
                                       "    border: 1px solid transparent;\n"
                                       "    background-color: rgb(255, 204, 153);\n"
                                       "}\n"
                                       "QPushButton#exit_button:hover {\n"
                                       "    background-color: rgba(255, 0, 0, 0.3);\n"
                                       "}")

        self.response_text = QLabel(self.widget)
        self.response_text.setObjectName(u"response_text")
        self.response_text.setGeometry(QRect(460, 370, 251, 20))
        font4 = QFont()
        font4.setFamilies([u"Alef"])
        font4.setPointSize(11)
        self.response_text.setFont(font4)
        self.response_text.setStyleSheet(u"color: rgb(255, 0, 0);")
        self.response_text.setAlignment(Qt.AlignCenter)

        self.exit_button.clicked.connect(lambda: Form.close())
        self.right_back.raise_()
        self.left_back.raise_()
        self.enter_your_label.raise_()
        self.input_key.raise_()
        self.loginButton.raise_()
        self.loginButton.clicked.connect(self.check_status)
        self.exit_button.raise_()
        self.response_text.raise_()

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)

    # setupUi
    def check_status(self):
        print(self.input_key.text())
        self.main_window()

    def get_id(self):
        current_machine_id = str(subprocess.check_output('wmic csproduct get uuid'), 'utf-8').split('\n')[1].strip()
        return current_machine_id

    def main_window(self):
        headers = {
            'Content-type': 'application/json'
        }

        data = {
            "key": self.input_key.text(),
            "device": self.get_id(),
            "id": "604a002dd52644b19441776d",
        }

        r = requests.post('https://api.cactusweb.io/api/v1/devices', headers=headers, json=data)
        if r.status_code == 400:
            self.response_text.setStyleSheet(u"color: rgb(255, 0, 0);")
            self.response_text.setText(QCoreApplication.translate("Form", u"Access Denied", None))
        elif r.status_code == 202:
            self.response_text.setStyleSheet(u"color: rgb(0, 170, 0);")
            self.response_text.setText(QCoreApplication.translate("Form", u"Logged succsefully", None))
            os.system('cls')
        elif r.status_code == 200:
            self.response_text.setStyleSheet(u"color: rgb(0, 170, 0);")
            self.response_text.setText(QCoreApplication.translate("Form", u"Logged succsefully", None))
            # Create the Qt Application
            self.main_form = QMainWindow()
            # Create and show the form
            license_key_not_found = False
            try:
                with open('upls\key.txt', 'r') as lickey:
                    lickey1 = lickey.readlines()
                    license_key = lickey1[0]
                if license_key != self.input_key.text():
                    write_key = open('upls\key.txt', 'w')
                    write_key.write(self.input_key.text())
                    write_key.close()
            except:
                license_key_not_found = True
                write_key = open('upls\key.txt', 'w')
                write_key.write(self.input_key.text())
                write_key.close()
            self.mainWindow = Ui_MainWindow()
            self.mainWindow.setupUi(self.main_form)
            self.main_form.show()
            Form.close()
        os.system('cls')

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Centurion AIO", None))
        self.left_back.setText("")
        self.right_back.setText("")
        self.enter_your_label.setText(QCoreApplication.translate("Form", u"Enter your license key:", None))
        self.response_text.setText(QCoreApplication.translate("Form", u"", None))
        self.input_key.setPlaceholderText(QCoreApplication.translate("Form", u"license key", None))
        try:
            with open('upls\key.txt', 'r') as key:
                key1 = key.readlines()[0]
        except:
            pass
        else:
            self.input_key.setText(QCoreApplication.translate("Form", str(key1), None))
        self.loginButton.setText(QCoreApplication.translate("Form", u"Log In", None))
        self.exit_button.setText(QCoreApplication.translate("Form", u"X", None))
    # retranslateUi

Ответы (1 шт):

Автор решения: S. Nick

Я не могу проверить ваш пример для PySide6, т.к. у меня PyQt5.

Всегда запускайте приложение в CMD/консоли/терминале и вы получите реальную ошибку!!!

Я не проверял логику вашего приложения. В PyQt5 ваше приложение, с безрамочным окном, отображается и перемещается без ошибок.

Скопируйте мой пример. Поменяйте импорты для PySide6, установите свое изображение для left_back
и запускайте приложение в CMD/консоли/терминале, , так как многие IDE не обрабатывают исключения Qt !!!

Если вы получите реальную ошибку - опубликуйте ее.

P.S. Не изменяйте код, сгенерированный Qt Designer. Создайте другой класс, который наследуется от соответствующего виджета и используйте созданный класс для его заполнения.

import os
import subprocess
import requests

# Пропишите ваши импорты для PySide6                         !!! PySide6
# и запускайте приложение в CMD/консоли/терминале            !!! PySide6

from PyQt5 import QtCore, QtGui, QtWidgets                #  !!! PyQt5
from PyQt5.Qt import *                                    #  !!! PyQt5


class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        centerPoint = QScreen.availableGeometry(QApplication.primaryScreen()).center()
        self.width = 862
        self.height = 660
        Form.resize(self.width, self.height)
        Form.move(centerPoint.x() - (self.width/2), centerPoint.y() - (self.height/2))
#        Form.setWindowFlags(Qt.FramelessWindowHint)
#        Form.setAttribute(Qt.WA_TranslucentBackground)
        self.widget = QWidget(Form)
        self.widget.setObjectName(u"widget")
        self.widget.setGeometry(QRect(70, 50, 771, 591))
# !!! +++          
        self.widget.setStyleSheet("background-color: #BFD8B8; border-radius: 14px;") # 
        
        self.left_back = QLabel(self.widget)
        self.left_back.setObjectName(u"left_back")
        self.left_back.setGeometry(QRect(90, 40, 351, 461))
        self.left_back.setStyleSheet(u"border: 1px solid white;")
#        self.left_back.setPixmap(QPixmap(u"img/background-login.jpg"))
        self.left_back.setPixmap(QPixmap(u"lena.jpg"))
        
        self.left_back.setScaledContents(True)
        self.right_back = QLabel(self.widget)
        self.right_back.setObjectName(u"right_back")
        self.right_back.setGeometry(QRect(410, 50, 321, 441))
        font = QFont()
        font.setPointSize(20)
        self.right_back.setFont(font)
        self.right_back.setStyleSheet(u"background-color: rgb(255, 204, 153);\n"
                                      "border: 2px solid transparent;\n"
                                      "border-radius: 14px;\n"
                                      "")
        self.enter_your_label = QLabel(self.widget)
        self.enter_your_label.setObjectName(u"enter_your_label")
        self.enter_your_label.setGeometry(QRect(460, 160, 251, 81))

        font1 = QFont()
        font1.setFamilies([u"Alef"])
        font1.setPointSize(17)
        font1.setItalic(False)
        self.enter_your_label.setFont(font1)
        self.enter_your_label.setAlignment(Qt.AlignCenter)
        self.input_key = QLineEdit(self.widget)
        self.input_key.setObjectName(u"input_key")
        self.input_key.setGeometry(QRect(460, 240, 251, 21))
        font2 = QFont()
        font2.setFamilies([u"Alef"])
        font2.setPointSize(11)
        self.input_key.setFont(font2)
        self.input_key.setMouseTracking(True)
        self.input_key.setFocusPolicy(Qt.ClickFocus)
        self.input_key.setStyleSheet(u"background-color: transparent;\n"
                                     "border: 2px solid black;\n"
                                     "border-radius: 3px;\n"
                                     "color: rgb(59, 59, 59);")
        self.input_key.setFrame(False)
        self.input_key.setAlignment(Qt.AlignCenter)
        self.loginButton = QPushButton(self.widget)
        self.loginButton.setObjectName(u"loginButton")
        self.loginButton.setGeometry(QRect(460, 300, 251, 61))
        font3 = QFont()
        font3.setPointSize(15)
        self.loginButton.setFont(font3)
        self.loginButton.setAutoFillBackground(False)
        self.loginButton.setStyleSheet(u"QPushButton#loginButton {\n"
                                       "    background-color: rgb(254, 255, 241);\n"
                                       "    \n"
                                       "    \n"
                                       "    border: 2px solid black;\n"
                                       "    border-radius: 5px;\n"
                                       "}\n"
                                       "QPushButton#loginButton:hover {\n"
                                       "    background-color: rgb(255, 220, 185);\n"
                                       "    border: 2px solid black;\n"
                                       "    border-radius: 5px;\n"
                                       "}\n"
                                       "")
        self.exit_button = QPushButton(self.widget)
        self.exit_button.setObjectName(u"exit_button")
        self.exit_button.setGeometry(QRect(694, 60, 21, 24))
        self.exit_button.setStyleSheet(u"QPushButton#exit_button {\n"
                                       "    border: 1px solid transparent;\n"
                                       "    background-color: rgb(255, 204, 153);\n"
                                       "}\n"
                                       "QPushButton#exit_button:hover {\n"
                                       "    background-color: rgba(255, 0, 0, 0.3);\n"
                                       "}")

        self.response_text = QLabel(self.widget)
        self.response_text.setObjectName(u"response_text")
        self.response_text.setGeometry(QRect(460, 370, 251, 20))
        font4 = QFont()
        font4.setFamilies([u"Alef"])
        font4.setPointSize(11)
        self.response_text.setFont(font4)
        self.response_text.setStyleSheet(u"color: rgb(255, 0, 0);")
        self.response_text.setAlignment(Qt.AlignCenter)

        self.exit_button.clicked.connect(lambda: Form.close())
        self.right_back.raise_()
        self.left_back.raise_()
        self.enter_your_label.raise_()
        self.input_key.raise_()
        self.loginButton.raise_()
        self.loginButton.clicked.connect(self.check_status)
        self.exit_button.raise_()
        self.response_text.raise_()

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)

    # setupUi
    def check_status(self):
        self.main_window()

    def get_id(self):
        current_machine_id = str(
            subprocess.check_output('wmic csproduct get uuid'), 
            'utf-8'
        ).split('\n')[1].strip()
        return current_machine_id

    def main_window(self):
        headers = {
            'Content-type': 'application/json'
        }

        data = {
            "key": self.input_key.text(),
            "device": self.get_id(),
            "id": "604a002dd52644b19441776d",
        }

        r = requests.post(
            'https://api.cactusweb.io/api/v1/devices', 
            headers=headers, 
            json=data
        )
# !!! +++
        print(f'r.status_code = {r.status_code}') #
        
        if r.status_code == 400:
            self.response_text.setStyleSheet(u"color: rgb(255, 0, 0);")
            self.response_text.setText(QCoreApplication.translate("Form", u"Access Denied", None))
        elif r.status_code == 202:
            self.response_text.setStyleSheet(u"color: rgb(0, 170, 0);")
            self.response_text.setText(QCoreApplication.translate("Form", u"Logged succsefully", None))
# ???            os.system('cls')
        elif r.status_code == 200:
            self.response_text.setStyleSheet(u"color: rgb(0, 170, 0);")
            self.response_text.setText(QCoreApplication.translate("Form", u"Logged succsefully", None))
            # Create the Qt Application
            self.main_form = QMainWindow()
            # Create and show the form
            license_key_not_found = False
            try:
                with open('upls\key.txt', 'r') as lickey:
                    lickey1 = lickey.readlines()
                    license_key = lickey1[0]
                if license_key != self.input_key.text():
                    write_key = open('upls\key.txt', 'w')
                    write_key.write(self.input_key.text())
                    write_key.close()
            except:
                license_key_not_found = True
                write_key = open('upls\key.txt', 'w')
                write_key.write(self.input_key.text())
                write_key.close()
            self.mainWindow = Ui_MainWindow()
            self.mainWindow.setupUi(self.main_form)
            self.main_form.show()
            Form.close()
# !!! раскомментируйте строку ниже
#        os.system('cls')

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Centurion AIO", None))
        self.left_back.setText("")
        self.right_back.setText("")
        self.enter_your_label.setText(QCoreApplication.translate("Form", u"Enter your license key:", None))
        self.response_text.setText(QCoreApplication.translate("Form", u"", None))
        self.input_key.setPlaceholderText(QCoreApplication.translate("Form", u"license key", None))
        try:
            with open('upls/key.txt', 'r') as key:
                key1 = key.readlines()[0]
        except:
            pass
        else:
            self.input_key.setText(QCoreApplication.translate("Form", str(key1), None))
        self.loginButton.setText(QCoreApplication.translate("Form", u"Log In", None))
        self.exit_button.setText(QCoreApplication.translate("Form", u"X", None))
    

class MoveWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

        self.old_pos = None

    # вызывается при нажатии кнопки мыши
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.old_pos = event.pos()

    # вызывается при отпускании кнопки мыши
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.old_pos = None

    # вызывается всякий раз, когда мышь перемещается
    def mouseMoveEvent(self, event):
        if not self.old_pos:
            return
        delta = event.pos() - self.old_pos
        self.move(self.pos() + delta)
        

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    w = MoveWindow()
    w.show()
    sys.exit(app.exec())
    

введите сюда описание изображения

→ Ссылка