Dr. Zezo
Dr. Zezo

Reputation: 415

Pyqt5 QLabel with image at top and text at bottom

I am trying to create a PyQt5 - QLabel with both image and text. I would like to have a text at the bottom of the image. Below is a part of the code

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        l4=QLabel()
        l4.setText('delete')  
        l4.setAlignment(Qt.AlignBottom)
        pixmap = QPixmap("/home/moh/Documents/My_GUI/Icons/Delete.png")
        l4.setPixmap(pixmap) 
        l4.setAlignment(Qt.AlignTop)         

        self.layout = QGridLayout()
        self.layout.addWidget(l4, 0, 0)


        self.setLayout(self.layout)
        self.show()

app = QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())

Upvotes: 2

Views: 1392

Answers (2)

OualidSai
OualidSai

Reputation: 117

If you want to use a circle Qlabel image, use this code:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainterPath, QPainter
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget

class Label(QLabel):
    def __init__(self, *args, antialiasing=True, **kwargs):

        super(Label, self).__init__(*args, **kwargs)

        self.Antialiasing = antialiasing
        self.setMaximumSize(90, 90)
        self.setMinimumSize(90, 90)
        self.radius = 45 

        self.target = QPixmap(self.size())  
        self.target.fill(Qt.transparent)   

        p = QPixmap("Image.jpg").scaled(  
        90, 90, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)

        painter = QPainter(self.target)
        if self.Antialiasing:
            painter.setRenderHint(QPainter.Antialiasing, True)
            painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
            painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

        path = QPainterPath()
        path.addRoundedRect(
        0, 0, self.width(), self.height(), self.radius, self.radius)

        painter.setClipPath(path)
        painter.drawPixmap(0, 0, p)
        self.setPixmap(self.target)


class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        pixmap_label = Label()
        text_label = QLabel(text="delete")
        lay = QVBoxLayout(self)
        lay.addWidget(pixmap_label, alignment=Qt.AlignCenter)
        lay.addWidget(text_label, alignment=Qt.AlignCenter)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 244282

You have to use 2 QLabel in a QVBoxLayout:

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget


class Window(QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        pixmap_label = QLabel(
            pixmap=QPixmap("/home/moh/Documents/My_GUI/Icons/Delete.png")
        )
        text_label = QLabel(text="delete")

        lay = QVBoxLayout(self)
        lay.addWidget(pixmap_label, alignment=Qt.AlignCenter)
        lay.addWidget(text_label, alignment=Qt.AlignCenter)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

Upvotes: 5

Related Questions