How To Create QStackedWidget in PyQt5

In this PyQt5 article we are going to learn How To Create QStackedWidget in PyQt5, QStackedWidget can be used to create a user interface similar to the one provided by QTabWidget. It is a convenience layout widget built on top of the QStackedLayout class.

 

 

What is PyQt5 QStackedWidget ?

PyQt5 QStackedWidget is a widget that allows you to stack multiple widgets on top of each other and display only one widget at a time. QStackedWidget widget is commonly used in graphical user interfaces (GUIs) to display different screens or pages in single window.

Each widget in QStackedWidget is assigned an index value, and widget with the highest index value is displayed on top. you can use setCurrentIndex() method to switch between the widgets and display the one you want to show.

some of the common properties and methods of the QStackedWidget widget includes number of widgets in the stack, current widget index, index of specific widget and list of widgets in the stack. QStackedWidget widget also emits signals when the current widget is changed or when a new widget is added to or removed from the stack.

QStackedWidget can be used to implement wizards, tabbed interfaces and multi page dialogs. It is easy way to organize multiple screens or pages within single window and can improve the user experience by reducing clutter and allowing the user to focus on the task at hand.

 

 

 

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

 

 

 

Create QStackedWidget in PyQt5

First of all we need some imports.

 

 

 

 

After that we are going to create our StackedWidget class that extends from QWidget and we initialize and set our window requirements in the constructor of that class also we have called our stackedWidgt() method in the class.

 

 

 

 

In here we have created the object of QVBoxLayout, also we have created the object of QStackedWidget and we have added the stacked widget to the vbox layout.

 

 

 

Now we need to create some buttons for our stacked widget, and we are going to use for loop to create 8 buttons for us.

 

 

 

After that we create btn_clicked() slot because we want to connect that with clicked signal of button.

 

 

 

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 PyQt5 How To Create QStackedWidget

 

 

 

 

Run the code and this is the result

How To Create QStackedWidget in PyQt5
How To Create QStackedWidget in PyQt5

 

 

 

This is the second example on PyQt5 QStackWidget

In this example we have defined two different widgets (Widget1 and Widget2) and use them to populate QStackedWidget. each widget contains QLabel and QPushButton and the buttons are used to switch between the two widgets.

when button is clicked, we use setCurrentIndex() method of the QStackedWidget to switch to the other widget. the initial widget displayed is set using setCurrentIndex(0).

when you run this code, you should see first widget displayed with button to switch to the second widget. when you click the button, the second widget will be displayed with a button to switch back to the first widget.

 

 

 

Run the code and this is the result

How To Create QStackedWidget in PyQt5
How To Create QStackedWidget in PyQt5

 

 

 

FAQs:

 

How to use stacked widget in Qt?

In Qt, you can use QStackedWidget class to create a stack of widgets where only one widget is visible at a time. 

 

 

Who makes PyQt?

PyQt is developed and maintained by Riverbank Computing. It is a set of Python bindings for the Qt application framework, and it enables you to create cross-platform GUI applications using Python programming language.

 

 

How to import PyQt in Python?

In Python, you can import PyQt using the following statement:

This statement imports the QtWidgets module, which contains classes for creating GUI applications, also QtCore and QtGui modules, which provide core functionality and graphical elements. These modules are typically required when working with PyQt applications.

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×