Qicon, Pushbutton; I put different images for the active selected and disabled states. But, when I run the program and click on the button, there is no change in the image
-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1090, 681)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(150, 290, 271, 121))
self.pushButton.setStyleSheet("")
self.pushButton.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("images/Auth0Off.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
icon.addPixmap(QtGui.QPixmap("images/Auth0Off.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
icon.addPixmap(QtGui.QPixmap("images/Auth0Off.png"), QtGui.QIcon.Disabled, QtGui.QIcon.Off)
icon.addPixmap(QtGui.QPixmap("images/Auth0Off.png"), QtGui.QIcon.Disabled, QtGui.QIcon.On)
icon.addPixmap(QtGui.QPixmap("images/Auth0On.png"), QtGui.QIcon.Active, QtGui.QIcon.Off)
icon.addPixmap(QtGui.QPixmap("images/Auth0On.png"), QtGui.QIcon.Active, QtGui.QIcon.On)
icon.addPixmap(QtGui.QPixmap("images/Auth0Hover.png"), QtGui.QIcon.Selected, QtGui.QIcon.Off)
icon.addPixmap(QtGui.QPixmap("images/Auth0Hover.png"), QtGui.QIcon.Selected, QtGui.QIcon.On)-10
self.pushButton.setIcon(icon)
self.pushButton.setObjectName("pushButton")
self.toolButton = QtWidgets.QToolButton(self.centralwidget)
self.toolButton.setGeometry(QtCore.QRect(170, 30, 181, 231))
self.toolButton.setText("")
self.toolButton.setIcon(icon)
self.toolButton.setIconSize(QtCore.QSize(200, 300))
self.toolButton.setCheckable(True)
self.toolButton.setChecked(True)
self.toolButton.setObjectName("toolButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1090, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)-10
self.statusbar.setStyleSheet("border-image: url(:/images/Button5-3.png)")
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if name == "main":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_()) -
Well my first question would be -- Is there a reason that you are not using the latest version of Python?
If no, then I would strongly suggest changing that element first.
If yes, then I would strongly suggest analyzing that to see if there is a way to easily move to the newest version.
For unless you are absolutely tied to an earlier version due to some compatibility restraint that you simply cannot isolate it is always a good practice to use the current version of your language because sooner or later you will have to and it is best to do it as soon as you feasibly can. For instance that is what I am doing upgrading a program because they did not choose to do so incrementally and now it has to be rewritten basically from scratch
That being said as I stated I copied your program and tried to run it in a python 3.7 / pyqt5 instance and it crashed even after tweaking it a little I still could not get it to run. On a positive note though I do believe I have done what it is you are trying to do but without being able to see what your program was supposed to do I cannot truly say that I have. So we need to get your program into a viable working state first.
-
Okay finally got a semi-copy of your code running -- below is the very slightly cleaned up tweaked version that basically does not work properly but does work. I have also commented on the few changes I had to make to get the code to function. I am guessing you used the Designer to create this because its a fairly ugly code which looks a lot like that stuff the Designer spits out. Note the biggest issue I encountered was the two cases of doing integer math on an object which my Python 3.7 / PyQt5 will not allow
import sys from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1090, 681) MainWindow.setStyleSheet("") self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(150, 290, 271, 121)) self.pushButton.setStyleSheet("") self.pushButton.setText("") icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("images/red-off-16.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("images/red-off-16.png"), QtGui.QIcon.Normal, QtGui.QIcon.On) icon.addPixmap(QtGui.QPixmap("images/red-off-16.png"), QtGui.QIcon.Disabled, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("images/red-off-16.png"), QtGui.QIcon.Disabled, QtGui.QIcon.On) icon.addPixmap(QtGui.QPixmap("images/red-on-16.png"), QtGui.QIcon.Active, QtGui.QIcon.Off) icon.addPixmap(QtGui.QPixmap("images/red-on-16.png"), QtGui.QIcon.Active, QtGui.QIcon.On) icon.addPixmap(QtGui.QPixmap("images/grn-hov-16.png"), QtGui.QIcon.Selected, QtGui.QIcon.Off) # Remarked this line out because it gives an error doing integer math on an object ?? # icon.addPixmap(QtGui.QPixmap("images/grn-hov-16.png"), QtGui.QIcon.Selected, QtGui.QIcon.On)-10 self.pushButton.setIcon(icon) self.pushButton.setObjectName("pushButton") self.toolButton = QtWidgets.QToolButton(self.centralwidget) self.toolButton.setGeometry(QtCore.QRect(170, 30, 181, 231)) self.toolButton.setText("") self.toolButton.setIcon(icon) self.toolButton.setIconSize(QtCore.QSize(200, 300)) self.toolButton.setCheckable(True) self.toolButton.setChecked(True) self.toolButton.setObjectName("toolButton") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1090, 22)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) # I added a modified version to make the program work self.statusbar = QtWidgets.QStatusBar(MainWindow) #This is the original line that gives an error because your doing integer math on an object?? # self.statusbar = QtWidgets.QStatusBar(MainWindow)-10 # Do not have the image so re-marked it out should have had no ill effect but not 100% sure # self.statusbar.setStyleSheet("border-image: url(:/images/Button5-3.png)") self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())