simpleguics2pygame — frame

simpleguics2pygame/frame (April 29, 2016)

Class Frame.

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

GPLv3 — Copyright (C) 2015, 2016 Olivier Pirson http://www.opimedia.be/

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

Frame similar to SimpleGUI Frame of CodeSkulptor.

__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
_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
_get_fps_average()[source]

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

(Not available in SimpleGUI of CodeSkulptor.)

Returns:float
_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

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 Frame._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 Frame._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
_set_canvas_background_image(image)[source]

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

Parameters:image – None or Image
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

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 they must be stopped.

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

__weakref__

list of weak references to the object (if defined)

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']

list() -> new empty list list(iterable) -> new list initialized from iterable’s items

[source]