0% found this document useful (0 votes)
27 views5 pages

Events and Animation: The Bouncing Square: Global vs. Local Canvas

1) This document discusses animating a bouncing square on a Tkinter canvas widget in Python. It describes initializing global variables to track the square's position, size and color. 2) A timer function moves the square and redraws it, checking the global data to determine if it should bounce left/right or up/down. 3) All functions work together to initialize the square, move and redraw it to create an animated bouncing effect on the canvas.

Uploaded by

CRCF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Events and Animation: The Bouncing Square: Global vs. Local Canvas

1) This document discusses animating a bouncing square on a Tkinter canvas widget in Python. It describes initializing global variables to track the square's position, size and color. 2) A timer function moves the square and redraws it, checking the global data to determine if it should bounce left/right or up/down. 3) All functions work together to initialize the square, move and redraw it to create an animated bouncing effect on the canvas.

Uploaded by

CRCF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Events and Animation: The Bouncing Square

So far, the Tkinters canvas widget has made drawing squares, circles, text, and even nyan
Poptart cats, practical and relatively straightforward. Now its time to animate our canvas
Global Vs. Local Canvas
!eve now see two ways of accessing our canvas in the various functions that might need
it" storing the widgets information within a glo#al dictionary, or declaring the canvas
widget itself as glo#al. !hen displaying many o#$ects on a canvas, however, we need a way
to store the information of each o#$ect %perhaps its location or color& for easy access later.
!ell use a glo#al dictionary for our #ouncing square example.
The init Function
!hen using a canvas in python, ' lines need to #e present nearly every time"
from Tkinter import (
root ) Tk
canvas ) *anvas%root, width ) foo, height ) #ar&
canvas.pack%&
root.mainloop%&
+ow we organi,e these %and all su#sequent lines& is highly flexi#le, however. -n a complex
animation, its a good idea to have a run function handle these initiali,ing steps, like so"
from Tkinter import (
def run%&"
root ) Tk
canvas ) *anvas%root, width ) foo, height ) #ar&
canvas.pack%&
root.mainloop%&
run%&
.or our #ouncing square example, there are quite a few varia#les we need to keep track of,
including where our square is at any given moment, how large our square is, what color,
etc. !ell create a glo#al dictionary to keep track of all this data, and to change some as
needed.
from Tkinter import (
def run%&"
root ) Tk
Events and Animation: The Bouncing Square
foo ) *anvas%root, width ) foo, height ) #ar&
canvas.pack%&
glo#al data
data ) /0
data12canvas34 ) foo
root.mainloop%&
run%&
This is not enough, however" with so many varia#les, it might #e a good idea to have a
function that initiali,es this type of information, so when we first draw the square %#efore
it moves at all&, everything exists. !e call this an init function, and it might look like this"
from Tkinter import (
def init%&"
glo#al data
data12square*olor34 ) 2#lue5
data12square6eft34 ) '7
data12squareTop34 ) '7
data12squareSi,e34 ) '7
def run%&"
root ) Tk
foo ) *anvas%root, width ) foo, height ) #ar&
foo.pack%&
glo#al data
data ) /0
data12canvas34 ) foo
init%&
root.mainloop%&
run%&
The redraAll !unction
!ith our squares starting point esta#lished, it would #e nice to see the square. To clear off
the canvas of old images, we would need to call foo.delete%866&, and then draw the square
#ased off the data saved in the glo#al dictionary. 9emem#er that the syntax for drawing a
square looks like this, where foo is the canvas, left:right:top:#ottom are integers, and
color is a string"
foo.create;rectangle%left, top, right, #ottom, fill)color&
Events and Animation: The Bouncing Square
Timer Fired
8#le to draw our square, we need to keep it moving across the canvas. 8 function that
serves as a timer would #e handy<perhaps using foo.after%delay, function& would help=
+aving the function, timer.ired, change the data from which we draw our square, as well as
having it call redraw8ll, would #e nice 8n example, where doTimer.ired is provided code
that moves the squares data"
def timer.ired%&"
glo#al data
foo ) data12foo34
doTimer.ired%&
redraw8ll%&
delay ) '7
foo.after%delay, timer.ired&
Let"s #ut it all together$
from Tkinter import (
def run%&"
root ) Tk%&
w ) >77
h ) '77
foo ) *anvas%root, width)w, height)h&
foo.pack%&
glo#al data
data ) /0
data12foo34 ) foo
data12w34 ) w
data12h34 ) h
init%&
timer.ired%&
root.mainloop%&
def init%&"
glo#al data
data12square*olor34 ) 2#lue5
data12square6eft34 ) '7
Events and Animation: The Bouncing Square
data12squareTop34 ) '7
data12squareSi,e34 ) '7
data12counter34 ) 7
data12going934 ) True
data12going?34 ) .alse
def timer.ired%&"
glo#al data
foo ) data12foo34
doTimer.ired%&
redraw8ll%&
delay ) '7
foo.after%delay, timer.ired&
def doTimer.ired%&"
glo#al data
if data12going934"
if %data12square6eft34 @ data12squareSi,e34 A data13w34&"
data12going934 ) .alse
else"
move9ight%&
else"
if %data12square6eft34 B 7&"
data12going934 ) True
else"
move6eft%&
if data12going?34"
if %data12squareTop34 @ data12squareSi,e34 A data13h34&"
data12going?34 ) .alse
else"
move?own%&
else"
if %data12squareTop34 B 7&"
data12going?34 ) True
else"
moveCp%&
def move6eft%&"
glo#al data
data12square6eft34 D) E7
def move9ight%&"
glo#al data
Events and Animation: The Bouncing Square
data12square6eft34 @) E7
def moveCp%&"
glo#al data
data12squareTop34 D) E7
def move?own%&"
glo#al data
data12squareTop34 @) E7
def redraw8ll%&"
glo#al data
data12foo34.delete%866&
data12foo34.create;rectangle%data12square6eft34,
data12squareTop34,
data12square6eft34 @ data12squareSi,e34,
data12squareTop34 @ data12squareSi,e34,
fill)data12square*olor34&
run%&

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy