StackedWidget is a custom PyQt/PySide widget that enables overlaying one widget on top of another with flexible alignment and margin configurations. This is particularly useful for GUI designs where layered widgets (e.g., buttons, labels, or overlays) are needed.
- Overlay a top widget on a bottom widget with alignment options (
Left,Right,Top,Bottom,Center, etc.). - Supports dynamic resizing and alignment adjustments on window resize.
- Easy to integrate with PyQt or PySide applications.
- Fully customizable margins and alignment settings.
- Clone this repository:
git clone https://github.com/bornalgo/stacked-widget.git cd StackedWidget - Install the required dependencies:
pip install PySide2 # Or PySide6/PyQt6/PyQt5/PyQt4/PySide based on your preference - Install the package:
python setup.py install
from PySide2.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QCheckBox, QWidget from balQt.stacked_widget import StackedWidget # Create the application app = QApplication([]) # Main window setup window = QMainWindow() window.setWindowTitle("Stacked Widget Example") # Create bottom and top widgets button = QPushButton("Click Me!") button.setEnabled(False) checkbox = QCheckBox("Enable Button") # Connect checkbox state change to toggle button enabled state checkbox.stateChanged.connect(lambda: button.setEnabled(checkbox.isChecked())) # Create a StackedWidget stacked_widget = StackedWidget(button, checkbox) # Setup layout layout = QVBoxLayout() layout.addWidget(stacked_widget) container = QWidget() container.setLayout(layout) window.setCentralWidget(container) # Show window window.show() # Run application app.exec_()Overlay a checkbox on a button, where the checkbox controls the button's enabled state.
def toggle_button_state(checkbox, button): button.setEnabled(checkbox.isChecked()) checkbox.stateChanged.connect(lambda: toggle_button_state(checkbox, button))Use StackedWidget to overlay a "loading spinner" or a progress bar on top of an underlying content widget.
A custom widget for overlaying widgets.
Parameters:
bottom_widget (QWidget): Widget at the base.top_widget (QWidget): Widget overlaid on the bottom widget.parent (QWidget, optional): Parent widget. Defaults toNone.margin (int, optional): Space between the widgets. Defaults to0.alignment (Qt.Alignment, optional): Alignment of thetop_widget(e.g.,Qt.AlignRight). Defaults toQt.AlignRight.
Methods:
__init__(bottom_widget, top_widget, parent=None, margin=0, alignment=Qt.AlignRight): Initializes theStackedWidget.resizeEvent(event): Dynamically adjusts widget alignment and size upon resizing.
- Interactive User Interfaces:
- Overlay a checkbox or a label on a button or image.
- Loading Screens:
- Place a spinner or progress indicator on top of a widget.
- Advanced Layouts:
- Use layered designs for applications like form builders or dashboards.
- Dynamic States:
- Display dynamic states (e.g., "Processing", "Completed") over widgets.
Contributions are welcome! If you'd like to add features, fix bugs, or suggest improvements:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/new-feature). - Commit your changes (
git commit -m "Add new feature"). - Push to the branch (
git push origin feature/new-feature). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
PyQtPySideStackedWidgetQt Custom WidgetsOverlaysGUI DevelopmentPython GUI
StackedWidget/ ├── balQt │ ├── __init__.py │ └── stacked_widget.py ├── images │ └── screenshot.png ├── .gitignore # Ignore artifacts ├── example.py # Usage example ├── LICENSE # License file ├── README.md # Documentation └── setup.py # Setup file 