Skip to content

GraphicsObject: The Mother of All

Bhavye Mathur edited this page Aug 4, 2020 · 9 revisions

All GraphicsObject function

All functions (except getters) return self which means you can multiple functions on the same line:

from goopylib.objects.imports import *

window = GraphWin("GraphicsObject Example Window", width=500, height=500)

# Multiple functions on the same line
rect = Rectangle(Point(70, 170), Point(300, 300)).glide_x(140, time=1).animate_blinking(0.2).draw(window)
while True:
    window.update_win()

Drawing functions

draw(graphwin=None)

This draws the GraphicsObject onto the graphwin (a GraphWin object) provided. If no argument is provided, the object is drawn on the last GraphWin created.

  • If the GraphWin is closed, this returns itself and does nothing.
  • If the object is already drawn, this undraws the object and draws it again.

When objects are drawn, they will be drawn in front of other objects but objects on higher layers will still remain in front.

The id variable references the Tkinter object inside the GraphWin's objects. To access the Tkinter object, you must use window.items[obj.id] where window is a GraphWin object and obj is a GraphicsObject

undraw(set_blinking=False)

Undraws (hides) the GraphicsObject but does not destroy it. If you want to permanently get rid of an object (advisable because it clears memory), use the destroy() function.

The set_blinking argument (bool) controls whether the object stops its blinking animation when undrawn. This is mainly used internally but can be used in your programs too. If True, the blinking stops.

redraw()

If the object is drawn, this undraws the object then draws it again. If the object is not drawn, this just returns itself. When redrawn, the object doesn't stop its blinking animation (the internal undraw function's set_blinking argument is False)

destroy()  # Internally, this is Tkinter's Canvas.destroy_item() function

Similar to the undraw() function except this is better as this clears the memory and doesn't slow down the program with many objects. An Object can still be drawn to a window after it has been destroyed.

Layering Functions

move_up_layer(layers=1)
move_down_layer(layers=1)

layers - number of layers to move the object int >= 0

By default, every GraphicsObject is on layer 0 (this can be set while creating the object) and these functions move the objects up or down. Objects on higher layers are drawn in front of objects with lower levels.

  • It is recommended that you only use as many layers as are required and not arbitrary numbers like 1000 to improve performance.
  • If while moving the layers down the new layer is less than 0, it sets the object's layer to 0.
  • Unless you have a need to use this, keep all objects on the default layer (0)
set_layer(layer=0)

layer - the layers to set for the object int >= 0

Sets the layer of the object to the one specified.


set_clickable(clickable=True)
set_draggable(draggable_x=True, draggable=None, callback_x=None, callback_y=None)

draggable_x - can the user drag the object in the x-direction? bool
draggable_y - can the user drag the object in the y-direction? bool or None. If none, this is set to the same as draggable_x.

callback_x or y - what function to call when the user drags the object, not recommended. callable or None

This function determines whether the object can be dragged around the window using the mouse. This can be useful in cases where you may want the user to drag and drop objects.

set_draggable_x(draggable=True, callback=None)
set_draggable_y(draggable=True, callback=None) 

draggable - can the user drag the object along the respective axis? bool
callback - what function to call when the user drags the object, not recommended. callable or None

Similar to the function above except specific to only 1 axis.

set_selected(selected=True)
set_fill(colour)
set_outline(colour)
set_outline_width(width)
set_cursor(cursor="arrow")

Obstacle Functions

add_obstacle(obj)
add_obstacle(obstacles, destroy_previous=True)
remove_obstacle(obj)
clear_obstacles()
copy_obstacles_from(other, create_copy=True)
draw_obstacles()
undraw_obstacles()
enable_obstacles()
disable_obstacles()
set_enabled_obstacles(enabled=True)
toggle_obstacles_enabled()

Movement Bounds Functions

set_movement_bounds(obj, allow_looping_x=False, allow_looping_y=False)
set_allow_movement_looping(allow_looping_x=False, allow_looping_y=False)
add_movement_line(obj)

Movement Line Functions

set_movement_lines(lines, destroy_previous=True)
remove_movement_line(obj)
clear_movement_lines()
copy_movement_lines_from(other, create_copy=True)
draw_movement_lines()
undraw_movement_lines()
enable_movement_lines()
disable_movement_lines()
set_enabled_movement_lines()
toggle_movement_lines_enabled()

