gpt4 book ai didi

python - Pyqt5 QLabel,图像在顶部,文本在底部

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:51 25 4
gpt4 key购买 nike

我正在尝试创建一个包含图像和文本的 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_())

最佳答案

您必须在 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_())

关于python - Pyqt5 QLabel,图像在顶部,文本在底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59127982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com