Skip to main content

A clean and customizable int and float slider widget for PyQt and PySide

Project description

PyQt Advanced Slider

PyPI Python Build Coverage License

A clean and customizable int and float slider widget for PyQt and PySide

pyqt-advanced-slider

Features

  • Supports int and float
  • Supports dynamic switching between types
  • Customizable decimal places
  • Customizable prefix and suffix
  • Customizable decimal separator and thousands separator
  • Supports keyboard and mouse wheel inputs
  • Modern and customizable UI
  • Works with PyQt5, PyQt6, PySide2, and PySide6

Installation

pip install pyqt-advanced-slider

Usage

Import the Slider class and add it to your window like any other PyQt Widget:

from PyQt6.QtWidgets import QMainWindow
from pyqt_advanced_slider import Slider


class Window(QMainWindow):
    def __init__(self):
        super().__init__(parent=None)

        slider = Slider(self)  # Add slider
        slider.setRange(-50, 100)  # Set min and max
        slider.setValue(25)  # Set value
        slider.valueChanged.connect(self.slider_value_changed)  # Connect change event
    
    # Called every time the slider value changes
    def slider_value_changed(self, value):
        print(value)

You can also set the minimum and maximum of the slider individually with the setMinimum() and setMaximum() methods:

slider.setMinimum(-50)  # Default: 0
slider.setMaximum(100)  # Default: 10

The getValue() method returns the current value while the getValueFormatted() method returns the value as the formatted string that is being displayed on the slider:

value = slider.getValue()  # 2500.0
value_formatted = slider.getValueFormatted()  # e.g. '~2,500.00 €'

NOTE:
When getting the value of the slider using the getValue() method or by subscribing to the valueChanged event, it will either be an int or a float, depending on whether float values are enabled or disabled for the slider.

Customization

  • Making the slider a float slider:
slider.setFloat(True)  # Default: False
slider.setDecimals(2)  # Default: 1
  • Adding a prefix and a suffix:
slider.setPrefix('~')   # Default: empty string
slider.setSuffix(' €')  # Default: empty string

EXAMPLE:
The value 100 formatted with ~ as the prefix and ° as the suffix would be shown as ~100°

  • Customizing the formatting of the value shown on the slider:
slider.setDecimalSeparator(',')    # Default: '.'
slider.setThousandsSeparator('.')  # Default: empty string

EXAMPLE:
The value 1052.17 formatted with , as the decimal separator and . as the thousands separator would be 1.052,17

  • Changing how much the value is incremented or decremented on keyboard and mouse inputs:
# If left default, the single step and page step will be 1% and 5% of the slider's value range
slider.setSingleStep(10)  # Default: 0
slider.setPageStep(25)    # Default: 0

SINGLE STEP: Increment or decrement of the value when the slider is scrolled or the arrow keys are pressed
PAGE STEP: Increment or decrement of the value when the PageUp or PageDown key is pressed

  • Hiding the value on the slider completely:
slider.showValue(False)  # Default: True
  • Enabling or disabling keyboard and mouse wheel input:
slider.setKeyboardInputEnabled(False)    # Default: True
slider.setMouseWheelInputEnabled(False)  # Default: True
  • Setting custom colors:
slider.setTextColor(QColor('#0F0F0F'))                # Default: #000000
slider.setBackgroundColor(QColor('#FFFFFF'))          # Default: #D6D6D6
slider.setAccentColor(QColor.fromRgb(100, 100, 100))  # Default: #0078D7
slider.setBorderColor(QColor.fromRgb(0, 0, 0))        # Default: #D1CFD3
  • Making the corners rounded:
slider.setBorderRadius(3)  # Default: 0
  • Setting a custom font:
# Init font
font = QFont()
font.setFamily('Times')
font.setPointSize(10)
font.setBold(True)

# Set font
slider.setFont(font)

Examples for PyQt5, PyQt6, and PySide6 can be found in the examples folder.

Tests

Installing the required test dependencies pytest, pytest-qt, coveragepy, and PyQt6:

pip install pytest pytest-qt coverage PyQt6

To run the tests with coverage, clone this repository, go into the main directory and run:

coverage run -m pytest
coverage report --ignore-errors -m

License

This software is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqt-advanced-slider-1.1.1.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

pyqt_advanced_slider-1.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pyqt-advanced-slider-1.1.1.tar.gz.

File metadata

  • Download URL: pyqt-advanced-slider-1.1.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.3

File hashes

Hashes for pyqt-advanced-slider-1.1.1.tar.gz
Algorithm Hash digest
SHA256 2f1fb78186aa4d9f0b35a176b1deedbfb8c57ce9ab4029ec6e5118e76cf03e96
MD5 97708ae0577367d438bf181efd3fb0a8
BLAKE2b-256 bf8af6efa450c46b8cc7ba5febeff5d601b68e1d8dd71b68c2b900cbfee9baa9

See more details on using hashes here.

File details

Details for the file pyqt_advanced_slider-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pyqt_advanced_slider-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a40b454393db1d13edd52a03a4b1ee37055c2dcde12bc7f614fe424112373980
MD5 7e00e110efb7774947dc73ee2d92c9ef
BLAKE2b-256 1eb2bcaccebc3845296bc5db0e8f86d68293b2f9c3f65a82e9083a1de6784c5f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page