컴퓨터/Python
python qlabel image aligncenter
풍경소리^^
2022. 6. 14. 16:03
qlabel.py--------------------
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QFileDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.title = '그림파일 열기'
# self.left = 50
# self.top = 50
# self.width = 320
# self.height = 360
self.setWindowTitle(self.title)
# self.setGeometry(self.left, self.top, self.width, self.height)
image = QFileDialog.getOpenFileName(None, 'OpenFile', '', "Image file(*.jpg *.png)")
imagePath = image[0]
pixmap = QPixmap(imagePath)
# label1 = QLabel('ok.png', self)
self.label1 = QLabel(self)
self.label1.setStyleSheet("color: #FF5733; border-style: solid; border-width: 2px; border-color: #FFC300; border-radius: 10px; ")
width = 300
height = 300
if pixmap.width() >= pixmap.height():
self.label1.setPixmap(pixmap.scaledToWidth(width))
self.label1.setAlignment(Qt.AlignVCenter)
else:
self.label1.setPixmap(pixmap.scaledToHeight(height))
self.label1.setAlignment(Qt.AlignCenter)
# self.label.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
self.label1.adjustSize()
layout = QVBoxLayout()
layout.addWidget(self.label1)
# layout.addWidget(label2)
self.setLayout(layout)
self.setWindowTitle('QLabel')
self.setGeometry(300, 300, width, height)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())