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")
    vBoxlayout.addWidget(pushButton1)
    vBoxlayout.addWidget(pushButton2)
    vBoxlayout.addWidget(pushButton3)
    tab1.setLayout(vBoxlayout)   
 
    # 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')
    tabs.show()
 
    sys.exit(app.exec_())
 
 
if __name__ == '__main__':
    main()

Result:

PyQT Tabs

PyQT Tabs

Download PyQT Code (Bulk Collection)

QT4 Table
QT4 File Dialog
This entry was posted in QT4 and tagged , , , , . Bookmark the permalink.

4 Responses to QT4 Tabs

  1. 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?

    Thanks

    • Frank says:

      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:

      tabs.setTabEnabled(2,False)

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

  2. Thor says:

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