Module DrawingPanel :: Class DrawingPanel

Class DrawingPanel

A DrawingPanel object represents a simple interface for creating graphical windows in Python for drawing shapes, lines, images, and colors. The DrawingPanel also supports getting and setting the RGB values of individual pixels, which makes it useful for learning about various looping and 2D list- processing algorithms.

See PyDoc comments for individual functions below for more information.

This library is based on Python's Tkinter GUI system. In particular, the drawing surface used by DrawingPanel is a Tkinter Canvas object, and many of our functions are wrappers around Tkinter Canvas functions.

The DrawingPanel supports properties like color, background, width, height, etc. that can be accessed/modified either using traditional methods like get_color and set_background, or as properties named color or background.


Authors:
Marty Stepp, Stanford University, Allison Obourn, University of Arizona, Stuart Reges, University of Washington

Version: 2018/02/28

Copyright: Marty Stepp, Allison Obourn, Stuart Reges; for individual, educational, non-commercial, private use only; all other rights reserved.

See Also: http://effbot.org/tkinterbook/canvas.htm

Instance Methods
 
__init__(self, width=500, height=500, background='white', **options)
Constructs a panel of a given width, height, and optional background color.
 
about(self)
Shows an About message.
 
add_key_listener(self, function, event_type='press')
Attaches the given function to be called when keyboard events occur in this DrawingPanel window.
 
add_mouse_listener(self, function, event_type='click')
Attaches the given function to be called when mouse events occur in this DrawingPanel window.
 
center(self)
Centers the DrawingPanel window's location on the screen.
 
clear(self)
Erases all shapes from the panel and fills it with its background color.
 
close(self)
Closes the DrawingPanel window so it is no longer visible.
 
draw_arc(self, x, y, w, h, start_angle, extent, color='', **options)
Draws an outlined arc (a partial oval).
 
draw_image(self, filename, x, y, w=0, h=0, **options)
Draws an image from a file with its top-left corner at the given (x, y) pixel.
 
draw_line(self, x1, y1, x2, y2, color='', **options)
Draws a line between the two given points (x1, y1) and (x2, y2).
 
draw_oval(self, x, y, w, h, color='', **options)
Draws an oval the given size with its top-left corner at the given (x, y) pixel.
 
draw_pixel(self, x, y, color='', **options)
Changes the color at the given (x, y) pixel.
 
draw_polyline(self, *coords, **options)
Draws a group of lines between the given coordinates passed as individual x/y coordinate parameters or (x,y) tuples.
 
draw_polygon(self, *coords, **options)
Draws an outlined polygon between the given coordinates passed as individual x/y coordinate parameters or (x,y) tuples.
 
draw_rect(self, x, y, w, h, color='', **options)
Draws a rectangle of the given size with its top-left corner at the given (x, y) pixel.
 
draw_rectangle(self, x, y, w, h, color='', **options)
Draws a rectangle of the given size with its top-left corner at the given (x, y) pixel.
 
draw_string(self, text, x, y, color='', font='', **options)
Draws a text string with its top-left corner at the given (x, y) pixel.
 
fill_arc(self, x, y, w, h, start_angle, extent, color='', **options)
Draws a filled arc (a partial oval).
 
fill_oval(self, x, y, w, h, color='', **options)
Draws a filled oval of the given size with its top-left corner at the given (x, y) pixel.
 
fill_polygon(self, *coords, **options)
Draws a filled polygon between the given coordinates passed as individual x/y coordinate parameters or (x,y) tuples.
 
fill_rect(self, x, y, w, h, color='', **options)
Draws a filled rectangle of the given size with its top-left corner at the given (x, y) pixel.
 
