python - how to set dynamic image path in pyqt5? -
here code
def setimage(self): self.image_label.setautofillbackground(true) filename = qtwidgets.qfiledialog.getopenfilename(self, 'insert image', r'c:\users\pristinesofts\demo\images','image (*.jpg *.png *.icon *.gif)') print(filename) self.image_label.setstylesheet("qlabel{border:2px solid #d6d6d6;\n" "background-image:url(" +filename+ ");\n" "border-radius:30px;}\n")
i trying set image on label dynamically,but code not working me.it shows path in filename correct,but shows blank , window gets closed.i used qpixmap not working. can me? in advanced!
with label do:
# make sure load library pyqt5.qtgui import qpixmap # , should work def setimage(self): filename = qtwidgets.qfiledialog.getopenfilename(self, 'insert image', r'c:\users\pristinesofts\demo\images','image (*.jpg *.png *.icon *.gif)') print("path " + filename[0] " , don't need " + filename[1]) pngfile = qpixmap(filename[0]) # create image qpixmap widget, using filename. self.image_label.setpixmap(pngfile) # , add this. note: if still have issues, make sure provide path correctly. also, might need self.image_label.clear() after print.
edit: filename seems tuple, need choose item want. filename[0] path string. filename[1] non-needed data image (*.jpg *.png *.icon *.gif).
at python, data in tuples, lists or dicts, , picking data done specifying index of data item[number of index]. note indexes @ python starts @ 0, not @ 1.
example: if want pick patata in list:
mytuple = ("something", "nothing", "patata", "test") i use mytuple[2], because index [0] something, index [1] nothing, patata [2] , test [3].
Comments
Post a Comment