PyQt5 QPushButton

QPushButton is a simple button in PyQt, when clicked by a user some associated action gets performed. For adding this button into the application, QPushButton class is used.

Example:

A window having a Push Button, when clicked a message will appear “You clicked Push Button”.

Below is the code:

filter_none

edit
close

play_arrow

link
brightness_4
code

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
  
class Ui_MainWindow(object):
  
    def setupUi(self, MainWindow):
        MainWindow.resize(506, 312)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
          
        # adding pushbutton
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(200, 150, 93, 28))
  
        # adding signal and slot 
        self.pushButton.clicked.connect(self.changelabeltext)
    
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(140, 90, 221, 20))      
  
        # keeping the text of label empty before button get clicked
        self.label.setText("")     
         
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Push Button"))
          
    def changelabeltext(self):
  
        # changing the text of label after button get clicked
        self.label.setText("You clicked PushButton")    
  
        # Hiding pushbutton from the main window
        # after button get clicked. 
        self.pushButton.hide()   
  
if __name__ == "__main__"
    app = QtWidgets.QApplication(sys.argv) 
    
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show()
   
    sys.exit(app.exec_()) 

chevron_right


Output:

Main Window having push button.

After clicking the button message appeared “You clicked Push Button.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.