fill_rectangle(self, x, y, w, h, fill='', color='', **options)
Draws a filled rectangle of the given size with its top-left corner at the given (x, y) pixel.
str
get_background(self)
Returns the panel's background color.
tuple
get_background_rgb(self)
Returns the panel's background color as an RGB tuple.
str
get_color(self)
Returns the default color used to outline drawn shapes.
str
get_fill_color(self)
Returns the default color used to fill in shapes.
str
get_font(self)
Returns the font used for drawn strings.
str
get_foreground(self)
Returns the default color used to outline drawn shapes.
int
get_height(self)
Returns the height of the panel's canvas area.
tuple
get_location(self)
Returns a 2-element tuple containing the panel's window's top-left x/y corner.
str
get_outline_color(self)
Returns the default color used to outline drawn shapes.
str
get_pixel_color(self, x, y)
Returns the RGB color of the given (x, y) pixel on the canvas as a string such as "#ff00ff".
str
get_pixel_color_rgb(self, x, y)
Returns the RGB color of the given (x, y) pixel on the canvas as a 3-tuple of three integers from 0-255 representing the Red, Green, and Blue components.
tuple
get_size(self)
Returns a 2-element tuple of the width and height of the panel's canvas area.
int
get_stroke(self)
Returns the width that will be used to draw lines on future shapes.
str
get_title(self)
Returns the window title bar text.
int
get_stroke_width(self)
Returns the width that will be used to draw lines on future shapes.
int
get_width(self)
Returns the width of the panel's canvas area.
Tk
get_window(self)
Returns the underlying Tkinter window being displayed on the screen.
tuple
get_window_size(self)
Returns a 2-element tuple of the width and height of the panel's entire window including borders and status bar.
int
get_x(self)
Returns the panel's window's left x pixel position.
int
get_y(self)
Returns the panel's window's top y pixel position.
 
hide(self)
Sets the DrawingPanel window to be not visible on the screen.
bool
is_resizable(self)
Returns whether the DrawingPanel window is able to be resized; initially True.
bool
is_visible(self)
Returns whether the DrawingPanel window is visible on the screen.
 
message(self, text, title='Message')
Pops up a message box displaying the given text.
 
message_error(self, text, title='Error')
Pops up an error message box displaying the given text.
 
message_yes_no(self, text, title='Question')
Pops up a question message box displaying the given text with Yes/No buttons.
 
save(self, filename)
Saves the drawing panel to the given output file.
 
save_as_dialog(self)
Pops up a file choosing dialog to allow the user to save the drawing panel's canvas output to a file.
 
set_background(self, *args)
Sets the panel's background color to be the given color.
 
set_color(self, *args)
Sets the color used to outline future drawn shapes.
 
set_fill_color(self, *args)
Sets the color used to fill in future drawn shapes.
 
set_font(self, font)
Sets the font used for future drawn strings.
 
set_foreground(self, *args)
Sets the color used to outline and fill future drawn shapes.
 
set_height(self, h)
Sets the panel's canvas height to the given value.
 
set_location(self, x, y)
Sets the window's location such that its top-left corner is at the given x/y pixel position.
 
set_outline_color(self, *args)
Sets the color used to outline future drawn shapes.
 
set_resizable(self, resizable)
Sets whether the DrawingPanel window is able to be resized; initially True.
 
set_size(self, width, height)
Sets the window's size such that its canvas will be the given width and height.
 
set_status(self, text)
Sets the text to show in the window's southern status bar area.
 
set_stroke(self, width)
Sets the width that will be used to draw lines on future shapes.
 
set_stroke_width(self, width)
Sets the width that will be used to draw lines on future shapes.
 
set_title(self, text)
Sets the title to display at the top of the window.
 
set_visible(self, value=True)
Sets whether the DrawingPanel window should be visible on the screen.
 
set_width(self, width)
Sets the panel's canvas width to the given value.
 
set_x(self, x)
Sets the panel's window's left x pixel position to the given value.
 
set_y(self, y)
Sets the panel's window's top y pixel position to the given value.
 
show(self)
Sets the DrawingPanel window to be visible on the screen.
 
sleep(self, ms)
Causes the DrawingPanel to pause for the given number of milliseconds.
Properties
tuple background_rgb
The panel's background color as an RGB tuple such as (255, 0, 192).
Tk window
The underlying Tkinter window being displayed on the screen.
tuple window_size
A 2-element tuple of the width and height of the panel's entire window including borders and status bar.
str background
The panel's background color.
str color
The default color used to outline drawn shapes.
str fill_color
The default color used to fill in shapes.
str font
The font used for drawn strings.
str foreground
The default color used to outline drawn shapes.
int height
The height of the panel's canvas area.
tuple location
A 2-element tuple containing the panel's window's top-left x/y corner.
str outline_color
The default color used to outline drawn shapes.
bool resizable
Whether the DrawingPanel window is able to be resized; initially True.
tuple size
A 2-element tuple of the width and height of the panel's canvas area.
int stroke
The width that will be used to draw lines on future shapes.
int stroke_width
The width that will be used to draw lines on future shapes.
str title
The window title bar text.
bool visible
Whether the DrawingPanel window is visible on the screen.
int width
The width of the panel's canvas area.
int x
The panel's window's left x pixel position.
int y
The panel's window's top y pixel position.
Method Details

