I am new to python and currently doing some basic excerises. I'm trying to rewrite some application in which I used tkinter to do the same with PyQt5. Everything works apart from one problem - I have a QLabel containing image and I'm trying to align the image in the center of label but it doesn't want to, image stays aligned to the left. This was answered by @eyllanesc, who suggested that the QLabel is not centered with respect to the window and I should center the widget by changing to:
layout.addWidget(label_img, alignment=Qt.AlignCenter)
And that worked perfectly, however there are two more widgets (label_top, label_bottom) in the same layout, labels with text.And despite not beign aligned to center of the window, the text displayed centered. Why does label with text behave different to label with image?
Code below:
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QPushButton, QFileDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
app=QApplication([])
window=QWidget()
window.setFixedSize(500,500)
layout=QVBoxLayout()
label_top=QLabel('PLEASE WAIT')
label_top.setAlignment(Qt.AlignCenter)
label_top.setStyleSheet("font: 20pt Bahnschrift; background-color: #ffd167; color: black")
layout.addWidget(label_top)
label_img=QLabel()
label_img.setFixedSize(300, 300)
label_img.setAlignment(Qt.AlignCenter)
image = QFileDialog.getOpenFileName(None,'Select file','D:\_Download', "Image files(*.png *.jpg *.jpeg *.gif)")
imagePath = image[0]
pixmap = QPixmap(imagePath)
pixmap.scaledToHeight(label_img.height(), Qt.SmoothTransformation)
label_img.setPixmap(pixmap)
#label_img.resize(pixmap.width(),pixmap.height())
layout.addWidget(label_img, alignment=Qt.AlignCenter)
label_bottom=QLabel('PLEASE WAIT')
label_bottom.setAlignment(Qt.AlignCenter)
label_bottom.setStyleSheet("font: 20pt Bahnschrift; background-color: #ffd167; color: black")
layout.addWidget(label_bottom)
window.setLayout(layout)
window.show()
app.setStyle('Fusion')
app.exec_()