Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update image on a label PyQT5

I am new in python. I want to update an image on label1. But function 'def browse' always give me blank label instead. Seems the code does not get the image or it failed to update the label, and the program did not give any error message. Did i missed something here? Also, print command below setPixmap command is executed. i am using python 2.7 and PyQT4.

Thanks in advance

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os, sys
import cv2
from PIL.ImageQt import ImageQt


class GuiCakep(QtGui.QWidget):

    global label1, label2

    def __init__(self):
        super(GuiCakep, self).__init__()
        self.initUI()


    def initUI(self):
        self.setGeometry(100, 100, 1000, 600)
        self.setWindowTitle('Single Browse')

        self.label1 = QtGui.QLabel(self)
        self.label1.setFrameShape(QtGui.QFrame.Box)
        self.label1.setGeometry(QtCore.QRect(20, 10, 451, 451))
        pixmap = QPixmap('1stimage.jpg')
        self.label1.setPixmap(pixmap)
        self.label2 =QtGui.QLabel(self)
        self.label2.setFrameShape(QtGui.QFrame.Box)
        self.label2.setGeometry(QtCore.QRect(481, 10, 451, 451))
        btn = QtGui.QPushButton('Browse', self)
        btn.resize(btn.sizeHint())
        btn.clicked.connect(self.browse)
        btn.move(775, 500)       
        self.show()

    def browse(self):
        filePath = QtGui.QFileDialog.getOpenFileName(self, 'a file','*.jpg')
        fileHandle = open(filePath, 'r')
        pixmap = QPixmap('filePath')
        self.label1.setPixmap(pixmap)
        print("Whoa awesome")



def main():
    app = QtGui.QApplication(sys.argv)
    w = GuiCakep()
    app.exec_()


if __name__ == '__main__':
    main()
like image 523
SimpleDish Avatar asked Oct 15 '25 03:10

SimpleDish


1 Answers

You are creating a Pixmap with 'filePath' as argument. This is a string, not your variable filePath.
Remove the both ' and this should update the label.

like image 133
Yohboy Avatar answered Oct 18 '25 02:10

Yohboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!