psychos.visual.Slider#
- class psychos.visual.Slider(initial_value: float | None = None, interval: Tuple[float, float] = (0.0, 100), position: Tuple[float, float] = (0, 0), width: str | int | float = '50vw', height: str | int | float = '4vh', color: ColorType = None, line_width: str | int | float = '2px', tick_width: str | int | float = '1px', ticks: Tuple[float, ...] | int | None = None, tick_labels: Tuple[str, ...] | None = None, tick_size: str | int | float = 10, tick_padding: str | float | int | None = None, circle_radius: str | int | float = '5px', circle_hover_increase: float = 1.5, circle_grab_increase: float = 1.5, action_radius: str | int | float = '10px', action_vertical_radius: str | int | float = '20px', circle_hover_color: ColorType = None, circle_grab_color: ColorType = None, window: Window | None = None, coordinates: UnitType | Unit | None = None)[source]#
Bases:
objectInteractive slider widget for Pyglet windows.
The
Sliderprovides a horizontal bar that allows users to select a continuous value using the mouse. It supports hover and grab states with customizable appearance and can be easily integrated into interactive or behavioral experiments.- Parameters:
initial_value (float, optional) – Initial slider value. Defaults to the midpoint of interval.
interval (tuple of float, default=(0.0, 100.0)) – Minimum and maximum numeric values of the slider.
position (tuple of float, default=(0, 0)) – Center position of the slider in the given coordinate system.
width (str or float, default="50vw") – Total width of the slider line (supports relative units, e.g., “vw”).
height (str or float, default="4vh") – Height of the slider area.
color (ColorType, optional) – Base color of the slider (RGBA or named color).
line_width (str or float, default="2px") – Thickness of the main slider line.
tick_width (str or float, default="1px") – Width of each tick mark line.
ticks (int or tuple of float, optional) – If int, number of evenly spaced tick marks. If tuple, explicit numeric tick values along the interval.
tick_labels (tuple of str, optional) – Optional text labels for each tick. Must match the number of ticks.
tick_size (str or float, default=10) – Length of each tick mark (in pixels or relative units).
tick_padding (str or float, optional) – Distance between the slider line and the tick labels, if provided.
circle_radius (str or float, default="5px") – Radius of the draggable circle indicating the current value.
circle_hover_increase (float, default=1.5) – Multiplicative factor for the circle radius when hovered.
circle_grab_increase (float, default=1.5) – Multiplicative factor for the circle radius when grabbed.
action_radius (str or float, default="10px") – Radius around the circle where hover or grab interactions are triggered.
action_vertical_radius (str or float, default="20px") – Vertical tolerance around the slider line for click-and-drag actions.
circle_hover_color (ColorType, optional) – Color of the circle when hovered. Defaults to color.
circle_grab_color (ColorType, optional) – Color of the circle when grabbed. Defaults to circle_hover_color.
window (Window, optional) – Target window where the slider is drawn. If not provided, uses the current active window.
coordinates (Unit or str, optional) – Coordinate system for positioning (e.g., “px”, “norm”, “vw”, “vh”).
Example
A minimal example showing a white slider and real-time value update:
>>> from psychos.visual import Window, Text, Slider >>> >>> window = Window(background_color="gray", mouse_visible=True) >>> text = Text("", position=(0, 0.3)) >>> slider = Slider(color="white", circle_grab_color="red", ticks=4) >>> >>> def callback(slider_state, _): ... text.text = f"Value: {slider_state.value:.2f}" ... text.draw() ... return False # continue interaction >>> >>> state = slider.wait_response(callback=callback, exit_key="SPACE") >>> print("Final value:", state.value)
- __init__(initial_value: float | None = None, interval: Tuple[float, float] = (0.0, 100), position: Tuple[float, float] = (0, 0), width: str | int | float = '50vw', height: str | int | float = '4vh', color: ColorType = None, line_width: str | int | float = '2px', tick_width: str | int | float = '1px', ticks: Tuple[float, ...] | int | None = None, tick_labels: Tuple[str, ...] | None = None, tick_size: str | int | float = 10, tick_padding: str | float | int | None = None, circle_radius: str | int | float = '5px', circle_hover_increase: float = 1.5, circle_grab_increase: float = 1.5, action_radius: str | int | float = '10px', action_vertical_radius: str | int | float = '20px', circle_hover_color: ColorType = None, circle_grab_color: ColorType = None, window: Window | None = None, coordinates: UnitType | Unit | None = None)[source]#
Methods
__init__([initial_value, interval, ...])draw()Draw the slider on the window.
wait_response([callback, max_wait, ...])Wait for user interaction with the slider and return the final state.
Attributes
Get the color of the text.
Get the coordinate system used for the text.
Get the height of the slider.
Get the initial value of the slider.
Get the position of the text in pixels.
Get the current value of the slider.
- wait_response(callback: Callable | None = None, max_wait: float | None = None, exit_key: str | Tuple[str, ...] = (), clock: Clock | None = None) float[source]#
Wait for user interaction with the slider and return the final state.
This method continuously updates and redraws the slider in real time while monitoring mouse movement, button presses, and keyboard input. It can optionally call a user-provided callback function every frame to handle custom logic (e.g., updating text, plotting feedback, or conditional stopping).
The loop terminates when:
The user presses any of the specified exit_key keys, or
The callback function returns
True, orThe optional time limit (max_wait) is reached.
- Parameters:
callback (callable, optional) –
A function called every frame during interaction,
callback(slider_state, window_state). It should accept two arguments:- slider_state (
SliderState) contains the current slider value, elapsed time, timestamp, and interaction state.
- slider_state (
- window_state (
InteractState) contains the current mouse position, pressed keys, and other window-level events.
- window_state (
The callback must return:
Trueto stop the interaction early.Falseto continue waiting.
max_wait (float, optional) – Maximum waiting time in seconds. If not provided, waits indefinitely.
exit_key (str or tuple of str, default="ESCAPE") – Key(s) that immediately terminate the interaction when pressed.
clock (Clock, optional) – Clock object providing precise timing. If not given, uses the default internal clock.
- Returns:
A named tuple with the final slider state, containing:
value: float — final slider value.elapsed_time: float — total interaction duration.timestamp: float — time when the interaction ended.slider_state: {“default”, “hover”, “grab”} — final circle state.has_been_updated: bool — whether the slider value was changed.
- Return type:
SliderState