Codeskulptor simplegui API

The functions below implement the basic methods used to interact with simplegui and create objects: frames, timers, images and sounds.

Create a Frame

simplequi.create_frame(title, canvas_width, canvas_height, control_width=None)

Creates a new frame for interactive programs.

The frame’s window has the given title, a string. The frame consists of two parts: a control panel on the left and a canvas on the right. The control panel’s width in pixels can be specified by the number control_width. The canvas width in pixels is the number canvas_width. The height in pixels of both the control panel and canvas is the number canvas_height.

Parameters
  • title (str) – the title of the frame window

  • canvas_width (int) – the width of the drawing area, in pixels

  • canvas_height (int) – the height of the drawing area, in pixels

  • control_width (Optional[int]) – the width of the control area of the frame, in pixels

Return type

Frame

Returns

a Frame that can be used to setup (most of) the rest of the program

Create a Timer

simplequi.create_timer(interval, timer_handler)

Creates a timer.

Once started, it will repeatedly call the given event handler at the specified interval, which is given in ms. The handler should be defined with no arguments.

Parameters
  • interval (int) – the interval at which to call the timer handler, in milliseconds

  • timer_handler (Callable[[], None]) – a function to call after each interval

Return type

Timer

Returns

a Timer that calls timer_handler every interval ms (once started)

Load an Image from a URL

simplequi.load_image(url)

Loads an image from the specified URL.

The image can be in any format supported by PySide2. No error is raised if the file isn’t found or is of an unsupported format.

Parameters

url (str) – the URL of the image to load

Return type

Image

Returns

an Image that can be passed to draw_image()

Load a Sound from a URL

simplequi.load_sound(url)

Loads a sound from the specified URL.

Supports whatever audio formats that PySide2 supports. No error is raised if the file isn’t found or is of an unsupported format.

Parameters

url (str) – the URL of the sound to load

Return type

Sound

Returns

a Sound that can be played, paused etc.

Lookup the Value for a Key Press

class simplequi._keys.KeyMap

The keyboard event handlers receive the relevant key as an integer.

Because different browsers can give different values for the same keystrokes, simplequi provides a way to get the appropriate key integer for a given meaning. The acceptable strings for character are the letters ‘a’…’z’ and A’…’Z’, the digits ‘0’…‘9’, ‘space’, ‘left’, ‘right’, ‘up’, and ‘down’. Note that other keyboard symbols are not defined in simplequi.KEY_MAP.

__getitem__(key)

x.__getitem__(‘y’) <==> x[‘y’]

Get the value that is sent to a handler for a given key press.

This is usually called using square brackets, like value = simplequi.KEY_MAP['space']

Parameters

key (str) – the key press to look up

Return type

int

Returns

the integer value for the key

Raises

KeyError – if the key is not in the map for simplegui

Example usage:

def key_handler(self, key):
    if key == simplequi.KEY_MAP['left']:
        print('Left key pressed!')
        self.move_ship_left()