campy.gui.events.timer module

Schedule time-delayed functions into the future.

To pause current execution, use the pause() method.

The usual warnings about using time-based functions apply in this module. Namely, the functions here are only as precise as the underlying system time used by the backend.

For more details, see: https://docs.python.org/3/library/time.html

campy.gui.events.timer.pause(milliseconds, step=None, time_fn=<built-in function time>, sleep_fn=<built-in function sleep>)[source]

Pause the current thread while pumping queued events.

This method pauses for at least at long as the requested number of milliseconds.

To pause for one second:

window = GWindow()
oval = GOval(200, 400)
window.add(oval, 50, 50)
pause(1000)
rect = GRect(200, 400)
window.add(rect, 50, 50)

While the execution is paused, queued events are flushed through the backend until the desired time has passed. This rate can be controlled with the step argument, which forces true thread sleep between event queue flushes.

Parameters:
  • milliseconds – The number of milliseconds to pause for.
  • step – The number of milliseconds to step between events.
  • time_fn – The time function to use, measured in seconds.
  • sleep_fn – The sleep function to use, measured in seconds.
campy.gui.events.timer.repeat_every(delay_ms, function=None, fargs=())[source]

Repeat a function periodically and infinitely.

This will schedule a delay between function calls, so if the function itself takes a long time, the gap between function calls may be larger than the supplied argument.

This function can be used either as a decorator or directly. If used as a decorator, the decorated function must be invoked to start the loop. If used directly, the looping function will be started immediately.)

Invoked as a decorator:

@repeat_every(1000)
def update_time_elapsed(label):
    label.text = 'Current Time: {}'.format(time.ctime())

window = GWindow()
label = GLabel()
window.add(label, window.width / 2, window.height / 2)
update_time_elapsed(label)  # Start the infinite loop.

Note that the new function is explicitly invoked to start the loop.

Invoked directly:

def update_time_elapsed(label):
label.text = ‘Current Time: {}’.format(time.ctime())

repeat_every(1000, update_time_elapsed, fargs=(label,)) # No additional invoking.

Parameters:delay_ms – The number of milliseconds to delay between invocations.
campy.gui.events.timer.schedule(function, delay_ms, fargs=())[source]

Schedule a function to execute in the future.

Usage:

window = GWindow()
oval = GOval(200, 400)
window.add(oval, 50, 50)

def draw_box(window):
    rect = GRect(200, 400)
    window.add(rect, 50, 50)

schedule(draw_box, 1000, fargs=(window, ))

This instructs the backend to execute a callable after some time has passed. :param function: The callable to schedule. :param delay_ms: The number of milliseconds in the future to schedule. :param fargs: Additional arguments to be passed to the function.