lib_slide_bar.py

Created by ilyas-r

Created on March 30, 2023

1.99 KB

slide_bar() function allows to display sliding bars, with the value of the bar state.
params of slide_bar()
x: x pos of slide bar
y: y pos of slide bar
range: range of slide bar
selected_color: enable or disable the selected color
front_color: color of the cursor and the selected before
back_color: color of the slide_bar
cursor: default value of cursor

▶ and ◀ allow to make movements.

By Ilyas R.


from kandinsky import *
from ion import *

# By Ilyas R. June 2022

"""
params of slide_bar function
x: x pos of slide bar, int
y: y pos of slide bar, int
range: range of slide bar, int
selected_color: enable or disable the selected color, bool
front_color: color of the cursor and the selected before, str|tuple
back_color: color of the slide_bar, str|tuple
cursor: default value of cursor, int
"""

abs_ws_v = (0, 10, 20)
abs_nw_v = (0, 75, 150)

def slide_bar(x, y, rge, selected_color=True, front_color="black", back_color="grey", cursor=0):
    key_sum = 0
    info(cursor)
    fill_rect(x, y, rge, 6, back_color)
    fill_rect(cursor + x - 6, y - 6 // 2, 12, 12, front_color)
    if selected_color:
        fill_rect(x, y, cursor, 6, front_color)
    while not keydown(17):
        if keydown(3) and cursor < rge:
            if key_sum == abs_ws_v[0] or key_sum == abs_ws_v[1] or key_sum > abs_ws_v[2]:
                fill_rect(cursor + x - 6, y - 6 // 2, 6 * 2, 6 * 2, "white")
                fill_rect(x, y, rge, 6, back_color)
                cursor += 1
                if selected_color:
                    fill_rect(x, y, cursor, 6, front_color)
                fill_rect(cursor + x - 6, y - 6 // 2, 6 * 2, 6 * 2, front_color)
                info(cursor)
            key_sum += 1
        if keydown(0) and cursor > 0:
            if key_sum == -abs_ws_v[0] or key_sum == -abs_ws_v[1] or key_sum < -abs_ws_v[2]:
                fill_rect(cursor + x - 6, y - 6 // 2, 6 * 2, 6 * 2, "white")
                fill_rect(x, y, rge, 6, back_color)
                cursor -= 1
                if selected_color:
                    fill_rect(x, y, cursor, 6, front_color)
                fill_rect(cursor + x - 6, y - 6 // 2, 6 * 2, 6 * 2, front_color)
                info(cursor)
            key_sum -= 1
        if not (keydown(0) or keydown(3)):
            key_sum = 0


def info(cursor):
    fill_rect(180, 55, 30, 20, "white")
    draw_string("Value: " + str(cursor), 110, 55)


slide_bar(32, 111, 255)