Why is there spacing between my location label and temperature label

52 views Asked by At

enter image description hereI am creating a widget that shows you weather conditions. Inside the QFrame, there is a temperature label and a location label. However, there is a lot of space between them, which I want to reduce.

This is the code you need:

weather_widget:

from PyQt6.QtWidgets import QVBoxLayout, QSpacerItem, QSizePolicy, QWidget, QMainWindow, QFrame, QApplication, QGridLayout, QLabel
from PyQt6.QtGui import QFont
import sys
from PyQt6.QtCore import Qt


class WeatherWidget(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)

        # Set the widget properties
        self.setFixedSize(250, 150)
        
        # Create a QVBoxLayout for the widget
        self.layout = QGridLayout(self)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(self)

        
        
        #create temperature and location label
        self.temp_label = QLabel(self)
        self.temp_label.setStyleSheet("margin: 0; padding: 0;")

        font = QFont('Varela Round', 20, 40)
        self.temp_label.setFont(font)
        self.temp_label.setText(‘25C’)
        print('Loaded temperature:', ,25C’)

        self.location_label = QLabel(self)
        self.location_label.setFont(font)
        self.location_label.setText(’City’)
        self.location_label.setStyleSheet("margin: 0; padding: 0;")

        self.layout.addWidget(self.location_label, 0, 0, alignment=Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
        self.layout.addWidget(self.temp_label, 1, 0, alignment=Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)

        
        self.setObjectName("WeatherWidgetFrame")
        self.setStyleSheet("""
            #WeatherWidgetFrame {
                border: 2px solid black;
                border-radius: 10px;
                padding: 10px;
                background-color: #87CEEB;
            }
        """)



if __name__ == '__main__':
    app = QApplication(sys.argv)

    main_window = QMainWindow()  # Main window for the demo application
    widget = WeatherWidget()
    main_widget = QWidget()

    main_window.setCentralWidget(main_widget)  # Set the WeatherWidget as the central widget of the window
    main_window.resize(450, 450)
    main_layout = QVBoxLayout(main_widget)
    main_layout.addWidget(widget, alignment=Qt.AlignmentFlag.AlignCenter)
    main_window.show()
    main_window.raise_()
    sys.exit(app.exec())

I tried setting the content margins to 0: self.layout.setContentsMargins(0, 0, 0, 0) expecting there to be no space between the widgets but that didn't work, so I set the spacing to 0 as well: self.layout.setSpacing(0) but that didnt change things ethier. Then I tried to set the margins and padding of the two labels:

self.temp_label = QLabel(self)
        self.temp_label.setStyleSheet("margin: 0;     padding: 0;")

    font = QFont('Varela Round', 20, 40)
    self.temp_label.setFont(font)
    self.temp_label.setText(str(ctemp))
    print('Loaded temperature:', ctemp)

    self.location_label = QLabel(self)
    self.location_label.setFont(font)
    self.location_label.setText(location)
        self.location_label.setStyleSheet("margin: 0;     padding: 0;")

that didnt work ethier.`

0

There are 0 answers