Skip to content Skip to sidebar Skip to footer

Pyqt Button Click Doesn't Work

So my problem is that instead of manually writing a ton of code for a bunch of buttons, I want to create a class for a QPushButton and then change so many variables upon calling th

Solution 1:

You should perform the connection to the clicked signal on the __init__ method:

from PyQt4 import QtGui,QtCore

classButton(QtGui.QPushButton):
    def__init__(self, parent):
        super(Button, self).__init__(parent)
        self.setAcceptDrops(True)

        self.setGeometry(QtCore.QRect(90, 90, 61, 51))
        self.setText("Change Me!")
        self.clicked.connect(self.printSomething) #connect here!#no need for retranslateUi in your code exampledefprintSomething(self):
        print"Hello"classMyWindow(QtGui.QWidget):
    def__init__(self):
        super(MyWindow,self).__init__()
        self.btn = Button(self)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.btn)
        self.setLayout(layout)


app = QtGui.QApplication([])
w = MyWindow()
w.show()
app.exec_()

You can run it and will see the Hello printed on the console every time you click the button.

The retranslateUi method is for i18n. You can check here.

Post a Comment for "Pyqt Button Click Doesn't Work"