PyQT Toolbar




In this PyQT tutorial, we're going to show how to add a toolbar to our GUI application.

Unlike the menubar, it really could go either way with a toolbar regarding whether it is something that follows the user everywhere the go in the application, or not. For this, we're going to just put it in the home method.

Adding a tool bar is a little easier, usually, because the is no "drop down." You could add one, but, for now, we'll just keep it simple:

        extractAction = QtGui.QAction(QtGui.QIcon('todachoppa.png'), 'Flee the Scene', self)
        extractAction.triggered.connect(self.close_application)
        
        self.toolBar = self.addToolBar("Extraction")
        self.toolBar.addAction(extractAction)

First we define what the toolbar button will look like.
Next, we define what it will do when clicked.
Then we define the toolbar itself, and then finally add the button we built moments prior.

You can create your own images for the toolbar if you want, but, if you'd like the same one I used: http://pythonprogramming.net/static/images/todachoppa.png

The full code for our application now:

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

        extractAction = QtGui.QAction("&GET TO THE CHOPPAH!!!", self)
        extractAction.setShortcut("Ctrl+Q")
        extractAction.setStatusTip('Leave The App')
        extractAction.triggered.connect(self.close_application)

        self.statusBar()

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(extractAction)        

        self.home()

    def home(self):
        btn = QtGui.QPushButton("Quit", self)
        btn.clicked.connect(self.close_application)
        btn.resize(btn.minimumSizeHint())
        btn.move(0,100)

        extractAction = QtGui.QAction(QtGui.QIcon('todachoppa.png'), 'Flee the Scene', self)
        extractAction.triggered.connect(self.close_application)
        
        self.toolBar = self.addToolBar("Extraction")
        self.toolBar.addAction(extractAction)
        
        self.show()

    def close_application(self):
        print("whooaaaa so custom!!!")
        sys.exit()


def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()

The resulting window is something like:

In the next tutorial, we're going to cover how to create pop up messages, like warning messages to the user.

The next tutorial:





  • PyQT Basic Tutorial
  • PyQT Application Structure
  • PyQT buttons
  • Button Functions with PyQT
  • PyQT Menubar
  • PyQT Toolbar
  • Pop up Message PyQT
  • PyQT Check box
  • PyQT Progress bar example
  • PyQT Dropdown button and QT Styles
  • PyQT Font widget
  • PyQT Color picker widget
  • PyQT Text Editor
  • PyQT open files to edit
  • PyQT file saving