Movement Functions

move(dx, dy, align="center")
move_y(dy, align="center")
move_x(dx, align="center")
move_to(x, y, align="center")
move_to_y(y, align="center")
move_to_x(x, align="center")
move_to_point(p, align="center")
move_forward(d, collision_callback=None)
move_backward(d, collision_callback=None)
move_left(d, collision_callback=None)
move_right(d, collision_callback=None)
rotate(dr, sampling="bicubic", center=None)
set_rotation(r, sampling="bicubic", center=None)
skew_x(scale, sampling="bicubic", align="center")
skew_y(scale, sampling="bicubic", align="center")
skew_xy(x_scale, y_scale=None, sampling="bicubic", x_align="center", y_align="center")
skew(scale, sampling="bicubic", align="center", skew_x=True, skew_y=True)

Animation Functions

animate_blinking(interval, animate=True, blink_graphic=None, number_of_blinks=None)

All Animation easing arguments default to ease_linear() unless otherwise mentioned
All Animation duplicates_metric arguments default to ("Time", "Initial", "Change")
All Animation allow_duplicate arguments default to True

Gliding Animations

glide(dx, dy, time, easing_x, easing_y=None, allow_duplicate, duplicates_metric)
glide_x(dx, time, easing, allow_duplicate, duplicates_metric)
glide_y(dy, time, easing, allow_duplicate, duplicates_metric)
glide_to(x, y, time, easing_x, easing_y=None, allow_duplicate, duplicates_metric)
glide_to_x(x, time, easing, allow_duplicate, duplicates_metric)
glide_to_y(y, time, easing, allow_duplicate, duplicates_metric)
glide_to_point(p, time, easing_x, easing_y, allow_duplicate, duplicates_metric)

Rotating Animations

animate_rotate(dr, time, easing, allow_duplicate, duplicates_metric)
animate_set_rotation(r, time, easing, allow_duplicate, duplicates_metric)

Colour Animations

animate_change_fill(colour_change, time, easing, allow_duplicate, duplicates_metric)
animate_set_fill(end_colour, time, easing, allow_duplicate, duplicates_metric)
animate_change_outline(colour_outline, time, easing, allow_duplicate, duplicates_metric)
animate_set_outline(end_colour, time, easing, allow_duplicate, duplicates_metric)
animate_change_outline_width(width_change, time, easing, allow_duplicate, duplicates_metric)
animate_set_outline_width(width_change, time, easing, allow_duplicate, duplicates_metric)

Skewing Animations

animate_skew_x(skew_change, time, easing, allow_duplicate, duplicates_metric)
animate_skew_y(skew_change, time, easing, allow_duplicate, duplicates_metric)
animate_skew(skew_change_x, skew_change_y, time, easing_x, easing_y=None, allow_duplicate, duplicates_metric)

Image Specific Animations

animate_change_contrast(contrast_change, time, easing, allow_duplicate, duplicates_metric)
animate_set_contrast(contrast, time, easing, allow_duplicate, duplicates_metric)
animate_change_blur(blur_change, time, easing, allow_duplicate, duplicates_metric)
animate_set_blur(blur, time, easing, allow_duplicate, duplicates_metric)

Getter Functions

get_width()
get_height()
get_size()
get_anchor()
get_x_pos()
get_y_pos()
get_cursor()
get_config_options()
get_fill()
get_outline()
get_outline_width()
get_x_skew()
get_y_skew()
get_skew()
get_rotation()
get_layer()
get_obstacles()
get_movement_looping()
get_movement_bounds()
get_tag()
is_x_looping_allowed(
is_y_looping_allowed()
check_is_gliding()
check_is_rotating()
check_is_animating_fill()
check_is_animating_outline()
check_is_animating_outline_width()
check_is_skewing()
check_is_animating_contrast()
check_is_animating_blur()
check_is_blinking()
check_is_animating()
gliding_time_left()
rotating_time_left()
animating_fill_time_left()
animating_outline_time_left()
animating_outline_width_time_left()
skewing_time_left()
animating_contrast_time_left()
animating_blur_time_left()

Static Functions

get_layer_objects(layer=0)
get_tagged_object(tag)
check_animation_exists(animations, check_animation, duplicates_metric=("Time", "Initial", "Change"))
Clone this wiki locally