如何从布局中删除小部件

ih99xse1  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(241)

**已关闭。**此问题需要调试详细信息。它目前不接受答案。
**想要改进此问题?**更新问题,使其位于堆栈溢出主题上。

昨天关门了。
改进这个问题
我有一个应用程序,有4个页面,在1个页面上有一个日历和一个天气小部件,它在点击按钮后被切断,因为它在布局中。我想从布局中删除天气小部件,但我不知道如何正确执行。
按下按钮前:

点击后:

应该如何:

以下是代码本身:(代码第143行有部分关于天气的代码)

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.Qt import *
from ext import *
from main import App                              # +++ 
from part_1 import Main

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self): 
        super().__init__()
        #размер основного окн
        self.resize(845, 535)
        self.setFixedSize(QtCore.QSize(845,535))

# 

        self.setStyleSheet("background-color: rgb(245, 45, 45);")        
        #центральный виджет
        self.centralwidget = QtWidgets.QWidget()
        self.centralwidget.setObjectName("centralwidget")
        self.setCentralWidget(self.centralwidget)

# Content horizontalLayout_2

        self.Content = QtWidgets.QFrame(self.centralwidget)
        self.Content.setStyleSheet("background-color: rgb(255, 255, 255);")

        self.Content.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.Content.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Content.setObjectName("Content")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.Content)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setSpacing(0)
        #виджет слева
        self.frame_left_menu = QtWidgets.QFrame(self.Content)
        self.frame_left_menu.setMinimumSize(QtCore.QSize(45, 0))

        self.frame_left_menu.setStyleSheet("background-color: rgb(255, 255, 255); border: 0px solid; ")

        self.frame_left_menu.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_left_menu.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_left_menu.setObjectName("frame_left_menu")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.frame_left_menu)
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)

        self.frame_top_menus = QtWidgets.QFrame(self.frame_left_menu)
        self.frame_top_menus.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.frame_top_menus.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_top_menus.setObjectName("frame_top_menus")
        #указываем вертикальное выравнивание
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.frame_top_menus)
        self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_4.setSpacing(10)

        #домик                                  
        self.btn_page_1 = QtWidgets.QPushButton("", self.frame_top_menus)
        self.btn_page_1.setIcon(QIcon('дом.png'))
        self.btn_page_1.setIconSize(QSize(45,45))
        self.btn_page_1.setMinimumSize(QtCore.QSize(0, 40))

        self.btn_page_1.setStyleSheet('border: 0px solid red;')

        self.btn_page_1.setObjectName("btn_page_1")
        self.verticalLayout_4.addWidget(self.btn_page_1)
        #Заметки
        self.btn_page_2 = QtWidgets.QPushButton("", self.frame_top_menus)
        self.btn_page_2.setIcon(QIcon('заметка1.png'))
        self.btn_page_2.setIconSize(QSize(45,45))
        self.btn_page_2.setMinimumSize(QtCore.QSize(0, 40))

        self.btn_page_2.setStyleSheet('border: 0px solid red;')
        self.btn_page_2.setObjectName("btn_page_2")

        self.verticalLayout_4.addWidget(self.btn_page_2)

        #погода
        self.btn_page_3 = QtWidgets.QPushButton("", self.frame_top_menus)
        self.btn_page_3.setIcon(QIcon('солнышко.png'))
        self.btn_page_3.setIconSize(QSize(45,45))
        self.btn_page_3.setMinimumSize(QtCore.QSize(0, 40))

        self.btn_page_4 = QtWidgets.QPushButton('', self.frame_top_menus)
        self.btn_page_4.setIcon(QIcon('папочка.png'))                          
        self.btn_page_4.setIconSize(QSize(45, 45))
        self.btn_page_4.setMinimumSize(QtCore.QSize(0, 40))

        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1000)

        font1 = QFont('Century Gothic', 13, QFont.Bold)
        self.timewidget = QtWidgets.QLabel()
        self.timewidget.setMinimumSize(QtCore.QSize(45,45))
        self.timewidget.setAlignment(Qt.AlignBottom)
        self.timewidget.setFont(font1)

        self.btn_page_3.setObjectName("btn_page_3")
        self.verticalLayout_4.addWidget(self.btn_page_3)

        self.verticalLayout_3.addWidget(self.frame_top_menus, 0, QtCore.Qt.AlignTop)
        self.horizontalLayout_2.addWidget(self.frame_left_menu)

        self.verticalLayout_4.addWidget(self.btn_page_4)                      

        self.frame_pages = QtWidgets.QFrame(self.Content)
        self.frame_pages.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_pages.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_pages.setObjectName("frame_pages")
        self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.frame_pages)
        self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_5.setObjectName("verticalLayout_5")

        self.stackedWidget = QtWidgets.QStackedWidget(self.frame_pages)
        self.stackedWidget.setMinimumSize(QtCore.QSize(800, 535))

        self.stackedWidget.setObjectName("stackedWidget")

