menulib2.py

Created by fime

Created on November 28, 2021

1.6 KB

Menulib is a simple function that allow the developper to create a customizable interactive menu.
Bug report or review here
Documentation here
Exemple here


from ion import keydown
from kandinsky import fill_rect as drawRect, draw_string as drawTxt

def menu(x,y,elements,col=(0,0,0),bg_col=(255,255,255)):
  def kd(x):
    if keydown(x):
      while keydown(x):pass
      return True
    return False
  kd(4)
  el_size,select,txt_size,draw=25,0,[0 for i in range(len(elements))],1
  while True:
    if kd(1):
      draw=1
      select=max(0,select-1)
    if kd(2):
      draw=1
      select=min(len(elements)-1,select+1)
    if kd(0):
      draw=1
      type=elements[select][0]
      if type=="sld":elements[select][-1]=max(elements[select][-1]-1,elements[select][2][0])
      if type=="lst":elements[select][-1]=max(elements[select][-1]-1,0)
    if kd(3):
      draw=1
      type=elements[select][0]
      if type=="sld":elements[select][-1]=min(elements[select][-1]+1,elements[select][2][1])
      if type=="lst":elements[select][-1]=min(elements[select][-1]+1,len(elements[select][2])-1)
    if kd(4) and elements[select][0]=="btn":
      break

    if draw:
      for nb,el in enumerate(elements):
        drawRect(x,y+el_size*nb,10*txt_size[nb],el_size,bg_col)
        slcted=1 if nb==select else 0
        type=el[0]
        name=el[1]
        val=el[-1]
        if type=="btn":disp_txt=name
        elif type=="sld":disp_txt=name+" : {}".format(val)
        elif type=="lst":disp_txt=name+" : {}".format(el[2][val])
        else : disp_txt="error"
        if slcted: disp_txt="> "+disp_txt
        txt_size[nb]=len(disp_txt)
        drawTxt(disp_txt,x,y+nb*el_size,col,bg_col)
        draw=0
  return elements[select][1],{x[1]:x[-1] for x in elements if x[0]!="btn"}

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.