Как изменить размер виджета в Layout? Как работает sizepolicy?

Рейтинг: 0Ответов: 1Опубликовано: 21.07.2023

Заранее извиняюсь за глупый вопрос. Я пытался по разному изменить размер на нужный мне, менял sizepolicy, капался в layout и много другое, но так и не вышло изменить на нужный мне размер, не прибегая к maximumsize, но я хочу что бы моё приложение можно было растянуть и что бы знак вопроса тоже растягивался, Пожалуйста подскажите как мне изменить размер виджета, а именно QLabel со знаком вопроса, мне нужно уменьшить размер примерно 20x20 px, если есть ещё какие то ошибки расскажите, буду очень благодарен, заранее спасибо =)

import sys

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QDialog, QFrame, QHBoxLayout,
    QLabel, QLayout, QPushButton, QSizePolicy,
    QTextEdit, QVBoxLayout, QWidget)
from additional_Func.add_word import res_rc

class Ui_background_window(object):
    def setupUi(self, background_window):
        if not background_window.objectName():
            background_window.setObjectName(u"background_window")
        background_window.resize(362, 439)
        background_window.setStyleSheet(u"background-color: rgb(31, 31, 31)")
        self.verticalLayout_2 = QVBoxLayout(background_window)
        self.verticalLayout_2.setObjectName(u"verticalLayout_2")
        self.main_window_frame = QFrame(background_window)
        self.main_window_frame.setObjectName(u"main_window_frame")
        self.main_window_frame.setEnabled(True)
        sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.main_window_frame.sizePolicy().hasHeightForWidth())
        self.main_window_frame.setSizePolicy(sizePolicy)
        self.main_window_frame.setMaximumSize(QSize(16777215, 16777215))
        self.main_window_frame.setStyleSheet(u"background-color: rgb(50, 50, 50);\n"
"border: 2px;\n"
"border-radius: 6px;")
        self.verticalLayout = QVBoxLayout(self.main_window_frame)
        self.verticalLayout.setObjectName(u"verticalLayout")
        self.verticalLayout.setSizeConstraint(QLayout.SetDefaultConstraint)
        self.intruction_foreword_Label = QLabel(self.main_window_frame)
        self.intruction_foreword_Label.setObjectName(u"intruction_foreword_Label")
        self.intruction_foreword_Label.setEnabled(True)
        self.intruction_foreword_Label.setStyleSheet(u"color: black;\n"
"font-size: 16px;\n"
"color: white\n"
"")
        self.intruction_foreword_Label.setWordWrap(True)

        self.verticalLayout.addWidget(self.intruction_foreword_Label)

        self.instruction_frame = QFrame(self.main_window_frame)
        self.instruction_frame.setObjectName(u"instruction_frame")
        self.horizontalLayout = QHBoxLayout(self.instruction_frame)
        self.horizontalLayout.setObjectName(u"horizontalLayout")
        self.instruction_advice_Label = QLabel(self.instruction_frame)
        self.instruction_advice_Label.setObjectName(u"instruction_advice_Label")
        self.instruction_advice_Label.setEnabled(True)
        sizePolicy.setHeightForWidth(self.instruction_advice_Label.sizePolicy().hasHeightForWidth())
        self.instruction_advice_Label.setSizePolicy(sizePolicy)
        self.instruction_advice_Label.setStyleSheet(u"color: black;\n"
"font-size: 16px;\n"
"color: white;\n"
"padding-bottom: 9px\n"
"")
        self.instruction_advice_Label.setWordWrap(True)

        self.horizontalLayout.addWidget(self.instruction_advice_Label)

        self.instruction_question_Label = QLabel(self.instruction_frame)
        self.instruction_question_Label.setObjectName(u"instruction_question_Label")
        self.instruction_question_Label.setEnabled(True)
        sizePolicy.setHeightForWidth(self.instruction_question_Label.sizePolicy().hasHeightForWidth())
        self.instruction_question_Label.setSizePolicy(sizePolicy)
        self.instruction_question_Label.setMinimumSize(QSize(0, 0))
        self.instruction_question_Label.setMaximumSize(QSize(233424, 232323))
        self.instruction_question_Label.setSizeIncrement(QSize(0, 0))
        self.instruction_question_Label.setBaseSize(QSize(0, 0))
        self.instruction_question_Label.setStyleSheet(u"QToolTip {\n"
"    background-color: rgb(31, 31, 31);\n"
"}\n"
"QLabel {\n"
"background-color: white;\n"
"border: 2px;\n"
"border-radius: 10px;\n"
"}")
        self.instruction_question_Label.setPixmap(QPixmap(u":/icons/icons/help_FILL0_wght400_GRAD0_opsz48.svg"))
        self.instruction_question_Label.setScaledContents(True)
        self.instruction_question_Label.setWordWrap(False)

        self.horizontalLayout.addWidget(self.instruction_question_Label)


        self.verticalLayout.addWidget(self.instruction_frame)

        self.TextArea_TextEdit = QTextEdit(self.main_window_frame)
        self.TextArea_TextEdit.setObjectName(u"TextArea_TextEdit")
        self.TextArea_TextEdit.setStyleSheet(u"QTextEdit{\n"
"background-color: white;\n"
"font-size: 12px;\n"
"color: rgb(52, 52, 52);\n"
"border: 3px;\n"
"border-radius: 10px;\n"
"padding-left: 5px;\n"
"widht: 30px;\n"
"font-size: 15px\n"
"}\n"
"\n"
"QScrollBar:vertical{\n"
"background-color: none;\n"
"border: none;\n"
"border-radius: none;\n"
"margin: 15px 0 15px 0;\n"
"width: 10px;\n"
"}\n"
"\n"
"QScrollBar::handle:vertical{\n"
"background-color: rgb(140, 140, 140);\n"
"min-height: 30px\n"
"}\n"
"\n"
"QScrollBar::handle:vertical:hover{\n"
"background-color: rgb(150, 150, 150);\n"
"}\n"
"\n"
"QScrollBar::handle:vertical:pressed{\n"
"background-color: rgb(160, 160, 160);\n"
"}\n"
"\n"
"QScrollBar::sub-line:vertical{\n"
"background-color: rgb(100, 100, 100);\n"
"border:none;\n"
"border-top-left-radius: 4px;\n"
"border-top-right-radius: 4px;\n"
"height: 15px;\n"
"subcontrol-position: top;\n"
"subcontrol-origin: margin;\n"
"min-width: 10px\n"
"}\n"
"\n"
"QScrollBar::sub-line:vertical:pressed{\n"
"background-color: rgb(130, 130, 130);\n"
"}\n"
"\n"
"QScrollB"
                        "ar::add-line:vertical {\n"
"background-color: rgb(100, 100, 100);\n"
"border: none;\n"
"border-bottom-left-radius: 4px;\n"
"border-bottom-right-radius: 4px;\n"
"height: 15px;\n"
"subcontrol-position: bottom;\n"
"subcontrol-origin: margin;\n"
"min-width: 10px\n"
"}\n"
"\n"
"QScrollBar::add-line:vertical:pressed{\n"
"background-color: rgb(130, 130, 130);\n"
"}\n"
"\n"
"QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {\n"
"   background: rgb(49, 49, 49);\n"
"}")
        self.TextArea_TextEdit.setFrameShadow(QFrame.Plain)
        self.TextArea_TextEdit.setUndoRedoEnabled(True)

        self.verticalLayout.addWidget(self.TextArea_TextEdit)

        self.AddWord_PushButton = QPushButton(self.main_window_frame)
        self.AddWord_PushButton.setObjectName(u"AddWord_PushButton")
        self.AddWord_PushButton.setEnabled(True)
        sizePolicy1 = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
        sizePolicy1.setHorizontalStretch(0)
        sizePolicy1.setVerticalStretch(0)
        sizePolicy1.setHeightForWidth(self.AddWord_PushButton.sizePolicy().hasHeightForWidth())
        self.AddWord_PushButton.setSizePolicy(sizePolicy1)
        self.AddWord_PushButton.setSizeIncrement(QSize(0, 0))
        self.AddWord_PushButton.setStyleSheet(u"QPushButton {\n"
"background-color: rgba(255, 255, 255, 50);\n"
"height: 50px;\n"
"color:white;\n"
"border: 1px outset rgba(255, 255, 255, 50);\n"
"border-radius: 6px;\n"
"font-size: 24px;\n"
"}\n"
"QPushButton:hover {\n"
"background-color: rgba(255, 255, 255, 60);\n"
"border: 2px outset rgba(255, 255, 255, 60);\n"
"}\n"
"QPushButton:pressed {\n"
"background-color: rgba(255, 255, 255, 70);\n"
"border: 3px outset rgba(255, 255, 255, 70);\n"
"} \n"
"")

        self.verticalLayout.addWidget(self.AddWord_PushButton)


        self.verticalLayout_2.addWidget(self.main_window_frame)


        self.retranslateUi(background_window)

        QMetaObject.connectSlotsByName(background_window)
    # setupUi

    def retranslateUi(self, background_window):
        background_window.setWindowTitle(QCoreApplication.translate("background_window", u"Dialog", None))
        self.intruction_foreword_Label.setText(QCoreApplication.translate("background_window", u"<html><head/><body><p align=\"center\"><span style=\" font-size:12pt;\">\u0427\u0442\u043e \u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u043e\u0432\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u044d\u0442\u043e\u0439 \u0441\u0445\u0435\u043c\u0435:</span></p></body></html>", None))
        self.instruction_advice_Label.setText(QCoreApplication.translate("background_window", u"<html><head/><body><p align=\"center\"><span style=\" font-size:12pt;\">Word_1</span><span style=\" font-size:18pt; color:#ff0000;\">:</span><span style=\" font-size:12pt;\">Translate</span><span style=\" font-size:18pt; color:#ff0000;\">;</span><span style=\" font-size:12pt;\">Word_2</span><span style=\" font-size:18pt; color:#ff0000;\">:</span><span style=\" font-size:12pt;\">Translate</span></p></body></html>", None))
