-1

I am making a tool in maya using pyside. I was able to add an image to my UI using QPixmap and adding it to QLabel. I am trying to figure out how to get the image to change with a press of a button (by pointing into to a new image path) but i am having trouble figuring out how to get it to change.

self.pix = QtGui.QPixmap(image_path)
self.lbl = QtWidgets.QLabel()
self.lbl.setPixmap(self.pix)
pic_layout.addWidget(self.lbl)
ghost654
  • 161
  • 2
  • 18

2 Answers2

1

Add button:

self.button = QtWidgets.QPushButton(self)

Subscribe to clicked event:

self.button.clicked.connect(self.button_clicked)

Add click handler:

def button_clicked(self, *args):
    pixmap = QtGui.QPixmap(new_image_path)
    self.lbl.setPixmap(pixmap)

new_image_path here is path to new image

ingvar
  • 4,169
  • 4
  • 16
  • 29
1

you can use the load method of the QPixmap object

    self.pix = QtGui.QPixmap(image_path)
    self.lbl = QtWidgets.QLabel()
    self.lbl.setPixmap(self.pix)
    
    self.button = QPushButton('Change Image')
    self.button.clicked.connect(self.changeImageHandler)
    
    
    pic_layout.addWidget(self.lbl)

def changeImageHandler(self):
    self.pix.load(new_image_path)
    self.lbl.setPixmap(self.pix)
Lcool
  • 321
  • 2
  • 5