menu.py

Created by cent20

Created on April 19, 2020

2.42 KB

Génère un menu pour paramétrer une application python sur la NumWorks.
Documentation : https://nsi.xyz/menu
Ceci est le script prêt à utiliser. Le script de démonstration est menu_demo.py


from kandinsky import *
from ion import keydown

def wait(buttons=range(53)):  # Attends qu'une des touches précisées soit pressée
    while True:
        for i in buttons:
            if keydown(i):
                while keydown(i): True  # Fonction anti-rebond
                return i

def omega():  # Vérificateur d'OS
    try:
        get_keys()
        return True
    except:
        return False

def menu(titre, action, *para):
    n, g, f = (0, 0, 0), (96, 96, 96), (255, 183, 52)  # couleurs : réglages, optionsn focus
    if omega():
        f = (192, 53, 53)
    chx = [1 for i in para]  
    curs = 0  # Initialisation sur l'action
    draw_string(titre, int(160 - 5 * len(titre)), 8, (42, 120, 224))
    draw_string(action, int(160 - 5 * len(action)), 36, f)

    def menu_option(i, col, cursor):
        draw_string(" " * 13, 150, 64 + i * 25, col)
        txt = str(para[i][chx[i]])
        if txt.find("$") != -1:
            txt2 = txt[txt.find("$") + 1:]
            txt = txt[:txt.find("$")]
            draw_string(" " * 32, 0, 64 + (i + 1) * 25)
            draw_string(str(txt2), int(150 + (150 - 10 * len(str(txt2))) / 2), 64 + (i + 1) * 25, col)
        draw_string(" " * 17, 140, 64 + (i) * 25)
        draw_string(str(txt), int(150 + (150 - 10 * len(str(txt))) / 2), 64 + i * 25, col)
        draw_string("<" * cursor, 140, 64 + i * 25, g)
        draw_string(">" * cursor, 300, 64 + i * 25, g)

    for i in range(len(para)):
        draw_string(para[i][0], 10, 64 + i * 25, n)
        menu_option(i, g, False)

    while True:
        r = wait((0, 1, 2, 3, 4, 52))
        if r in (4, 52):  # Lance l'application
            return [para[i][chx[i]] for i in range(len(para))]
        elif r in (1, 2):
            if curs == 0:
                draw_string(action, int(160 - 5 * len(action)), 36, n)
            else:
                menu_option(curs - 1, g, False)
            curs = (curs - 1 * (r == 1) + 1 * (r == 2)) % (len(para) + 1)
            if curs == 0:
                draw_string(action, int(160 - 5 * len(action)), 36, f)
            else:
                menu_option(curs - 1, f, True)
        elif r in (0, 3) and curs != 0:
            chx[curs - 1] = chx[curs - 1] + 1 * (r == 3) - 1 * (r == 0)
            if chx[curs - 1] == 0:
                chx[curs - 1] = len(para[curs - 1]) - 1
            if chx[curs - 1] == len(para[curs - 1]):
                chx[curs - 1] = 1
            menu_option(curs - 1, f, True)

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 cookies policy.