Here's a PySide6 program that creates exactly what you described:
import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QLabel, QScrollArea)
from PySide6.QtCore import Qt
from PySide6.QtGui import QWheelEvent
class CustomScrollArea(QScrollArea):
"""Custom QScrollArea that can toggle wheel scrolling on/off"""
def __init__(self):
super().__init__()
self.wheel_enabled = True
def wheelEvent(self, event: QWheelEvent):
"""Override wheel event to control scrolling"""
if self.wheel_enabled:
super().wheelEvent(event)
else:
# Ignore the wheel event
event.ignore()
def set_wheel_enabled(self, enabled: bool):
"""Enable or disable wheel scrolling"""
self.wheel_enabled = enabled
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Scrollable Labels with Toggle")
self.setGeometry(100, 100, 400, 300)
# Create central widget and main layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QHBoxLayout(central_widget)
# Create the scroll area
self.scroll_area = CustomScrollArea()
self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll_area.setWidgetResizable(True)
# Create widget to hold the labels
scroll_widget = QWidget()
scroll_layout = QVBoxLayout(scroll_widget)
scroll_layout.setSpacing(10)
scroll_layout.setContentsMargins(10, 10, 10, 10)
# Create 7 labels
self.labels = []
for i in range(1, 8):
label = QLabel(f"Label {i}")
label.setAlignment(Qt.AlignCenter)
label.setStyleSheet("""
QLabel {
background-color: #f0f0f0;
border: 2px solid #333;
padding: 20px;
font-size: 16px;
font-weight: bold;
}
""")
label.setMinimumHeight(60)
scroll_layout.addWidget(label)
self.labels.append(label)
# Add stretch at the bottom to keep labels together
scroll_layout.addStretch()
# Set the widget in scroll area
self.scroll_area.setWidget(scroll_widget)
# Calculate height to show only 3 labels
# Each label is 60px height + 20px*2 padding + 4px border = 104px
# Plus 10px spacing between labels
# Plus margins (10px top and bottom)
label_total_height = 104 # height + padding + border
spacing = 10
margins = 20 # top and bottom margins
visible_height = (label_total_height * 3) + (spacing * 2) + margins
self.scroll_area.setFixedHeight(visible_height)
# Create toggle label for wheel control
self.toggle_label = QLabel("Allow")
self.toggle_label.setAlignment(Qt.AlignCenter)
self.toggle_label.setStyleSheet("""
QLabel {
background-color: #4CAF50;
color: white;
padding: 10px;
font-size: 14px;
font-weight: bold;
border-radius: 5px;
}
QLabel:hover {
background-color: #45a049;
cursor: pointer;
}
""")
self.toggle_label.setFixedWidth(80)
self.toggle_label.setCursor(Qt.PointingHandCursor)
self.toggle_label.mousePressEvent = self.toggle_wheel_scrolling
# Create layout for toggle control
toggle_layout = QVBoxLayout()
toggle_layout.addWidget(QLabel("Wheel Scroll:"))
toggle_layout.addWidget(self.toggle_label)
toggle_layout.addStretch()
# Add scroll area and toggle control to main layout
main_layout.addWidget(self.scroll_area)
main_layout.addLayout(toggle_layout)
def toggle_wheel_scrolling(self, event):
"""Toggle between Allow and Stop for wheel scrolling"""
if self.toggle_label.text() == "Allow":
self.toggle_label.setText("Stop")
self.toggle_label.setStyleSheet("""
QLabel {
background-color: #f44336;
color: white;
padding: 10px;
font-size: 14px;
font-weight: bold;
border-radius: 5px;
}
QLabel:hover {
background-color: #da190b;
cursor: pointer;
}
""")
self.scroll_area.set_wheel_enabled(False)
else:
self.toggle_label.setText("Allow")
self.toggle_label.setStyleSheet("""
QLabel {
background-color: #4CAF50;
color: white;
padding: 10px;
font-size: 14px;
font-weight: bold;
border-radius: 5px;
}
QLabel:hover {
background-color: #45a049;
cursor: pointer;
}
""")
self.scroll_area.set_wheel_enabled(True)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
This program creates:
- 7 Vertical QLabels: Each labeled "Label 1" through "Label 7" with distinct styling
- QScrollArea: Sized to show only 3 labels at a time
- Vertical Scroll Bar: Always visible on the right side
- Toggle QLabel: Displays "Allow" or "Stop" to control mouse wheel scrolling
- Green "Allow" state: Mouse wheel scrolling is enabled
- Red "Stop" state: Mouse wheel scrolling is disabled
Key features:
- The
CustomScrollArea class extends QScrollArea to override the wheelEvent method
- The toggle label is clickable and changes color/text when clicked
- When set to "Stop", the mouse wheel is ignored but the scroll bar still works
- The scroll area height is calculated to show exactly 3 labels with proper spacing
The program provides a clean interface where users can easily toggle between allowing and disabling mouse wheel scrolling while maintaining scroll bar functionality at all times.