#if QT_CONFIG(tooltip)
        self.instruction_question_Label.setToolTip(QCoreApplication.translate("background_window", u"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"hr { height: 1px; border-width: 0; }\n"
"li.unchecked::marker { content: \"\\2610\"; }\n"
"li.checked::marker { content: \"\\2612\"; }\n"
"</style></head><body style=\" font-family:'Segoe UI'; font-size:9pt; font-weight:400; font-style:normal;\" bgcolor=\"#000000\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-style:italic; color:#ff0000;\">:</span><span style=\" font-style:italic; color:#ffffff;\"> - \u0420\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044c \u043c\u0435\u0436\u0434\u0443 \u0430\u043d\u0433\u043b.\u0441\u043b\u043e\u0432\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u043e\u043c</span></p>\n"
"<p style=\" margin"
                        "-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-style:italic; color:#ff0000;\">;</span><span style=\" font-style:italic; color:#ffffff;\"> - \u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0438 \u0434\u0440\u0443\u0433\u043e\u0435 \u0441\u043b\u043e\u0432\u043e</span></p></body></html>", None))
#endif // QT_CONFIG(tooltip)
#if QT_CONFIG(whatsthis)
        self.instruction_question_Label.setWhatsThis(QCoreApplication.translate("background_window", u"<html><head/><body><p><br/></p></body></html>", None))
#endif // QT_CONFIG(whatsthis)
        self.instruction_question_Label.setText("")
        self.TextArea_TextEdit.setHtml(QCoreApplication.translate("background_window", u"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"hr { height: 1px; border-width: 0; }\n"
"li.unchecked::marker { content: \"\\2610\"; }\n"
"li.checked::marker { content: \"\\2612\"; }\n"
"</style></head><body style=\" font-family:'Segoe UI'; font-size:15px; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:12px;\"><br /></p></body></html>", None))
        self.AddWord_PushButton.setText(QCoreApplication.translate("background_window", u"Add Word", None))
    # retranslateUi

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = QDialog()
    ui = Ui_background_window()
    ui.setupUi(win)
    win.show()
    sys.exit(app.exec())

Вот тут я указываю minimumsize и ставлю в sizepolicy: minimum и ничего не меняется, но потом ставлю в sizepolicy: maximum и размер меняется как в minimumsize. Как это работает?

Ответы

▲ 0

Проблема с картинкой была из того что её изначальный размер был 48x48px и поэтому меньше этого размера она не хотела ужиматься в designer, 2 проблема так и не решена, не понимаю как работает sizepolicy