Giter Site home page Giter Site logo

zetamap / pymenu Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 14 KB

The new library for making beautiful menus on numworks with dynamic accent colors. Thanks to cent20, for inspiring me for this library.

Home Page: https://my.numworks.com/python/zetamap/pymenu

Python 100.00%
library numworks numworks-python python3 python3-library numworks-library

pymenu's Introduction

PyMenu

The new library for making beautiful menus on numworks with dynamic accent colors. Thanks to cent20's library, menu.py, for inspiring me for this library.

To give a little aspect, here is a demo script showing the options menu of my game snake_lite.

How to use

This is simple, you just need to create an instance of Menu class and set menu title, menu confirm label, and options, like this :

menu = Menu(
  "My Title",  # Replace by your menu title (e.g. "Game Options")
  "Close menu",  # Replace by your menu confirm label (e.g. "Start Game")
  [  # And here is the options of menu. 
     #Must be formatted like this: ["<option label>", "<default value>", <values...>],
    ["My option", 0, "value 1", 2, [0,1]],
])

Note: The default option value must be an index of the list of values, not the value itself.

And after, just open the menu like this:

menu.open()

This will automatically clear the screen, format and display options, add a scrollbar if list exceed 6 options per page, handle keys to move through the menu, clear again the screen at end and return the result of selected options.
You can open the menu as many times you want by recalling the function, the last selected option will be automatically focused.

When you navigating through the menu, you can press EXE or OK to reset value of selected option.
To validate the menu, press EXE or OK on the action label, this will return selected values for options, in a simple list without the option label. Also to cancel the menu, press BACK, BACKSPACE or press EXE or OK on the arrow at the top left, this will return an empty list, so you can handle that to open another menu if you wish.

To go further...

You can change all colors of menu, the palette is stored in this variable : Menu.colors, and formatted like this:

  1. Default font color in darkmode or the background color in light mode.
  2. Background color in darkmode or the default font color in light mode.
  3. Font color for not selected options values.
  4. Accent color for focused option value.
  5. Font color for menu title.

There is also an hidden feature. You can automatically switch between light and dark mode by adding an option named “darkmode” with “enabled”/“disabled” as values.
And when user will change its value, the background and fonts colors will be changed.

Selected options values can be obtained without reopening the menu, by calling the method .options() of your menu object. If the menu has never opened, this will return default values.

The function accent_colors() has been created to get the accent and background colors for most common OSes. You can copy it for your own scripts if you want.

Limitations

There are some limitations with labels, to avoid text overlaps.
The title is limited to 25 characters, the confirm action to 28 characters, options label to 12 characters, options value to 15 characters, and multi-lines labels are not allowed, this will break the menu.
Note: A convertion is done for options label and values, so you don't need to convert it to string before.

Public Content

wait_key():

  • Parameters: k...
  • Description: Wait for one of specified keys to be pressed and released.

accent_colors():

  • Parameters: no parameters
  • Description: Return accent color and background color according to most common OSes.

Menu():

  • Parameters: title, action, options...
  • Description: Create a menu, with a title, an action name to confirm the menu, and the list of options.
    Options must be formatted like this: ["<label>", <default option>, [option values ...]],
  • Note: The default option value must be an index of the list of values, not the value itself.

Menu.open():

  • Parameters: no parameters
  • Description: Open the menu, display everything beautifully =), and return selected values.

Menu.close():

  • Parameters: no parameters
  • Description: Close the menu, so clear the screen and return selected options values.

Menu.options():

  • Parameters: no parameters
  • Description: Return the list of selected options values (without the option label, just value), or default values if the menu has never been opened.

pymenu's People

Contributors

zetamap avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.