Graphics ======== .. automodule:: graphics Drawing Functions ----------------- .. autofunction:: graphics.draw_image .. autofunction:: graphics.draw_sprite .. autofunction:: graphics.image_size .. autofunction:: graphics.draw_text .. autofunction:: graphics.text_size .. autofunction:: graphics.clear .. autofunction:: graphics.mainloop Window Functions ---------------- .. autofunction:: graphics.window_size .. autofunction:: graphics.window_title .. autofunction:: graphics.window_background_color .. autofunction:: graphics.frame_rate .. autofunction:: graphics.window_open Keyboard Functions ------------------ .. autofunction:: graphics.key_down .. autofunction:: graphics.key_up .. autofunction:: graphics.key_pressed .. autofunction:: graphics.key_released Mouse Functions --------------- .. autofunction:: graphics.button_down .. autofunction:: graphics.button_up .. autofunction:: graphics.button_pressed .. autofunction:: graphics.button_released .. autofunction:: graphics.mouse_x .. autofunction:: graphics.mouse_y .. autofunction:: graphics.mouse_moved .. _coordinate-system-label: Coordinate System ----------------- The point (0, 0) is the upper left of the window. Positive x is to the right and positive y is down. For example:: >>> import graphics >>> window_width = 160 >>> window_height = 120 >>> graphics.window_size(window_width, window_height) >>> graphics.window_background_color('gray') >>> graphics.draw_image('red_circle.gif', 0, 0) >>> graphics.draw_image('green_circle.gif', window_width, 0) >>> graphics.draw_image('blue_circle.gif', 0, window_height) >>> graphics.mainloop() Would produce the following drawing. .. figure:: coords.png The red circle is at (0, 0), the green circle is at (160, 0), and the blue circle is at (0, 120). .. _colors-label: TKinter Colors Strings ---------------------- The following are valid TKinter Color Strings: +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | Color Strings | | | | | | | | +========================+========================+========================+========================+========================+========================+========================+========================+ | snow | mint cream | cornflower blue | light blue | sea green | pale goldenrod | light salmon | medium violet red | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | ghost white | azure | dark slate blue | powder blue | medium sea green | light goldenrod yellow | orange | violet red | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | white smoke | alice blue | slate blue | pale turquoise | light sea green | light yellow | dark orange | medium orchid | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | gainsboro | lavender | medium slate blue | dark turquoise | pale green | yellow | coral | dark orchid | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | floral white | lavender blush | light slate blue | medium turquoise | spring green | gold | light coral | dark violet | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | old lace | misty rose | medium blue | turquoise | lawn green | light goldenrod | tomato | blue violet | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | linen | dark slate gray | royal blue | cyan | medium spring green | goldenrod | orange red | purple | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | antique white | dim gray | blue | light cyan | green yellow | dark goldenrod | red | medium purple | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | papaya whip | slate gray | dodger blue | cadet blue | lime green | rosy brown | hot pink | thistle | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | blanched almond | light slate gray | deep sky blue | medium aquamarine | yellow green | indian red | deep pink | | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | bisque | gray | sky blue | aquamarine | forest green | saddle brown | pink | | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | peach puff | light grey | light sky blue | dark green | olive drab | sandy brown | light pink | | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | navajo white | midnight blue | steel blue | dark olive green | dark khaki | dark salmon | pale violet red | | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ | lemon chiffon | navy | light steel blue | dark sea green | khaki | salmon | maroon | | +------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+------------------------+ .. _key-symbols-label: TKinter Key Symbols ------------------- The following are valid TKinter key symbols: +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | Key Symbols | | | | | | | | | | | +==============+==============+==============+==============+==============+==============+==============+==============+==============+==============+==============+ | space | slash | greater | M | backslash | k | z | Kanji | Execute | F6 | Super_L | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | exclam | 0 | question | N | bracketright | l | braceleft | Home | Insert | F7 | Super_R | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | quotedbl | 1 | at | O | asciicircum | m | bar | Left | Undo | F8 | Hyper_L | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | numbersign | 2 | A | P | underscore | n | braceright | Up | Redo | F9 | Hyper_R | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | dollar | 3 | B | Q | quoteleft | o | asciitilde | Right | Menu | F10 | Delete | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | percent | 4 | C | R | a | p | BackSpace | Down | Find | Shift_L | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | ampersand | 5 | D | S | b | q | Tab | Prior | Cancel | Shift_R | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | quoteright | 6 | E | T | c | r | Linefeed | Next | Help | Control_L | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | parenleft | 7 | F | U | d | s | Clear | End | Break | Control_R | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | parenright | 8 | G | V | e | t | Return | Begin | Num_Lock | Caps_Lock | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | asterisk | 9 | H | W | f | u | Pause | Win_L | F1 | Shift_Lock | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | plus | colon | I | X | g | v | Scroll_Lock | Win_R | F2 | Meta_L | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | comma | semicolon | J | Y | h | w | Sys_Req | App | F3 | Meta_R | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | minus | less | K | Z | i | x | Escape | Select | F4 | Alt_L | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ | period | equal | L | bracketleft | j | y | Multi_key | Print | F5 | Alt_R | | +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ From http://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm .. _button-symbols-label: TKinter Button Symbols ---------------------- The following are valid TKinter button symbols: +---------------+-----------------------+ | Button Symbol | Mouse Button | +===============+=======================+ |1 | Left Mouse Button | +---------------+-----------------------+ |2 | Middle Mouse Button | +---------------+-----------------------+ |3 | Right Mouse Button | +---------------+-----------------------+ |4 | Up Mouse Scroll Wheel | +---------------+-----------------------+ |5 | Up Mouse Scroll Wheel | +---------------+-----------------------+ .. _examples-label: Examples -------- Here is an example of drawing using the Graphics module:: import graphics # Initialize the window window_width = 640 window_height = 480 window_background_color = 'white' window_title = 'Testing...' graphics.window_size(window_width, window_height) graphics.window_background_color(window_background_color) graphics.window_title(window_title) # Draw a circle to it circle_image_name = 'red_circle.gif' circle_x = 200 circle_y = 100 graphics.draw_image(circle_image_name, circle_x, circle_y) # Draw some text on top of the circle text = 'Testing' text_x = circle_x text_y = circle_y text_color = 'black' text_size = 12 graphics.draw_text(text, circle_x, circle_y, text_color, text_size) # Keep the window from closing after drawing graphics.mainloop() Here is an example of animating using the Graphics module:: import graphics # Initialize the window window_width = 640 window_height = 480 window_background_color = 'white' window_title = 'Testing...' graphics.window_size(window_width, window_height) graphics.window_background_color(window_background_color) graphics.window_title(window_title) # Initialize a circle circle_image_name = 'red_circle.gif' circle_x = 200 circle_y = 100 number_of_frames = 100 # Animate the circle for a fixed number of frames for frame in range(number_of_frames): # Erase any previous drawing graphics.clear() # Change the location of the circle circle_x += 1 # Draw the circle at the new location graphics.draw_image(circle_image_name, circle_x, circle_y) # Keep the window from closing after animating graphics.mainloop() Here is an example of user interaction using the Graphics module:: import graphics # Initialize the window window_width = 640 window_height = 480 window_background_color = 'white' window_title = 'Testing...' graphics.window_size(window_width, window_height) graphics.window_background_color(window_background_color) graphics.window_title(window_title) # Initialize a circle circle_image_name = 'red_circle.gif' circle_x = 200 circle_y = 100 number_of_frames = 100 # Animate the circle until the window closes while graphics.window_open(): # Erase any previous drawing graphics.clear() # Change the location of the circle based on what key the user is pressing if graphics.key_down('Right'): circle_x += 1 if graphics.key_down('Left'): circle_x -= 1 # Draw the circle at the new location graphics.draw_image(circle_image_name, circle_x, circle_y) # Keep the window from closing after animating graphics.mainloop()