__init__(self, width=500, height=500, background='white', **options)
(Constructor)

 

Constructs a panel of a given width, height, and optional background color.

Parameters:

Parameters:
  • width - (optional) width of the DrawingPanel central canvas area in pixels (default 500)
  • height - (optional) height of the DrawingPanel central canvas area in pixels (default 500)
  • background - (optional) background color of the DrawingPanel canvas area (default "white")
  • options - (optional) keyword arguments to pass directly to Tk window constructor (see Tkinter documentation)

add_key_listener(self, function, event_type='press')

 

Attaches the given function to be called when keyboard events occur in this DrawingPanel window.

Parameters:
  • function - The function to be called; should accept an event as a parameter.
  • event_type - (optional) What type of mouse event to respond to. The default is key presses, but you can pass any of "press" or "release".

See Also: http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

add_mouse_listener(self, function, event_type='click')

 

Attaches the given function to be called when mouse events occur in this DrawingPanel window.

Parameters:
  • function - The function to be called; should accept an event as a parameter.
  • event_type - (optional) What type of mouse event to respond to. The default is clicks, but you can pass any of "click", "doubleclick", "drag", "enter", "exit", "middleclick", "move", "press", "release", "rightclick", or "wheel".

See Also: http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

center(self)

 

Centers the DrawingPanel window's location on the screen. If the DrawingPanel's size is larger than the screen size in either dimension, does not move it.

close(self)

 

Closes the DrawingPanel window so it is no longer visible. Once it is closed, it cannot be drawn on or shown on screen again during the current run of your program.

draw_arc(self, x, y, w, h, start_angle, extent, color='', **options)

 

Draws an outlined arc (a partial oval). The resulting arc begins at 'start_angle' and extends for 'extent' degrees. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation.

If you want to draw a pie-slice style arc rather than just the outer line, pass an option of style="pieslice".