# 1 страница

        self.page_1 = QtWidgets.QWidget()
        self.page_1.setObjectName("page_1")
        self.verticalLayout_7 = QtWidgets.QVBoxLayout(self.page_1)
        self.verticalLayout_7.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_7.setObjectName("verticalLayout_7")
        #указываем что будет находится на 1 странице
        self.label_1 = QtWidgets.QLabel(self.page_1)
        font = QtGui.QFont()
        font.setPointSize(40)
        font.setFamily("Century Gothic")
        font.setPointSize(15)
        font.setKerning(True)
        self.label_1.setFont(font)

        self.label_1.setAlignment(QtCore.Qt.AlignCenter)
        self.label_1.setObjectName("label_1")
        self.verticalLayout_7.addWidget(self.label_1)

        self.main = App()

        self.main.setMinimumSize(QtCore.QSize(500, 154))
        self.main.w_root.setWindowFlag(Qt.FramelessWindowHint)

        self.verticalLayout_7.addWidget(self.main.w_root, alignment = QtCore.Qt.AlignRight)
        self.main.w_root.setWindowFlag(Qt.CustomizeWindowHint)
        self.stackedWidget.addWidget(self.page_1)

        #2 страница
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.verticalLayout_6 = QtWidgets.QVBoxLayout(self.page_2)
        self.verticalLayout_6.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_6.setObjectName("verticalLayout_6")
        #указываем что будет находиться в 2 странице
        self.label_2 = QtWidgets.QLabel("Страница2", self.page_2)
        font = QtGui.QFont()
        font.setPointSize(40)
        font.setFamily("Century Gothic")
        font.setPointSize(15)
        font.setKerning(True)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: #FFF; background-color: #1eae98;")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.verticalLayout_6.addWidget(self.label_2)
        self.stackedWidget.addWidget(self.page_2)
        #3 страница
        self.page_3 = QtWidgets.QWidget()
        self.page_3.setObjectName("page_3")
        self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.page_3)
        self.verticalLayout_8.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_8.setObjectName("verticalLayout_8")
        #указываем что будет находиться на 3 странице
        self.label = QtWidgets.QLabel("Страница3", self.page_3)
        font = QtGui.QFont()

        font.setFamily("Century Gothic")
        font.setPointSize(15)
        font.setKerning(True)
        self.label.setFont(font)
        self.label.setStyleSheet("color: green; background-color: black;")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.verticalLayout_8.addWidget(self.label)
        self.stackedWidget.addWidget(self.page_3)

# page_4 verticalLayout_9

        self.page_4 = QtWidgets.QWidget()
        self.page_4.setObjectName('page_4')

        self.gg = Main()

        self.verticalLayout_9 = QtWidgets.QVBoxLayout(self.page_4)               

        self.verticalLayout_9.addWidget(self.gg)

        self.verticalLayout_9.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_9.setSpacing(0)
        self.verticalLayout_9.setObjectName('verticalLayout_9')

        self.stackedWidget.addWidget(self.page_4)

        self.verticalLayout_5.addWidget(self.stackedWidget)
        self.horizontalLayout_2.addWidget(self.frame_pages)
        self.stackedWidget.setCurrentIndex(0)

# verticalLayout  centralwidget

        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)    
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setSpacing(0)

# verticalLayout.addWidget(self.Content)

        self.verticalLayout.addWidget(self.Content)

        #Нажатие на дом
        self.btn_page_1.clicked.connect(
            lambda: self.stackedWidget.setCurrentWidget(self.page_1))
        #Нажатие на заметки
        self.btn_page_2.clicked.connect(
            lambda: self.stackedWidget.setCurrentWidget(self.page_2))
        #Нажатие на погоду
        self.btn_page_3.clicked.connect(
            lambda: self.stackedWidget.setCurrentWidget(self.page_3))

        self.btn_page_4.clicked.connect(
            lambda: self.stackedWidget.setCurrentWidget(self.page_4))

        self.frame_left_menu.installEventFilter(self)
        self.verticalLayout_3.addWidget(self.timewidget, 0, QtCore.Qt.AlignBottom)

