Skip to content
Related Articles

Related Articles

Improve Article

PyQt5 QToolButton

  • Difficulty Level : Expert
  • Last Updated : 17 Sep, 2019

Tool button is a PyQt5 widget which looks like the buttons used in Toolbar. This button contains icon which gives an idea about its utility. For adding this button in application QToolButton class is used.


A window having a Tool button with an exit icon. When the user clicks this button the application gets closed.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(506, 312)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.toolButton = QtWidgets.QToolButton(self.centralwidget)
        self.toolButton.setGeometry(QtCore.QRect(220, 120, 41, 41))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("exiticon.png [exact location of image]"),
                                          QtGui.QIcon.Normal, QtGui.QIcon.Off)
        # adding icon to the toolbutton
        # adding signal and slot 
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    # For closing the application
    def exitapp(self):
if __name__ == "__main__"
    app = QtWidgets.QApplication(sys.argv) 
    MainWindow = QtWidgets.QMainWindow() 
    ui = Ui_MainWindow() 


When user clicks this button, application get closed.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :