QT4 Tabs

Tabs are very useful in graphical applications. They appear in webbrowsers, text editors and any other apps.  To create a tabbed window, you need to call the  QTabWidget()  function.  Every tab is a QWidget() which you have seen before.  You can connect the QWidgets with the QTabWidget with the function:

tabs.addTab(tab1,"Tab 1")

where the first parameter is the tab object and the second the name that appears on the screen. We added some buttons to the first tab (QWidget).

Related course:
Create Simple GUI Applications with Python and Qt

Example code:

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
def main():
    app 	= QtGui.QApplication(sys.argv)
    tabs	= QtGui.QTabWidget()
    # Create tabs
    tab1	= QtGui.QWidget()	
    tab2	= QtGui.QWidget()
    tab3	= QtGui.QWidget()
    tab4	= QtGui.QWidget()
    # Resize width and height
    tabs.resize(250, 150)
    # Set layout of first tab
    vBoxlayout	= QtGui.QVBoxLayout()
    pushButton1 = QtGui.QPushButton("Start")
    pushButton2 = QtGui.QPushButton("Settings")
    pushButton3 = QtGui.QPushButton("Stop")
    # Add tabs
    tabs.addTab(tab1,"Tab 1")
    tabs.addTab(tab2,"Tab 2")
    tabs.addTab(tab3,"Tab 3")
    tabs.addTab(tab4,"Tab 4") 
    # Set title and show
    tabs.setWindowTitle('PyQt QTabWidget @ pythonspot.com')
if __name__ == '__main__':


PyQT Tabs
PyQT Tabs

Download PyQT Code (Bulk Collection)


4 thoughts on “QT4 Tabs

  1. Francesco Gallo - August 27, 2017

    Hello….I am new in pyqt4 and python programming. The my question is: How do I enable or disable a tab as a function of a previous tab value?


    1. Frank - August 29, 2017

      There’s the method setTabEnabled(int index, bool enable). Specify the tab index and the enable boolean (True/False).
      To disable the 3rd tab, you can write:


      Counting starts from zero, so 2 is the 3rd tab. True/False should start with a capital letter.

  2. Thor - May 17, 2015

    Okay, I tried the sample…I’m officially impressed…
    Python as the vehicle of choice for desktop programs…
    Thanks 🙂

    1. Frank - May 17, 2015

      Awesome! There will be more tutorials soon, I may write some this evening.

Leave a Reply