# календарь

        self.calendarWidget = QtWidgets.QCalendarWidget(self.page_1)
        self.calendarWidget.setWindowFlag(Qt.FramelessWindowHint)
        self.calendarWidget.setEnabled(True)
        self.calendarWidget.setGeometry(QtCore.QRect(0, 154, 800, 381))
        self.calendarWidget.setFixedSize(QtCore.QSize(800, 381))
        self.calendarWidget.setWindowFlags(Qt.FramelessWindowHint)

        font = QtGui.QFont()
        font.setFamily("Century Gothic")
        font.setPointSize(15)
        font.setKerning(False)
        self.calendarWidget.setFont(font)
        self.calendarWidget.setAutoFillBackground(True)

        self.calendarWidget.setGridVisible(False)

        self.calendarWidget.setVerticalHeaderFormat(QtWidgets.QCalendarWidget.NoVerticalHeader)
        self.calendarWidget.setDateEditEnabled(False)
        self.calendarWidget.setDateEditAcceptDelay(0)
        self.calendarWidget.setObjectName("calendarWidget")

        self.calendarWidget.setStyleSheet(StyleSheet)

        self.verticalLayout_7.addWidget(self.calendarWidget)

    def showTime(self):
        current_time = QtCore.QTime.currentTime()
        label_time = current_time.toString('hh:mm')
        self.timewidget.setText(label_time)

StyleSheet = '''
/* Верхняя область навигации                            */

# qt_calendar_navigationbar {

    background-color: rgb(0, 188, 212);
    min-width: 800px;
    max-width: 800px;
    min-height: 35px;
    max-height: 35px;
}

/*  Кнопка последнего месяца и кнопка следующего месяца */

# qt_calendar_prevmonth, #qt_calendar_nextmonth {

    border: none;                     /* убрать границу */
    margin-top: 0px;
    color: white;
    min-width: 30px;
    max-width: 30px;
    min-height: 30px;
    max-height: 30px;
    border-radius: 10px;            /* выглядит как эллипс */
    font-weight: bold;              /* шрифт полужирный    */
    qproperty-icon: none;    
    background-color: transparent; /* Цвет фона прозрачный */
}

# qt_calendar_prevmonth {

    qproperty-text: "<";         /* Изменить текст кнопки  */
}

# qt_calendar_nextmonth {

    qproperty-text: ">";
}

# qt_calendar_prevmonth:hover, #qt_calendar_nextmonth:hover {

    background-color: rgba(225, 225, 225, 100);
}

# qt_calendar_prevmonth:pressed, #qt_calendar_nextmonth:pressed {

    background-color: rgba(235, 235, 235, 100);
}

/*  год, месяц                                                */

# qt_calendar_yearbutton, #qt_calendar_monthbutton {

    color: white;
    margin: -1px;
    min-width: -1px;
    border-radius: 15px;
    background-color: rgba(0, 188, 212, 100)
}

# qt_calendar_yearbutton:hover, #qt_calendar_monthbutton:hover {

    background-color: rgba(225, 225, 225, 100);
}

# qt_calendar_yearbutton:pressed, #qt_calendar_monthbutton:pressed {

    background-color: rgba(235, 235, 235, 100);
}

/* Поле ввода года                                                        */

# qt_calendar_yearedit {

    min-width: 85px;
    color: white;
    background: transparent;         /* Сделать фон окна ввода прозрачным */
}

# qt_calendar_yearedit::up-button {   /* Кнопка вверх                      */

    width: 25px;
    subcontrol-position: right;      
}

# qt_calendar_yearedit::down-button { /* Кнопка вниз     */

    width: 25px;
    subcontrol-position: left;       
}

/* меню выбора месяца                                          */
CalendarWidget_QToolButton_QMenu {
     background-color: white;
}

CalendarWidget_QToolButton_QMenu_item {
    padding: 10px;
}
CalendarWidget_QToolButton_QMenu_item_selected_enabled {
    background-color: rgb(230, 230, 230);
    selection-color: rgb(45, 86, 100);
    selection-background-color: rgb(205, 217, 226);
}
CalendarWidget_QToolButton_menu-indicator {
    subcontrol-position: right center;                
}

/* ниже календарной формы */

# qt_calendar_calendarview {

    outline: 0px;                                 /* Удалить выделенную пунктирную рамку */
    selection-background-color: rgb(0, 188, 212); 
}
'''

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setFont(QtGui.QFont("Century Gothic", 12,))
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题