How To Create QTabWidget In PyQt5

In this article i want to show you How To Create QTabWidget in PyQt5, we are using QTabWidget class for this. the QTabWidget class provides a stack of tabbed widgets. a tab widget provides a tab bar  and a “page area” that is used to display pages related to each tab. By default, the tab bar is shown above the page area, but different configurations are available



What is QTabWidget in PyQt5 ?

In PyQt5 QTabWidget is a widget that provides tabbed interface for displaying multiple widgets within single window. it allows you to switch between different pages or tabs, each of which can contain its own set of widgets. QTabWidget widget is commonly used in graphical user interfaces (GUIs) to display multiple views or modes within a single application.

each tab in QTabWidget is associated with widget that is displayed when the tab is selected. you can use the addTab() method to add a new tab and assign a widget to it. the widget can be any PyQt5 widget, such as QLabel, QPushButton or QComboBox.

QTabWidget provides several methods to customize the appearance and behavior of the tabs, such as setTabText() to set the text of a tab, setTabToolTip() to set the tooltip of a tab, and setTabEnabled() to enable or disable a tab. You can also use the currentChanged signal to detect when the user changes the selected tab.




Also you can read more Python GUI articles in the below links




First we need some imports




After that we are going to create our main window class that extends from QDialog and we have added our window requirements in the constructor of that class. for example we have set the window title, window icon.




If you want to give a color for your window you can use this code.




So now we need to create the object of QVBoxLayout, QTabWidget and also QDialogButtonBox. the  QDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the current widget style. Dialogs and message boxes typically present buttons in a layout that conforms to the interface guidelines for that platform. Invariably, different platforms have different layouts for their dialogs. QDialogButtonBox allows a developer to add buttons to it and will automatically use the appropriate layout for the user’s desktop environment.


Most buttons for a dialog follow certain roles. Such roles include:

  • Accepting or rejecting the dialog.
  • Asking for help.
  • Performing actions on the dialog itself (such as resetting fields or applying changes).





These are the roles for our button box.




In here iam going to set the font for my TabWidget.




OK now we are going to add our two tab classes in our TabWidget.




Also we need to add our tabwidget and buttonbox to the vbox layout.




And this is our two tab class that we have already added to our TabWidget. and we have added some widgets in these two tabs.




Also every PyQt5 application must create an application object. 




Finally, we enter the mainloop of the application. The event handling starts from this point. 






Complete source code for this article





Run the code and this is the result

How To Create QTabWidget In PyQt5
How To Create QTabWidget In PyQt5






Also you can watch the complete video for this article

Leave a Comment

Share via
Copy link
Powered by Social Snap