Parameters:
  • x - left x coordinate of bounding box of arc's oval
  • y - top y coordinate of bounding box of arc's oval
  • w - width of bounding box of arc's oval
  • h - height of bounding box of arc's oval
  • start_angle - starting angle in degrees, where 0 is at "3 o'clock" position
  • extent - degrees that the arc should extend from its start, where positive numbers are counter-clockwise and negative values are clockwise
  • color - (optional) color to use for outline of shape (if absent, uses panel's default color as set via set_color)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_arc-method

draw_image(self, filename, x, y, w=0, h=0, **options)

 

Draws an image from a file with its top-left corner at the given (x, y) pixel. Due to tkinter limitations, can recognize PNG and GIF but not JPG. Sorry. :-(

Parameters:
  • filename (str) - path to image file
  • x - top-left x-coordinate at which to place the image
  • y - top-left y-coordinate at which to place the image
  • w - (optional) width at which to draw the image (not yet supported)
  • h - (optional) height at which to draw the image (not yet supported)
  • options - (optional) keyword arguments to pass directly to Tk create_image function (see Tkinter documentation)

To Do: support resizing of images (does not work yet)

See Also: http://effbot.org/tkinterbook/canvas.htm#canvas.Canvas.create_image-method

draw_line(self, x1, y1, x2, y2, color='', **options)

 

Draws a line between the two given points (x1, y1) and (x2, y2).

Parameters:
  • x1 - starting point's x-coordinate
  • y1 - starting point's y-coordinate
  • x2 - ending point's x-coordinate
  • y2 - ending point's y-coordinate
  • color - (optional) color with which to draw line (if absent, uses panel's default color as set via set_color)
  • options - (optional) keyword arguments to pass directly to Tk create_line function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_line-method

draw_oval(self, x, y, w, h, color='', **options)

 

Draws an oval the given size with its top-left corner at the given (x, y) pixel. You can pass the outline color as a last positional parameter, or it can be a keyword parameter (in options), or if none is passed at all, we will fall back to the panel's default outline color. The polygon will not be filled unless a 'fill' named parameter is passed.

Parameters:
  • x - left x coordinate of bounding box of oval
  • y - top y coordinate of bounding box of oval
  • w - width of bounding box of oval
  • h - height of bounding box of oval
  • color - (optional) color to use for outline of shape (if absent, uses panel's default color as set via set_color)
  • options - (optional) keyword arguments to pass directly to Tk create_oval function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_oval-method

draw_pixel(self, x, y, color='', **options)

 

Changes the color at the given (x, y) pixel. Equivalent to drawing a 1x1 rectangle.

Parameters:
  • x - x coordinate
  • y - y coordinate
  • color - (optional) color to use for pixel (if absent, uses panel's default color as set via set_color)
  • options - (optional) keyword arguments to pass directly to Tk create_rectangle function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_rectangle-method

draw_polyline(self, *coords, **options)

 

Draws a group of lines between the given coordinates passed as individual x/y coordinate parameters or (x,y) tuples.

You can pass the outline color as a last positional parameter, or it can be a keyword parameter (in options), or if none is passed at all, we will fall back to the panel's default outline color. The difference between draw_polyline and draw_polygon is that the polygon will connect its last line point back to the start point; the polygon can also be filled, while a polyline cannot.

example: panel.draw_polyline(x1, y1, x2, y2, x3, y3) example: panel.draw_polyline(x1, y1, x2, y2, x3, y3, x4, y4, "red") example: panel.draw_polyline(p1, p2, p3)

Parameters:
  • coords - x, y coordinate pairs of arbitrary number
  • options - (optional) keyword arguments to pass directly to Tk create_line function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_line-method

draw_polygon(self, *coords, **options)

 

Draws an outlined polygon between the given coordinates passed as individual x/y coordinate parameters or (x,y) tuples. You can pass the outline color as a last positional parameter, or it can be a keyword parameter (in options), or if none is passed at all, we will fall back to the panel's default outline color. The polygon will not be filled unless a 'fill' named parameter is passed.

example: panel.draw_polygon(x1, y1, x2, y2, x3, y3) example: panel.draw_polygon(x1, y1, x2, y2, x3, y3, x4, y4, "red") example: panel.draw_polygon(p1, p2, p3)

Parameters:
  • coords - x, y coordinate pairs of arbitrary number
  • options - (optional) keyword arguments to pass directly to Tk create_polygon and/or create_line function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_polygon-method

draw_rect(self, x, y, w, h, color='', **options)

 

Draws a rectangle of the given size with its top-left corner at the given (x, y) pixel. Equivalent to draw_rectangle.

Parameters:
  • x - left x coordinate of bounding box of rectangle
  • y - top y coordinate of bounding box of rectangle
  • w - width of bounding box of rectangle
  • h - height of bounding box of rectangle
  • color - (optional) color to use for outline of shape (if absent, uses panel's default color as set via set_color)
  • options - (optional) keyword arguments to pass directly to Tk create_rectangle function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_rectangle-method

draw_rectangle(self, x, y, w, h, color='', **options)

 

Draws a rectangle of the given size with its top-left corner at the given (x, y) pixel. Equivalent to draw_rect.

Parameters:
  • x - left x coordinate of bounding box of rectangle
  • y - top y coordinate of bounding box of rectangle
  • w - width of bounding box of rectangle
  • h - height of bounding box of rectangle
  • color - (optional) color to use for outline of shape (if absent, uses panel's default color as set via set_color)
  • options - (optional) keyword arguments to pass directly to Tk create_rectangle function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_rectangle-method

draw_string(self, text, x, y, color='', font='', **options)

 

Draws a text string with its top-left corner at the given (x, y) pixel.

Parameters:
  • x - left x coordinate of start of text
  • y - top y coordinate of start of text
  • color - (optional) color to use for outline of shape (if absent, uses panel's default color as set via set_color)
  • font - (optional) font face to use to render text, in "family size style" format, such as "Times New Roman 16 bold italic"
  • options - (optional) keyword arguments to pass directly to Tk create_text function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_text-method

fill_arc(self, x, y, w, h, start_angle, extent, color='', **options)

 

Draws a filled arc (a partial oval). The resulting arc begins at 'start_angle' and extends for 'extent' degrees. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation. You can pass the outline color as a named 'outline' parameter, else the fill color will be used as the outline color.

Parameters:
  • x - left x coordinate of bounding box of arc's oval
  • y - top y coordinate of bounding box of arc's oval
  • w - width of bounding box of arc's oval
  • h - height of bounding box of arc's oval
  • start_angle - starting angle in degrees, where 0 is at "3 o'clock" position
  • extent - degrees that the arc should extend from its start, where positive numbers are counter-clockwise and negative values are clockwise
  • color - (optional) color to use for fill of shape (if absent, uses panel's default fill color as set via set_fill_color)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_arc-method

fill_oval(self, x, y, w, h, color='', **options)

 

Draws a filled oval of the given size with its top-left corner at the given (x, y) pixel. You can pass the fill color as a fifth positional parameter, or it can be a keyword parameter (in options), or if none is passed at all, we will fall back to the panel's default fill color. You can pass the outline color as a named 'outline' parameter, else the fill color will be used as the outline color.

Parameters:
  • x - left x coordinate of bounding box of oval
  • y - top y coordinate of bounding box of oval
  • w - width of bounding box of oval
  • h - height of bounding box of oval
  • color - (optional) color to use for fill of shape (if absent, uses panel's default color as set via set_fill_color)
  • options - (optional) keyword arguments to pass directly to Tk create_oval function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_oval-method

fill_polygon(self, *coords, **options)

 

Draws a filled polygon between the given coordinates passed as individual x/y coordinate parameters or (x,y) tuples. You can pass the 'fill' as a last positional parameter, or it can be a keyword parameter (in options), or if none is passed at all, we will fall back to the panel's default fill color. You can pass the outline color as a named 'outline' parameter, else the fill color will be used as the outline color.

example: panel.fill_polygon(x1, y1, x2, y2, x3, y3) example: panel.fill_polygon(x1, y1, x2, y2, x3, y3, x4, y4, "red") example: panel.fill_polygon(p1, p2, p3)

Parameters:
  • coords - x, y coordinate pairs of arbitrary number
  • options - (optional) keyword arguments to pass directly to Tk create_polygon function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_polygon-method

fill_rect(self, x, y, w, h, color='', **options)

 

Draws a filled rectangle of the given size with its top-left corner at the given (x, y) pixel. You can pass the fill color as a fifth positional parameter, or it can be a keyword parameter (in options), or if none is passed at all, we will fall back to the panel's default fill color. You can pass the outline color as a named 'outline' parameter, else the fill color will be used as the outline color. Equivalent to fill_rectangle.

Parameters:
  • x - left x coordinate of bounding box of rectangle
  • y - top y coordinate of bounding box of rectangle
  • w - width of bounding box of rectangle
  • h - height of bounding box of rectangle
  • color - (optional) color to use to fill shape (if absent, uses panel's default color as set via set_fill_color)
  • options - (optional) keyword arguments to pass directly to Tk create_rectangle function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_rectangle-method

fill_rectangle(self, x, y, w, h, fill='', color='', **options)

 

Draws a filled rectangle of the given size with its top-left corner at the given (x, y) pixel. Equivalent to fill_rect.

Parameters:
  • x - left x coordinate of bounding box of rectangle
  • y - top y coordinate of bounding box of rectangle
  • w - width of bounding box of rectangle
  • h - height of bounding box of rectangle
  • color - (optional) color to use to fill shape (if absent, uses panel's default color as set via set_fill_color)
  • options - (optional) keyword arguments to pass directly to Tk create_rectangle function (see Tkinter documentation)

See Also: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_rectangle-method

get_background(self)

 

Returns the panel's background color. Default is white.

Returns: str
color as hex string such as "#ff00cc"

get_background_rgb(self)

 

Returns the panel's background color as an RGB tuple. Default is white.

Returns: tuple
color as a 3-element rgb tuple such as (255, 0, 192)

get_color(self)

 

Returns the default color used to outline drawn shapes. This color will be used unless you explicitly pass a color to the drawing function to draw a particular shape. Equivalent to get_foreground.

Returns: str

get_fill_color(self)

 

Returns the default color used to fill in shapes. This color will be used unless you explicitly pass a color to the filling function to draw a particular shape.

Returns: str

get_font(self)

 

Returns the font used for drawn strings. Pass a string in "FONTNAME SIZE STYLE" format, with the last two tokens optional, such as "Helvetica" or "Consolas 14" or "Times New Roman 16 bold".

Returns: str
font as a "family size style" string such as "Times New Roman 16 bold italic"

See Also: http://effbot.org/tkinterbook/tkinter-widget-styling.htm

get_foreground(self)

 

Returns the default color used to outline drawn shapes. This color will be used unless you explicitly pass a color to the drawing function to draw a particular shape. Equivalent to get_color.

Returns: str

get_height(self)

 

Returns the height of the panel's canvas area.

Returns: int
height in pixels as an int

get_outline_color(self)

 

Returns the default color used to outline drawn shapes. This color will be used unless you explicitly pass a color to the drawing function to draw a particular shape. Equivalent to get_color.

Returns: str

get_pixel_color(self, x, y)

 

Returns the RGB color of the given (x, y) pixel on the canvas as a string such as "#ff00ff". NOTE: This function currently fails when there are text strings drawn onto the canvas. It interprets every pixel in the bounding box of the text string to be the text string's color.

Parameters:
  • x - x coordinate of pixel
  • y - y coordinate of pixel
Returns: str

get_pixel_color_rgb(self, x, y)

 

Returns the RGB color of the given (x, y) pixel on the canvas as a 3-tuple of three integers from 0-255 representing the Red, Green, and Blue components.

Parameters:
  • x - x coordinate of pixel
  • y - y coordinate of pixel
Returns: str

get_size(self)

 

Returns a 2-element tuple of the width and height of the panel's canvas area.

Returns: tuple
2-element tuple such as (400, 300)

get_stroke(self)

 

Returns the width that will be used to draw lines on future shapes. Equivalent to get_stroke_width.

Returns: int
stroke width (default 1)

get_title(self)

 

Returns the window title bar text.

Returns: str
window title bar text

get_stroke_width(self)

 

Returns the width that will be used to draw lines on future shapes. Equivalent to get_stroke.

Returns: int
stroke width (default 1)

get_window_size(self)

 

Returns a 2-element tuple of the width and height of the panel's entire window including borders and status bar.

Returns: tuple
2-element tuple such as (410, 318)

is_resizable(self)

 

Returns whether the DrawingPanel window is able to be resized; initially True.

Returns: bool
True if the DrawingPanel window is able to be resized, otherwise False

is_visible(self)

 

Returns whether the DrawingPanel window is visible on the screen. If the DrawingPanel has been closed or hidden, returns False.

Returns: bool
True if the DrawingPanel window is visible on the screen, otherwise False

message_yes_no(self, text, title='Question')

 

Pops up a question message box displaying the given text with Yes/No buttons. If user presses Yes, returns True; if No, returns False.

save(self, filename)

 

Saves the drawing panel to the given output file. Formats supported: .ppm, .ps NOTE: This function currently fails when there are text strings drawn onto the canvas. It interprets every pixel in the bounding box of the text string to be the text string's color.

Parameters:
  • filename - path to file to save into (must be .ps or .ppm format)

To Do: use PIL library to save if lib is present

set_background(self, *args)

 

Sets the panel's background color to be the given color. color -- the color to set, as a string such as "yellow" or "black"

Parameters:
  • args - either a color string such as "yellow" or "#ff00cc", or a 3-element RGB tuple such as (255, 0, 192)

set_color(self, *args)

 

Sets the color used to outline future drawn shapes. The color passed can be a color string like "purple" or a hex string like "#ff00ff" or an RGB tuple like (255, 0, 255).

Parameters:
  • args - either a color string such as "yellow" or "#ff00cc", or a 3-element RGB tuple such as (255, 0, 192)

See Also: http://wiki.tcl.tk/16166

set_fill_color(self, *args)

 

Sets the color used to fill in future drawn shapes. The color passed can be a color string like "purple" or a hex string like "#ff00ff" or an RGB tuple like (255, 0, 255).

Parameters:
  • args - either a color string such as "yellow" or "#ff00cc", or a 3-element RGB tuple such as (255, 0, 192)

set_font(self, font)

 

Sets the font used for future drawn strings. Pass a string in "FONTNAME SIZE STYLE" format, with the last two tokens optional, such as "Helvetica" or "Consolas 14" or "Times New Roman 16 bold".

Parameters:
  • font - a font string in "FONTNAME SIZE STYLE" format, with the last two tokens optional, such as "Helvetica" or "Consolas 14" or "Times New Roman 16 bold".

See Also: http://effbot.org/tkinterbook/tkinter-widget-styling.htm

set_foreground(self, *args)

 

Sets the color used to outline and fill future drawn shapes. The color passed can be a color string like "purple" or a hex string like "#ff00ff" or an RGB tuple like (255, 0, 255). Equivalent to set_color.

Parameters:
  • args - either a color string such as "yellow" or "#ff00cc", or a 3-element RGB tuple such as (255, 0, 192)

set_height(self, h)

 

Sets the panel's canvas height to the given value.

Parameters:
  • h - desired height of window in pixels

set_location(self, x, y)

 

Sets the window's location such that its top-left corner is at the given x/y pixel position.

Parameters:
  • x - desired x coordinate of top-left corner of window
  • y - desired y coordinate of top-left corner of window

set_outline_color(self, *args)

 

Sets the color used to outline future drawn shapes. Equivalent to set_color.

Parameters:
  • args - either a color string such as "yellow" or "#ff00cc", or a 3-element RGB tuple such as (255, 0, 192)

set_resizable(self, resizable)

 

Sets whether the DrawingPanel window is able to be resized; initially True.

Parameters:
  • resizable - True if window should be resizable; False if not

set_size(self, width, height)

 

Sets the window's size such that its canvas will be the given width and height.

Parameters:
  • width - desired canvas width in pixels
  • height - desired canvas height in pixels

set_stroke(self, width)

 

Sets the width that will be used to draw lines on future shapes. Equivalent to set_stroke_width.

set_stroke_width(self, width)

 

Sets the width that will be used to draw lines on future shapes. Equivalent to set_stroke.

set_x(self, x)

 

Sets the panel's window's left x pixel position to the given value.

Parameters:
  • x - desired x position of top-left corner of window

set_y(self, y)

 

Sets the panel's window's top y pixel position to the given value.

Parameters:
  • y - desired y position of top-left corner of window

sleep(self, ms)

 

Causes the DrawingPanel to pause for the given number of milliseconds. Useful for creating simple animations.

Parameters:
  • ms - number of milliseconds to pause

Property Details

background_rgb

The panel's background color as an RGB tuple such as (255, 0, 192). Default is white.

Get Method:
unreachable.background_rgb(self) - The panel's background color as an RGB tuple such as (255, 0, 192).
Type:
tuple

window

The underlying Tkinter window being displayed on the screen.

Get Method:
unreachable.window(self) - The underlying Tkinter window being displayed on the screen.
Type:
Tk

window_size

A 2-element tuple of the width and height of the panel's entire window including borders and status bar.

Get Method:
unreachable.window_size(self) - A 2-element tuple of the width and height of the panel's entire window including borders and status bar.
Type:
tuple

background

The panel's background color. Default is white.

Get Method:
unreachable.background(self) - The panel's background color.
Set Method:
unreachable.background(self, value) - Sets the panel's background color to be the given color, as a string such as "yellow" or "black" or "yellow" or "#ff00cc"
Type:
str

color

The default color used to outline drawn shapes. This color will be used unless you explicitly pass a color to the drawing function to draw a particular shape. Equivalent to foreground.

Get Method:
unreachable.color(self) - The default color used to outline drawn shapes.
Set Method:
unreachable.color(self, value) - Sets the color used to outline future drawn shapes.
Type:
str

fill_color

The default color used to fill in shapes. This color will be used unless you explicitly pass a color to the filling function to draw a particular shape.

Get Method:
unreachable.fill_color(self) - The default color used to fill in shapes.
Set Method:
unreachable.fill_color(self, value) - Sets the color used to fill in future drawn shapes.
Type:
str

font

The font used for drawn strings. Pass a string in "FONTNAME SIZE STYLE" format, with the last two tokens optional, such as "Helvetica" or "Consolas 14" or "Times New Roman 16 bold".

Get Method:
unreachable.font(self) - The font used for drawn strings.
Set Method:
unreachable.font(self, value) - Sets the font used for future drawn strings.
Type:
str

See Also: http://effbot.org/tkinterbook/tkinter-widget-styling.htm

foreground

The default color used to outline drawn shapes. This color will be used unless you explicitly pass a color to the drawing function to draw a particular shape. Equivalent to color.

Get Method:
unreachable.foreground(self) - The default color used to outline drawn shapes.
Set Method:
unreachable.foreground(self, value) - Sets the color used to outline and fill future drawn shapes.
Type:
str

height

The height of the panel's canvas area.

Get Method:
unreachable.height(self) - The height of the panel's canvas area.
Set Method:
unreachable.height(self, h) - Sets the panel's canvas height to the given value.
Type:
int

location

A 2-element tuple containing the panel's window's top-left x/y corner.

Get Method:
unreachable.location(self) - A 2-element tuple containing the panel's window's top-left x/y corner.
Set Method:
unreachable.location(self, xy) - Sets the window's location such that its top-left corner is at the given x/y pixel position.
Type:
tuple

outline_color

The default color used to outline drawn shapes. This color will be used unless you explicitly pass a color to the drawing function to draw a particular shape. Equivalent to color.

Get Method:
unreachable.outline_color(self) - The default color used to outline drawn shapes.
Set Method:
unreachable.outline_color(self, value) - Sets the color used to outline future drawn shapes.
Type:
str

resizable

Whether the DrawingPanel window is able to be resized; initially True.

Get Method:
unreachable.resizable(self) - Whether the DrawingPanel window is able to be resized; initially True.
Set Method:
unreachable.resizable(self, value) - Sets whether the DrawingPanel window is able to be resized; initially True.
Type:
bool

size

A 2-element tuple of the width and height of the panel's canvas area.

Get Method:
unreachable.size(self) - A 2-element tuple of the width and height of the panel's canvas area.
Set Method:
unreachable.size(self, wh) - Sets the window's size such that its canvas will be the given width and height in pixels.
Type:
tuple

stroke

The width that will be used to draw lines on future shapes. Equivalent to stroke_width.

Get Method:
unreachable.stroke(self) - The width that will be used to draw lines on future shapes.
Set Method:
unreachable.stroke(self, width) - Sets the width that will be used to draw lines on future shapes.
Type:
int

stroke_width

The width that will be used to draw lines on future shapes. Equivalent to stroke.

Get Method:
unreachable.stroke_width(self) - The width that will be used to draw lines on future shapes.
Set Method:
unreachable.stroke_width(self, width) - Sets the width that will be used to draw lines on future shapes.
Type:
int

title

The window title bar text.

Get Method:
unreachable.title(self) - The window title bar text.
Set Method:
unreachable.title(self, text) - Sets the title to display at the top of the window.
Type:
str

visible

Whether the DrawingPanel window is visible on the screen. If the DrawingPanel has been closed or hidden, returns False.

Get Method:
unreachable.visible(self) - Whether the DrawingPanel window is visible on the screen.
Set Method:
unreachable.visible(self, value) - Sets whether the DrawingPanel window should be visible on the screen.
Type:
bool

width

The width of the panel's canvas area.

Get Method:
unreachable.width(self) - The width of the panel's canvas area.
Set Method:
unreachable.width(self, value) - Sets the panel's canvas width to the given value.
Type:
int

x

The panel's window's left x pixel position.

Get Method:
unreachable.x(self) - The panel's window's left x pixel position.
Set Method:
unreachable.x(self, value) - Sets the panel's window's left x pixel position to the given value.
Type:
int

y

The panel's window's top y pixel position.

Get Method:
unreachable.y(self) - The panel's window's top y pixel position.
Set Method:
unreachable.y(self, value) - Sets the panel's window's top y pixel position to the given value.
Type:
int