In this Python PySide6 article we are going to learn about How to Create Scrollbar & Slider in Python PySide6, so PySide6 is powerful and popular Python binding for Qt framework. It provides different tools and widgets for creating graphical user interfaces (GUI) in Python. in this article we will learn how to create scrollbar and slider widgets in PySide6, but before that we need to install PySide6.
You can use pip for PySide6 installation
1 |
pip install PySide6 |
PySide6 Scrollbar Widget
Scrollbar widgets are used to provide scrolling mechanism for windows or widgets that contain more information than can be displayed at one time. Scrollbars are typically placed at the edge of a widget, such as text area or list box.
For creating scrollbar widget in PySide6, we will use the QScrollBar class. this is an example code that creates vertical scrollbar widget and adds it to a window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QScrollBar class Example(QMainWindow): def __init__(self): super().__init__() scrollbar = QScrollBar(self) scrollbar.move(50, 50) scrollbar.resize(30, 200) self.setGeometry(300, 300, 350, 250) self.setWindowTitle('GeeksCoders - Scrollbar Example') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) |
In the above code we have created vertical scrollbar using QScrollBar class, sets its position using move() method, set its size using resize() method, and add it to the window using the self argument. also we set the window geometry and title and then show it.
If you run your code you will see this result
Slider Widget
Slider widgets are used to provide different values that can be selected by the user. Sliders can be horizontal or vertical and can be used for different purposes, such as selecting a volume level or adjusting a brightness setting.
For creating slider widget in PySide6, we will use the QSlider class. this is an example code that creates a horizontal slider widget and adds it to the window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QSlider from PySide6.QtCore import Qt class Example(QMainWindow): def __init__(self): super().__init__() slider = QSlider(self) slider.setOrientation(Qt.Orientation.Horizontal) slider.setGeometry(50, 50, 200, 30) self.setGeometry(300, 300, 350, 250) self.setWindowTitle('GeeksCoders - Slider Example') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) |
In the above code we have created horizontal slider using the QSlider class, set its orientation using the setOrientation() method, set its position and size using the setGeometry() method, and add it to the window using the self argument. We also set the window geometry and title, and then show it.
(How to Create Scrollbar & Slider in Python PySide6)
Run your code and this is the result
Learn More on Python GUI
- How to Create Label in PySide6
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6