Pyqt5 QLabel,顶部为图像,底部为文本

时间:2019-12-01 17:15:19

标签: python pyqt5

我正在尝试使用图像和文本创建PyQt5-QLabel。我想在图像底部添加一个文本。以下是部分代码

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_())

1 个答案:

答案 0 :(得分:0)

您必须在QVBoxLayout中使用2个QLabel:

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_())