sd ashok
sd ashok

Reputation: 31

How to disable a QPushButton

I tried calling UI from another file, but couldn't disable a button. I don't know where to place .setEnabled(False). I placed it everywhere except main, but the button is still enabled.

import sys
from PyQt4 import QtCore, QtGui
from a import Ui_MainWindow

class machine(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(machine, self).__init__()
        self.setupUi(self)
        self.initUI()
        self.disablebtn()
        self.btn_Save.setEnabled(False);
        self.btn_Close.setEnabled(False);  
        self.show()

    def initUI(self):
        self.desktopSize()
        self.statusbar().showMessage("super dooper")
        self.btn_Save.setEnabled(False);
        self.btn_Close.setEnabled(False);  

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: 1

Views: 18039

Answers (1)

ekhumoro
ekhumoro

Reputation: 120798

Your machine class does disable the buttons correctly, but you never create an instance of it, and so it never gets a chance to work properly.

The code should probably look more like this:

import sys
from PyQt4 import QtCore, QtGui
from a import Ui_MainWindow

class machine(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(machine, self).__init__()
        self.setupUi(self)
        self.initUI()

    def initUI(self):
        self.statusBar().showMessage("super dooper")
        self.btn_Save.setEnabled(False)
        self.btn_Close.setEnabled(False)

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    MainWindow = machine()
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions