simpleguics2pygame — frame

simpleguics2pygame module: simpleguics2pygame/frame.

Class Frame.

Piece of SimpleGUICS2Pygame. https://bitbucket.org/OPiMedia/simpleguics2pygame

license:GPLv3 — Copyright (C) 2015-2016, 2020 Olivier Pirson
author:Olivier Pirson — http://www.opimedia.be/
version:November 29, 2020
class SimpleGUICS2Pygame.simpleguics2pygame.frame.Frame(title, canvas_width, canvas_height, control_width=200)[source]

Frame similar to SimpleGUI Frame of CodeSkulptor.

_Frame__deal_event_joypad(event)

Private function that dispatch joypad event.

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:event – Pygame event
Returns:True if some event match, else False
_Frame__deal_event_key(event)

Private function that dispatch key event.

Parameters:event – Pygame event
Returns:True if some event match, else False
_Frame__deal_event_mouse(event)

Private function that dispatch mouse event.

Parameters:event – Pygame event
Returns:True if some event match, else False
__init__(title, canvas_width, canvas_height, control_width=200)[source]

Set the frame.

Don’t use directly, use create_frame().

Parameters:
  • title – str
  • canvas_width – (int or float) >= 0
  • canvas_height – (int or float) >= 0
  • control_width – (int or float) >= 0
__repr__()[source]

Return ‘<Frame object>’.

Returns:str
__weakref__

list of weak references to the object (if defined)

_background_pygame_color

Default background color of frame.

_canvas_border_pygame_color

Border color of canvas.

_controlpanel_background_pygame_color

Background color of control panel.

_cursor_auto_hide = False

When move cursor, if True then hide cursor when on canvas, else show cursor.

_cursor_in_canvas()[source]
Returns:True if the cursor is on canvas, False else.
_display_fps_average = False

If True then display FPS average on the canvas.

_draw_controlpanel()[source]

Draw the control panel and two status boxes.

(Not available in SimpleGUI of CodeSkulptor.)

_draw_statuskey(key=0, pressed=None)[source]

Draw the status box of key.

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:
  • key – int
  • pressed – None or bool
_draw_statusmouse(position=(0, 0), pressed=None)[source]

Draw the status box of mouse.

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:
  • position – (int or float, int or float) or [int or float, int or float]
  • pressed – bool
_fps = 60

Frames per second drawed (frequency of draw and check events)

_frame_instance = None

The only instance of Frame.

_frame_padding = 2

The padding in pixels around the canvas

_get_fps_average()[source]

Return the framerate average (in frame per second) computed by Pygame.

(Not available in SimpleGUI of CodeSkulptor.)

Returns:float
_hide_controlpanel = False

If True then hide control panel (and status box).

_hide_status = False

If True then hide status box.

_keep_timers = None

If None then ask (when stop frame) if it should be stop timers when program ending. (This is the default behavior.)

If True then timers keep running when program ending.

If False then stop all timers when program ending.

_pos_in_control(x, y)[source]

If position (x, y) is on the zone of one Control or TextAreaControl then return it else return None.

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:
  • x – int or float
  • y – int or float
Returns:

None or Control or TextAreaControl

_print_stats_cache = False

If True then print some statistics of caches after frame stopped.

_pygame_mode_depth = 0

Default number of bits used to represent color.

See https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

_pygame_mode_flags = 0

Default options of graphic mode.

See https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

classmethod _pygamecolors_cached_clear()[source]

Empty the cache of Pygame colors used.

Each color used is cached to accelerate drawing. If you use many many different colors maybe use this function to free memory.

(Not available in SimpleGUI of CodeSkulptor.)

Side effect: Empty _colors._PYGAMECOLORS_CACHED.

classmethod _pygamefonts_cached_clear()[source]

Empty the cache of Pygame fonts used.

Each font used with each size is cached to accelerate drawing. If you use many many different sizes maybe use this function to free memory.

(Not available in SimpleGUI of CodeSkulptor.)

Side effect: Empty _fonts.__PYGAMEFONTS_CACHED.

_save_canvas_and_stop(filename, after=1000)[source]

Wait after ms (first wait until the frame is started), then save the canvas in a file and stop the program.

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:
  • filename – str
  • after – int or float >= 0
_save_canvas_request(filename)[source]

Request to save the canvas image in a file.

(The images are saved on each cycle fixed by Frame._fps.)

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:filename – str
_save_canvas_requests = []

List of filenames in which to save canvas image.

_set_canvas_background_image(image)[source]

Set an image to replace the background color of the canvas.

Parameters:image – None or Image
classmethod _set_cursor_visible(visible=True)[source]

If visible is True then show cursor, else hide cursor.

Independently of _cursor_auto_hide value.

Parameters:visible – bool
_set_joypadaxe_handler(joypad_handler)[source]

Set the function handler that will be executed (with the joypad index, the axe index and the value) when axis of joypad move.

(The events are checked on each cycle fixed by Frame._fps.)

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:joypad_handler – function (int >= 0, int >=0, -1 <= float <= 1) -> *
_set_joypaddown_handler(joypad_handler)[source]

Set the function handler that will be executed (with the joypad index and the button index) when a button of joypad is pressed.

(The events are checked on each cycle fixed by Frame._fps.)

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:joypad_handler – function (int >= 0, int >= 0) -> *
_set_joypadhat_handler(joypad_handler)[source]

Set the function handler that will be executed (with the joypad index, the hat index and the values (a, b) where a and b == -1, 0 or 1) when hat of joypad move.

(The events are checked on each cycle fixed by Frame._fps.)

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:joypad_handler – function (int >= 0, int >= 0, (int, int)) -> *
_set_joypadup_handler(joypad_handler)[source]

