-1

So i have created a window using QtDesigner and i added an icon to the QPushButton in the window (self.wlcm_registerbtn).

but after pyuic5 the file, the icon just don't want to show itself anymore knowing that the image file is in the same directory as the .ui and .py file. so this is the code part of the QpushButton generated from pyuic5 :

Feel free to mark this question as duplicate if you have already helped someone with the same issue. thank you in advance !

self.wlcm_registerbtn = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setWeight(50)
self.wlcm_registerbtn.setFont(font)
icon_registerbtn = QtGui.QIcon()
icon_registerbtn.addPixmap(QtGui.QPixmap("images/registericon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.wlcm_registerbtn.setIcon(icon_registerbtn)
self.wlcm_registerbtn.setObjectName("wlcm_registerbtn")
self.gridLayout.addWidget(self.wlcm_registerbtn, 1, 1, 1, 1)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Zack
  • 73
  • 10
  • when you point to "images / registericon.png" you are indicating that the image is inside the folder images, and as you point out it is on the side of the .py so you will not find it so there are 2 solutions: you change "images / registericon.png" to "registericon.png" or create a folder called images on the side of the .py and inside it places the image. – eyllanesc Oct 20 '18 at 22:39
  • i've already tried that and tweaked the code but no result. – Zack Oct 20 '18 at 22:58
  • could you show an image of the folder where the image is and the folder where the .py is? also shows how you have executed the .py – eyllanesc Oct 20 '18 at 23:01
  • py file path: pc\Desktop\qt_test\WelcomeWin.py || png path : pc \ Desktop \ qt_test \ images \ registericon.png – Zack Oct 20 '18 at 23:12
  • and how do you execute the .py? – eyllanesc Oct 20 '18 at 23:15
  • change `"images/registericon.png"` to `os.path.join(os.path.dirname(os.path.abspath(__file__)), "images/registericon.png")` and add `import os` on top file – eyllanesc Oct 20 '18 at 23:17
  • i execute the .py file from the Vscode terminal (it has the sys.exit(app.exec_)). i'll try your suggestion now – Zack Oct 20 '18 at 23:25
  • and once again it worked like a charm :D thank you very much – Zack Oct 20 '18 at 23:26
  • In what path do you open the terminal Vscode ?, try opening the CMD in the .py directory and run it with: python your_script.py – eyllanesc Oct 20 '18 at 23:27
  • Possible duplicate of [Python \_\_file\_\_ attribute absolute or relative?](https://stackoverflow.com/questions/7116889/python-file-attribute-absolute-or-relative) – eyllanesc Oct 20 '18 at 23:28
  • Vscode Terminal :||C:\Users\pc>python c:/Users/pc/Desktop/qt_test/WelcomeWin.py|| – Zack Oct 20 '18 at 23:31
  • the path to which the .png is relative is to where is the executable that launches the .py, in your case it is relative to the binary of Vscode Terminal so it could not find the image, to check it execute `print(os.getcwd())` – eyllanesc Oct 20 '18 at 23:34
  • i have just tried giving the full path and it also worked, i dont know why i haven't thought about it. thank you for your time – Zack Oct 20 '18 at 23:40

1 Answers1

0

Error fixed by stating the full path of .png's. Full Path:

C:/users/pc/desktop/*pydirectory/*pngdirectory/*pngfilename.png
Zack
  • 73
  • 10