M
M
MajorTom692017-06-17 20:01:12
Python
MajorTom69, 2017-06-17 20:01:12

How to insert image in pyqt5?

you need to insert an image in the window. everywhere shows the same way

label = QLabel(self)
pixmap = QPixmap('image.jpeg')
label.setPixmap(pixmap)

what I actually did, but it even does not work
here is my code
import sys
from PyQt5 import QtWidgets, QtGui

class Window(QtWidgets.QWidget):
    def __init__(self, ):
        super().__init__()
        self.resize(800, 600)
        self.center()
        self.general()
    
    def general(self, ):
        
        # Menu Bar 
       
        self.lbl = QtWidgets.QLabel(self)
        self.pix = QtGui.QPixmap("img.png")
        print(self.pix.isNull())
        self.lbl.setPixmap(self.pix)
        self.lbl.resize(500, 500)
        self.lbl.move(50, 50)
        self.show()




    def center(self, ):
        frame_window = self.frameGeometry()
        center_coord = QtWidgets.QDesktopWidget().availableGeometry().center()
        frame_window.moveCenter(center_coord)
        self.move(frame_window.topLeft())


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

and here is the result:
c6f0d7edc7c741e39576e39a85af4229.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex F, 2017-06-19
@MajorTom69

According to code:

#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QApplication)
from PyQt5.QtGui import QPixmap
 
class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):      
        hbox = QHBoxLayout(self)
        pixmap = QPixmap("/home/fil/pictures/Torrents.png")
        lbl = QLabel(self)
        lbl.setPixmap(pixmap)
        hbox.addWidget(lbl)
        self.setLayout(hbox)
        
        self.move(100, 200)
        self.setWindowTitle('Red Rock')
        self.show()        
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

It works.
You need the full path to the file. I don't know why you can't process png , but here's my screenshot:
s_1497833842_9919210_30791ea709.png

A
Alexander, 2017-06-18
@sanya84

This is how it works tested

import sys
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QApplication)
from PyQt5.QtGui import QPixmap
 
class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        self.initUI()
        
    def initUI(self):      
        hbox = QHBoxLayout(self)
        pixmap = QPixmap("ёлка.png")
        lbl = QLabel(self)
        lbl.setPixmap(pixmap)
        hbox.addWidget(lbl)
        self.setLayout(hbox)
        
        self.move(100, 200)
        self.setWindowTitle('Red Rock')
        self.show()        
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question