Set the function handler that will be executed (with the joypad index and the button index) when a button of joypad is released.

(The events are checked on each cycle fixed by Frame._fps.)

(Not available in SimpleGUI of CodeSkulptor.)

Parameters:joypad_handler – function (int >= 0, int >= 0) -> *
_statuskey_background_pygame_color

pygame.Color of background in status key box.

_statuskey_height = 20

Height of the status key box.

_statuskey_pygame_color

pygame.Color of status key box (text and rectangle).

_statuskey_pygame_font

pygame.font.Font of status key box.

_statusmouse_background_pygame_color

pygame.Color of background in status mouse box.

_statusmouse_height = 20

Height of the status mouse box.

_statusmouse_pygame_color

pygame.Color of status mouse box (text and rectangle).

_statusmouse_pygame_font

pygame.font.Font of status mouse box.

add_button(text, button_handler, width=None)[source]

Add a button in the control panel.

When the button are pressed and released, button_handler are executed.

If width is not None then text is possibly cutted.

But, in CodeSkulptor, the accurate appearance is browser dependent. And in SimpleGUICS2Pygame, the accurate appearance is font dependent.

Parameters:
  • text – str
  • button_handler – function () -> *
  • width – None or int
Returns:

Control

add_input(text, input_handler, width)[source]

Add a “label” with an input box in the control panel.

When click with left button of mouse on the “label” or input box, the focus is give to this input box.

When press Tab, the focus is give to the next input box (if exist).

When press Enter, this input box lost the focus and input_handler are executed with the input text.

Parameters:
  • text – str
  • input_handler – function (str) -> *
  • width – int
Returns:

Control

add_label(text, width=None)[source]

Add a label in the control panel.

If width is not None then text is possibly cutted.

But, in CodeSkulptor, the accurate appearance is browser dependent. And in SimpleGUICS2Pygame, the accurate appearance is font dependent.

Parameters:
  • text – str
  • width – None or int
Returns:

Control

download_canvas_image(filename='canvas.png')[source]

Save the content of the canvas in a local file.

In SimpleGUICS2Pygame supported formats are supported formats by Pygame to save: TGA, PNG, JPEG or BMP (see https://www.pygame.org/docs/ref/image.html#pygame.image.save ).

If filename extension is not recognized then TGA format is used.

If filename == ‘’ then a random filename is used, beginning by ‘canvas_’ and with ‘.png’ extension.

In CodeSkulptor the format is always PNG.

(Available in SimpleGUI of CodeSkulptor3 but not in CodeSkulptor 2 and not in CodeSkulptor documentation!)

Parameters:filename – str
get_canvas_image()[source]

NOT YET IMPLEMENTED! (Does nothing.)

(Available in SimpleGUI of CodeSkulptor but not in CodeSkulptor documentation!)

get_canvas_textwidth(text, font_size, font_face='serif')[source]

Return the width needed to draw text by Frame.draw_text().

Parameters:
  • text – str
  • font_size – (int or float) >= 0
  • font_face – str == ‘monospace’, ‘sans-serif’, ‘serif’
Returns:

int or float >= 0

set_canvas_background(color)[source]

Set the background color of the canvas.

Parameters:color – str
set_draw_handler(draw_handler)[source]

Set the function handler that will be executed each cycle fixed by Frame._fps.

Parameters:draw_handler – function (Canvas) -> *
set_keydown_handler(key_handler)[source]

Set the function handler that will be executed (with the key code) when a key is released.

(The events are checked on each cycle fixed by Frame._fps.)

Parameters:key_handler – function (int >= 0) -> *
set_keyup_handler(key_handler)[source]

Set the function handler that will be executed (with the key code) when a key is pressed.

(The events are checked on each cycle fixed by Frame._fps.)

Parameters:key_handler – function (int >= 0) -> *
set_mouseclick_handler(mouse_handler)[source]

Set the function handler that will be executed (with the position of the mouse) when the left button of mouse is released.

(The events are checked on each cycle fixed by Frame._fps.)

Parameters:mouse_handler – function ((int >= 0, int >= 0)) -> *
set_mousedrag_handler(mouse_handler)[source]

Set the function handler that will be executed (with the position of the mouse) for each new mouse position when the left button of mouse is pressed.

(The events are checked on each cycle fixed by Frame._fps.)

Parameters:mouse_handler – function ((int >= 0, int >= 0)) -> *
start()[source]

Start the frame and these handler events.

Warning

With SimpleGUICS2Pygame, Frame.start() is blocking until Frame.stop() execution or closing window. So timers must be started before, and states must be initialized before. (Or maybe after by a handler function.)

(In SimpleGUI of CodeSkulptor this function is not blocking.)

stop()[source]

Stop frame activities.

If (Frame._keep_timers is None) and there is still running timers then ask in the canvas if it should be stop timers when program ending.

(Maybe available in SimpleGUI of CodeSkulptor but not in CodeSkulptor documentation!)

SimpleGUICS2Pygame.simpleguics2pygame.frame.create_frame(title, canvas_width, canvas_height, control_width=200)[source]

Create and return an interactive window.

| +-------+
| | title |
| +---------+--------------+
| | control |              |
| | panel   |    canvas    |
| |         |              |
| +---------+--------------+
title: title of the window.
canvas_width, canvas_height: dimensions of the canvas.
control_width: width of the control panel.

(The frame is inactive until the execution of Frame.start().)

Don’t run twice!

Parameters:
  • title – str
  • canvas_width – (int or float) >= 0
  • canvas_height – (int or float) >= 0
  • control_width – (int or float) >= 0
Returns:

Frame

SimpleGUICS2Pygame.simpleguics2pygame.frame.__all__ = ('Frame', 'create_frame')

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

[source]