python logo

pyqt tabs


Python hosting: Host, run, and code Python in the cloud!

Tabs are a prominent feature in various graphical applications. These tabbed interfaces can be seen in web browsers, text editors, and numerous other software applications. In order to craft a tabbed interface in your application, PyQt offers the QTabWidget() method. Each individual tab is represented as a QWidget(). Associating these QWidgets to the QTabWidget is achieved through the following method:

tabs.addTab(tab1,"Tab 1")

Here, the initial parameter is the tab’s object, while the second denotes the label that gets displayed to the user. In our example, we’ve embellished the first tab (QWidget) with a few buttons for demonstration.

Related course:

Detailed Example:

This sample code leverages the qtabwidget to demonstrate the tab display mechanism in PyQt:

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

def main():

app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()

# Initializing tabs
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
tab3 = QtGui.QWidget()
tab4 = QtGui.QWidget()

# Adjusting dimensions
tabs.resize(250, 150)

# Designing the first tab's layout
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)

# Integrating tabs
tabs.addTab(tab1,"Tab 1")
tabs.addTab(tab2,"Tab 2")
tabs.addTab(tab3,"Tab 3")
tabs.addTab(tab4,"Tab 4")

# Assigning a title and making it visible
tabs.setWindowTitle('PyQt QTabWidget Tutorial at pythonspot.com')
tabs.show()

sys.exit(app.exec_())

if __name__ == '__main__':
main()

Outcome:

PyQT Tabs Example

Interested in more such PyQt samples? Download the Bulk Collection of PyQT Code.

Previous Tutorial | Next Tutorial






Leave a Reply:




Thor Sun, 17 May 2015

Okay, I tried the sample...I'm officially impressed...
Python as the vehicle of choice for desktop programs...
Thanks :)

Frank Sun, 17 May 2015

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