1

В общем, извиняюсь. Код большой, а комментариев мало, уже сам немного путаюсь, но основное, что скорее всего является проблемой я пометил.

Суть проблемы: при нажатии на кнопку "Settings" открывается дочернее окно с настройками (будет в будущем), а при его закрывании, закрывается и основное.
Такого не было до того, как я не убрал рамку и не установил свою кнопку выхода.

main.py:

from PyQt5 import QtCore, QtGui, QtWidgets
from settings import Ui_Settings


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(330, 170)
        Form.setStyleSheet("QWidget {\n"
"    background-color: white;\n"
"    border: 2px solid gray;\n"
"}")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(20, 80, 201, 20))
        self.lineEdit.setStyleSheet("QLineEdit:hover {\n"
"    background-color: silver;\n"
"}")
        self.lineEdit.setMaxLength(8)
        self.lineEdit.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setGeometry(QtCore.QRect(20, 50, 201, 21))
        self.lineEdit_2.setStyleSheet("QLineEdit:hover {\n"
"    background-color: silver;\n"
"}")
        self.lineEdit_2.setMaxLength(16)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(240, 50, 75, 23))
        self.pushButton.setToolTip("")
        self.pushButton.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: gray;\n"
"    background-color: silver;\n"
"}")
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.onClicked)

        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(160, 130, 75, 23))
        self.pushButton_2.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: gray;\n"
"    background-color: silver;\n"
"}")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(240, 130, 75, 23))
        self.pushButton_3.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: gray;\n"
"    background-color: silver;\n"
"}")
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(Form)
        self.pushButton_4.setGeometry(QtCore.QRect(300, 0, 31, 31))
        self.pushButton_4.setStyleSheet("QPushButton {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: silver;\n"
"}")
        self.pushButton_4.setObjectName("pushButton_4")

        self.pushButton_4.clicked.connect(QtCore.QCoreApplication.instance().quit)

        self.pushButton_5 = QtWidgets.QPushButton(Form)
        self.pushButton_5.setGeometry(QtCore.QRect(270, 0, 31, 31))
        self.pushButton_5.setToolTip("")
        self.pushButton_5.setStyleSheet("QPushButton {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: silver;\n"
"}")
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_5.clicked.connect(self.min)

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(0, 0, 271, 31))
        self.label.setStyleSheet("QLabel {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"    font-size: 14px;\n"
"    font-weight: bold;    \n"
"    font-family: Lucida Console;\n"
"}")
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.lineEdit.setPlaceholderText(_translate("Form", "Password"))
        self.lineEdit_2.setPlaceholderText(_translate("Form", "Login"))
        self.pushButton.setText(_translate("Form", "Settings"))
        self.pushButton_2.setToolTip(_translate("Form", "New user registration"))
        self.pushButton_2.setText(_translate("Form", "Register"))
        self.pushButton_3.setText(_translate("Form", "Login"))
        self.pushButton_4.setToolTip(_translate("Form", "Close the program"))
        self.pushButton_4.setText(_translate("Form", "X"))
        self.pushButton_5.setText(_translate("Form", "-"))
        self.label.setText(_translate("Form", "  Login"))

    def min(self):
        self.showMinimized()

    def onClicked(self):
        self.sett = MySettings()
        self.sett.show()

class Widget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.setupUi(self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)# Убрать рамку
        self.old_pos = None

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

    # вызывается при отпускании кнопки мыши
    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.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)

class MySettings(QtWidgets.QWidget, Ui_Settings):
    def __init__(self, parent=None):
        super(MySettings, self).__init__(parent)
        self.setupUi(self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)  # Убрать рамку
        self.old_pos = None

if __name__ == "__main__":
   import sys
   app = QtWidgets.QApplication(sys.argv)
   w = Widget()
   w.show()
   sys.exit(app.exec_())

settings.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Settings(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(220, 120)
        Form.setStyleSheet("QWidget {\n"
"    background-color: white;\n"
"    border: 2px solid gray;\n"
"}")
        self.pushButton_4 = QtWidgets.QPushButton(Form)
        self.pushButton_4.setGeometry(QtCore.QRect(190, 0, 31, 31))
        self.pushButton_4.setStyleSheet("QPushButton {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: silver;\n"
"}")
        self.pushButton_4.setObjectName("pushButton_4")

        self.pushButton_4.clicked.connect(QtCore.QCoreApplication.instance().quit) # ВОТ ЭТА ВОТ ПАДЛА УБИВАЕТ ОСНОВУ

        self.pushButton_5 = QtWidgets.QPushButton(Form)
        self.pushButton_5.setGeometry(QtCore.QRect(160, 0, 31, 31))
        self.pushButton_5.setToolTip("")
        self.pushButton_5.setStyleSheet("QPushButton {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: silver;\n"
"}")
        self.pushButton_5.setObjectName("pushButton_5")

        self.pushButton_5.clicked.connect(self.min)

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(0, 0, 161, 31))
        self.label.setStyleSheet("QLabel {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"    font-size: 14px;\n"
"    font-weight: bold;    \n"
"    font-family: Lucida Console;\n"
"}")
        self.label.setObjectName("label")
        self.checkBox = QtWidgets.QCheckBox(Form)
        self.checkBox.setGeometry(QtCore.QRect(10, 40, 101, 17))
        self.checkBox.setToolTip("")
        self.checkBox.setStyleSheet("QCheckBox {\n"
"    border: none;\n"
"}")
        self.checkBox.setChecked(False)
        self.checkBox.setObjectName("checkBox")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(10, 60, 75, 23))
        self.pushButton_2.setToolTip("")
        self.pushButton_2.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: gray;\n"
