import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel class MyWindow(QMainWindow): def __init__(self): super().__init__() self.label = QLabel("Hello World!", self) self.label.adjustSize() self.setGeometry(100, 100, 300, 200) self.setWindowTitle("Example 1") if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPixmap class MyWindow(QMainWindow): def __init__(self): super().__init__() self.label = QLabel(self) pixmap = QPixmap("image.jpg") self.label.setPixmap(pixmap) self.label.adjustSize() self.setGeometry(100, 100, 300, 200) self.setWindowTitle("Example 2") if __name__ == '__main__': app = QApplication(sys.argv) window = MyWindow() window.show() sys.exit(app.exec_())In this example, a QLabel widget is created with an image file ("image.jpg"). The image is set as the label's pixmap and the adjustSize method is called to resize the label to fit the image. The window is then shown with the label. The PyQt5.QtWidgets package library is used in both examples to create and manipulate the QLabel widget. The adjustSize method is a built-in method of the QLabel class in PyQt5.QtWidgets.