Refresher UI element
from zpui_lib.ui import Refresher
counter = 0
def get_data():
counter += 1
return [str(counter), str(1000-counter)] #Return value will be sent directly to output.display_data
Refresher(get_data, i, o, 1, name="Counter view").activate()
Instantiating the Refresher:
- class zpui_lib.ui.Refresher(refresh_function, i, o, refresh_interval=1, keymap=None, name='Refresher', **kwargs)[source]
A Refresher allows you to update the screen on a regular interval. All you need is to provide a function that’ll return the text/image you want to display; that function will then be called with the desired frequency and the display will be updated with whatever it returns.
- __init__(refresh_function, i, o, refresh_interval=1, keymap=None, name='Refresher', **kwargs)[source]
Initialises the Refresher object.
Args:
refresh_function: a function which returns data to be displayed on the screen upon being called, in the format accepted byscreen.display_data()orscreen.display_image(). To be exact, supported return values are:Tuples and lists - are converted to lists and passed to
display_data()Strings - are converted to a single-element list and passed to
display_data()PIL.Image objects - are passed to
display_image()
i,o: input&output device objects
Kwargs:
refresh_interval: Time between display refreshes (and, accordingly,refresh_functioncalls).keymap: Keymap entries you want to set while Refresher is active. * By default, KEY_LEFT deactivates the Refresher, if you want to override it, make sure that user can still exit the Refresher.name: Refresher name which can be used internally and for debugging.
- activate()
A method which is called when the UI element needs to start operating. Is blocking, sets up input&output devices, refreshes the UI element, then calls the
idle_loopmethod while the UI element is active.self.in_foregroundis True, while callbacks are executed from the input device thread.
- deactivate()
Deactivates the UI element, exiting it.
- print_name()
A debug method. Useful for hooking up to an input event so that you can see which UI element is currently active.
More info:
- class zpui_lib.ui.Refresher(refresh_function, i, o, refresh_interval=1, keymap=None, name='Refresher', **kwargs)[source]
Bases:
BaseUIElementA Refresher allows you to update the screen on a regular interval. All you need is to provide a function that’ll return the text/image you want to display; that function will then be called with the desired frequency and the display will be updated with whatever it returns.
- activate()
A method which is called when the UI element needs to start operating. Is blocking, sets up input&output devices, refreshes the UI element, then calls the
idle_loopmethod while the UI element is active.self.in_foregroundis True, while callbacks are executed from the input device thread.
- activate_input()
Sets up the input device if one was passed to the UI element - calling
i.stop_listen,self.configure_inputandi.listen. If an input element wasn’t passed, checks if one is expected.
- after_activate()
Hook for child UI elements, is called each time before activate() returns - before
get_return_valueis called. Is the perfect place to, say, remove input streaming callbacks.
- background_if_inactive()[source]
If the UI element hasn’t been launched yet, launches it in background and waits until it’s fully running. Otherwise, resumes the UI element.
- before_activate()
Hook for child UI elements, is called each time activate() is called. Is the perfect place to clear any flags that you don’t want to persist between multiple activations of a single instance of an UI element.
- before_foreground()
Hook for child UI elements. Is called each time to_foreground is called.
- calculate_intervals()[source]
Calculates the sleep intervals of the refresher, so that no matter the
refresh_interval, the refresher is responsive. Also, sets the counter to zero.
- change_interval()[source]
A helper function to adjust the Refresher’s refresh interval while it’s running
- configure_input()
Configures the input device - you can set your keymap, streaming callbacks or both - abstracting away
stop_listenandlisten. Can be overridden by child elements.
- deactivate()
Deactivates the UI element, exiting it.
- determine_location()
Figures out the place in ZPUI where the UI element is summoned from. Pretty cool!
- generate_keymap()[source]
Returns the default keymap for the UI element. To be implemented by a child object.
- generate_name_if_not_supplied()
Generating a random yet descriptive UI element name if one was not supplied. The name hasa to be random enough so that overlays can be applied properly. The name generated will include the directory where the app is called from.
- get_default_view()
Decides on the view to use for a BaseListUIElement when config file has no information on it.
- get_return_value()
Can be overridden by child UI elements. Return value will be returned when UI element’s
activate()exits.
- get_views_dict()
Is called if you explicitly set up your UI element to accept views. Expected to return a list of all available views.
- idle_loop()[source]
A method that will be called all the time while the UI element is in background (regardless of whether it’s in foreground or not). To be implemented by a child object.
- key_deactivate()
A method to deactivate the menu specifically on key press. Is mostly useful for making key callbacks easily patchable.
- pause()[source]
Pauses the refresher, not allowing it to print anything on the screen while it’s paused.
- print_keymap()
A debug method. Useful for hooking up to an input event so that you can see what’s the keymap of a currently active UI element.
- print_name()
A debug method. Useful for hooking up to an input event so that you can see which UI element is currently active.
- process_callback(func)[source]
Decorates a function so that during its execution the UI element stops being in foreground. Is typically used as a wrapper for a callback from input event processing thread. After callback’s execution is finished, sets the keymap again and refreshes the UI element.
- process_keymap(keymap)
Processes the keymap, wrapping all callbacks using the
process_callbackmethod. If a string is supplied instead of a callable, it looks it up from methods - if a method is not found, raisesValueError. Also, sets KEY_LEFT todeactivateunlessself.override_leftis set toFalse(override with caution).
- refresh()[source]
A placeholder to be used for BaseUIElement.
- resume()[source]
Resumes the refresher after it’s been paused, allowing it to continue printing things on the screen. Refreshes the screen when it’s called.
- set_default_keymap()
Sets the default keymap, getting it straight from the
generate_keymap.
- set_keymap(keymap)[source]
Receives and processes UI element’s keymap (filtered using
process_keymapbefore setting).
- set_refresh_interval(new_interval)[source]
Allows setting Refresher’s refresh intervals after it’s been initialized
- set_views_dict()
Sets the views dict and adds view mixins to views if mixins are available and adding one is appropriate.
- to_background()
Signals
activateto finish executing.
- to_foreground()
A method that is called once in
activateto set all the variables, activates the input device and draw the first image on the display.
- update_keymap(new_keymap)
Updates the UI element’s keymap with a new one (filtered using
process_keymapbefore updating).
- wait_for_active(timeout=100)[source]
If the UI element hasn’t been launched yet, launches it in background and waits until it’s fully running.
- property is_active
A condition to be checked by
activateto see when the UI element is active. Can be overridden by child elements if necessary. By default returnsself.in_background, so if you only have a single UI element without external callback processing, it might make sense to checkin_foregroundinstead.