"    background-color: silver;\n"
"}")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(10, 90, 121, 23))
        self.pushButton_3.setToolTip("")
        self.pushButton_3.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: red;\n"
"    background-color: red;\n"
"}")
        self.pushButton_3.setObjectName("pushButton_3")
        self.checkBox_2 = QtWidgets.QCheckBox(Form)
        self.checkBox_2.setGeometry(QtCore.QRect(120, 40, 91, 17))
        self.checkBox_2.setStyleSheet("QCheckBox {\n"
"    border: none;\n"
"}")
        self.checkBox_2.setChecked(True)
        self.checkBox_2.setObjectName("checkBox_2")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton_4.setToolTip(_translate("Form", "Close the program"))
        self.pushButton_4.setText(_translate("Form", "X"))
        self.pushButton_5.setText(_translate("Form", "-"))
        self.label.setText(_translate("Form", "  Settings"))
        self.checkBox.setText(_translate("Form", "Show password"))
        self.pushButton_2.setText(_translate("Form", "Save"))
        self.pushButton_3.setText(_translate("Form", "Delete all accounts"))
        self.checkBox_2.setText(_translate("Form", "Show login"))

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

    # вызывается при отпускании кнопки мыши
    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.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)

    def min(self):
        self.showMinimized()

Собственно, как сделать, чтобы закрывалось только окно с настройками?

1 ответ 1

1

Замените строку

self.pushButton_4.clicked.connect(QtCore.QCoreApplication.instance().quit)

на

self.pushButton_4.clicked.connect(self.hide)

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Settings(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(220, 120)
        Form.setStyleSheet("QWidget {\n"
"    background-color: white;\n"
"    border: 2px solid gray;\n"
"}")
        self.pushButton_4 = QtWidgets.QPushButton(Form)
        self.pushButton_4.setGeometry(QtCore.QRect(190, 0, 31, 31))
        self.pushButton_4.setStyleSheet("QPushButton {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: silver;\n"
"}")
        self.pushButton_4.setObjectName("pushButton_4")

#        self.pushButton_4.clicked.connect(QtCore.QCoreApplication.instance().quit) # ВОТ ЭТА ВОТ ПАДЛА УБИВАЕТ ОСНОВУ
        self.pushButton_4.clicked.connect(self.hide)

        self.pushButton_5 = QtWidgets.QPushButton(Form)
        self.pushButton_5.setGeometry(QtCore.QRect(160, 0, 31, 31))
        self.pushButton_5.setToolTip("")
        self.pushButton_5.setStyleSheet("QPushButton {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"}\n"
"QPushButton:hover {\n"
"    background-color: silver;\n"
"}")
        self.pushButton_5.setObjectName("pushButton_5")

        self.pushButton_5.clicked.connect(self.min)

        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(0, 0, 161, 31))
        self.label.setStyleSheet("QLabel {\n"
"    background-color: gray;\n"
"    border:none;\n"
"    color:white;\n"
"    font-size: 14px;\n"
"    font-weight: bold;    \n"
"    font-family: Lucida Console;\n"
"}")
        self.label.setObjectName("label")
        self.checkBox = QtWidgets.QCheckBox(Form)
        self.checkBox.setGeometry(QtCore.QRect(10, 40, 101, 17))
        self.checkBox.setToolTip("")
        self.checkBox.setStyleSheet("QCheckBox {\n"
"    border: none;\n"
"}")
        self.checkBox.setChecked(False)
        self.checkBox.setObjectName("checkBox")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(10, 60, 75, 23))
        self.pushButton_2.setToolTip("")
        self.pushButton_2.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: gray;\n"
"    background-color: silver;\n"
"}")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(Form)
        self.pushButton_3.setGeometry(QtCore.QRect(10, 90, 121, 23))
        self.pushButton_3.setToolTip("")
        self.pushButton_3.setStyleSheet("QPushButton {\n"
"    border: none;\n"
"    color: white;\n"
"    background-color: gray;\n"
"    font-weight: bold;\n"
"    border-bottom: 2px solid black;\n"
"    border-right: 2px solid black;\n"
"}\n"
"QPushButton:hover {\n"
"    border-color: red;\n"
"    background-color: red;\n"
"}")
        self.pushButton_3.setObjectName("pushButton_3")
        self.checkBox_2 = QtWidgets.QCheckBox(Form)
        self.checkBox_2.setGeometry(QtCore.QRect(120, 40, 91, 17))
        self.checkBox_2.setStyleSheet("QCheckBox {\n"
"    border: none;\n"
"}")
        self.checkBox_2.setChecked(True)
        self.checkBox_2.setObjectName("checkBox_2")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton_4.setToolTip(_translate("Form", "Close the program"))
        self.pushButton_4.setText(_translate("Form", "X"))
        self.pushButton_5.setText(_translate("Form", "-"))
        self.label.setText(_translate("Form", "  Settings"))
        self.checkBox.setText(_translate("Form", "Show password"))
        self.pushButton_2.setText(_translate("Form", "Save"))
        self.pushButton_3.setText(_translate("Form", "Delete all accounts"))
        self.checkBox_2.setText(_translate("Form", "Show login"))

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

    # вызывается при отпускании кнопки мыши
    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.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)

    def min(self):
        self.showMinimized()

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

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.