0% found this document useful (0 votes)
14 views2 pages

Python Graphics: Create Shapes & Animations

The document provides a tutorial on using the graphics.py library to create a graphical window and draw basic shapes such as circles, rectangles, lines, and text. It also covers user interactions through mouse clicks and keyboard presses, as well as animating shapes and removing objects from the window. Finally, it explains how to close the graphical window.

Uploaded by

Hicham Atatri
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)
14 views2 pages

Python Graphics: Create Shapes & Animations

The document provides a tutorial on using the graphics.py library to create a graphical window and draw basic shapes such as circles, rectangles, lines, and text. It also covers user interactions through mouse clicks and keyboard presses, as well as animating shapes and removing objects from the window. Finally, it explains how to close the graphical window.

Uploaded by

Hicham Atatri
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

graphics.

py

1 1. Creating a Window
from graphics import *

win = GraphWin ( " My Window " , 400 , 400) # Title , width , height
win . getMouse ()
win . close ()

2 2. Drawing Basic Shapes


2.1 Circle
from graphics import *

win = GraphWin ( " Circle " , 300 , 300)

c = Circle ( Point (150 , 150) , 50)


c . setFill ( " blue " )
c . draw ( win )

win . getMouse ()
win . close ()

2.2 Rectangle
r = Rectangle ( Point (50 , 50) , Point (250 , 200) )
r . setOutline ( " red " )
r . setWidth (3)
r . draw ( win )

2.3 Line
l = Line ( Point (0 , 0) , Point (300 , 300) )
l . draw ( win )

2.4 Text
t = Text ( Point (150 , 20) , " Hello world " )
t . setSize (12)
t . setTextColor ( " green " )
t . draw ( win )

1
[Link]

3 3. Waiting for User Actions


3.1 Mouse Click: getMouse()
p = win . getMouse ()
print ( " Mouse click at : " , p . getX () , p . getY () )

3.2 Keyboard Press: getKey()


key = win . getKey ()
print ( " Key pressed : " , key )

4 4. Animating a Shape
from graphics import *
import time

win = GraphWin ( " Animation " , 400 , 400)


ball = Circle ( Point (50 , 200) , 20)
ball . setFill ( " red " )
ball . draw ( win )

for _ in range (100) :


ball . move (2 , 0)
time . sleep (0.05)

win . getMouse ()
win . close ()

5 5. Removing an Object
obj . undraw () # Removes the object from the window

6 6. Closing the Window


win . close ()

You might also like