Package: pygame-imslider
pygame-imsliderFlexible images slider for Pygame engine.Defaultslider=ImSlider((800,300))Multiple Slidesslider=ImSlider((800,300),per_page=3,rewind=True)1 Slide Per Moveslider=ImSlider((800,300),stype=STYPE_LOOP,per_page=3,per_move=1)Focus Centerslider=ImSlider((800,300),stype=STYPE_LOOP,per_page=3,per_move=2,focus='center')Fade Transitionslider=ImSlider((800,300),stype=STYPE_FADE,rewind=True,focus=False)Install$ pip3 install pygame-imsliderBasic usageImSlideronly require a pygame surface to be displayed on and a index consumer function, as
in the following example :frompygame_imsliderimport*defconsumer(index):print('Current index :%s'%index)# Initializes and activates ImSliderslider=ImSlider((300,100),callback=consumer)slider.load_images(['image1.png','image2.png','image3.png',])The slider has the following optional parameters:stype: determine a slider type: STYPE_SLIDE, STYPE_LOOP or STYPE_FADEper_page: determine how many slides should be displayed per page. Ignored if
stype=STYPE_FADE.per_move: determine how many slides should be moved when a slider goes
to next or perv. Ignored if stype=STYPE_FADE.focus: determine which slide should be focused if there are multiple
slides in a page. A string “center” is acceptable for centering slides.rewind: whether to rewind a slider before the first slide or after the
last one. Ignored if stype=STYPE_LOOP.speed: transition duration in seconds.renderer: a ImSliderRenderer to customize colors of the slidercallback: callback called each time the selection is changed.Event managementAImSliderobject handles the following pygame event :MOUSEBUTTONDOWNMOUSEBUTTONUPFINGERDOWNFINGERUPKEYDOWNKEYUPJOYHATMOTIONIn order to process those events, slider instance event handling method should be called like
in the following example:whileTrue:events=pygame.event.get()# Update internal variablesslider.update(events)# Draw the sliderslider.draw(surface)## Perform other tasks here## Update the displaypygame.display.flip()Theglobal performances can be improved avoiding to flip the entire displayat each
loop by using thepygame.display.update()function.whileTrue:# Draw the sliderrects=slider.draw(surface)# Update only the dirty rectangles of the displaypygame.display.update(rects)Notethesurfaceparameter of thedraw()method is optional, it is used to
clear/hide the slider when it is necessary and may be mandatory if the surface
has changed.Custom rendering using ImSliderRendererIf you want to customize slider rendering you could provide aImSliderRendererinstance atImSliderconstruction.slider=ImSlider(size,renderer=ImSliderRenderer.DARK)Here is the list of default renderers provided withpygame-imslider:ImSliderRenderer.DEFAULTImSliderRenderer.DARKA customImSliderRenderercan be built using following constructor :renderer=ImSliderRenderer(# RGB tuple for arrow color (one tuple per state: released, pressed).((255,255,255),(54,54,54)),# RGB tuple for page-dot color (one tuple per state: released, pressed).((120,120,120),(54,54,54)),# RGB tuple for slide color.(242,195,195),# RGB tuple for selected image color.(245,95,76),# RGB tuple for selected page-dot color.(255,255,255),# RGB tuple for background color.(32,135,156))You can also create your own renderer. Just overrideImSliderRendererclass and
override any of the following methods:draw_arrow(surface, arrow): Draw an arrow.draw_arrow_state(surface, arrow): Draw arrow state.draw_dot(surface, dot): Draw a dot.draw_dot_state(surface, dot): Draw page-dot statedraw_slide(surface, slide): Draw a slide.draw_slide_state(surface, slide): Draw slide state.draw_background(surface): Draw background.Getting/Setting dataSeveral information can be retrieved from the slider:slider=ImSlider(...)# Load a sequence of image files.slider.load_images(['image1.png','image2.png','image3.png'])# Get a pygame.Rect object in which the slider is included.slider.get_rect()# Get the current pygame image (optionally resized).slider.get_image(resized=False)# Get the current index.slider.get_index()# Set the current index.slider.set_index(2)# Hide left and right arrowsslider.set_arrows_visible(False)Run examplesSeveral examples are provided with thepygame_imsliderlibrary.
To run the examples, simply execute these commands in a terminal:python-mpygame_imslider.examples.defaultpython-mpygame_imslider.examples.multiplepython-mpygame_imslider.examples.one_per_movepython-mpygame_imslider.examples.small_looppython-mpygame_imslider.examples.focuspython-mpygame_imslider.examples.fadeContributingIf you develop you own renderer please share it ! I will keep a collection of
rendering class in this repository. Don’t hesitate to report bug, feedback,
suggestion into the repository issues section.