03. Modulesx
Download
Report
Transcript 03. Modulesx
142-253 Computer
Programming
BSc in Digital Media, PSUIC
Aj. Andrew Davison, CoE, PSU Hat Yai Campus
E-mail: [email protected]
3. Modules
Using modules to give Python even more POWER
Look at built-in modules: random, math, turtle, winsound
Start turtle graphics.
Look at installing easygui using pip
Using easygui
1
1. What is a Module?
A module is a program that adds extra features
to Python
e.g. for 3D graphics, for writing games
There are lots of modules that come with Python
called built-in or standard modules
It is easy to add more
called third-party modules
2
Python's Design
A small
language …
… plus lots
of modules
turtle
math
winsound
3
2. Using the random Module
4
List Modules Already in Python
5
Help on any Modules using the
word "random"
6
click on "modules"
Module Documentation
7
8
Information on random
Look under 'r' in the Python Module Index
9
3. The math Module
Function name
abs(value)
Description
Constant
Description
absolute value
e
2.7182818...
ceil(value)
rounds up
pi
3.1415926...
cos(value)
cosine, in radians
degrees(value)
convert radians to degrees
floor(value)
rounds down
log(value, base)
logarithm in any base
log10(value)
logarithm, base 10
max(value1, value2, ...) larger of two (or more) values
min(value1, value2, ...) smaller of two (or more) values
radians(value)
convert degrees to radians
round(value)
nearest whole number
sin(value)
sine, in radians
sqrt(value)
square root
tan(value)
tangent
import math
or
from math import *
10
11
Information on math
Look under 'm' in the Python Module Index
12
4. The turtle Module
The turtle moves about in a drawing window with a pen
for drawing lines.
The turtle can be told to turn a number of degrees,
move a distance, move to a new position, and change
its pen's color and line width
If the turtle’s pen is down, it draws a line; otherwise, it
moves without drawing
The Turtle Drawing Window
The turtle is an icon
Initial position: (0, 0)
Initial direction: East (0°)
Color: black
Line width: 1 pixel
Pen: down (ready to draw)
90°
x-axis
(0,0)
180°
0°
y-axis
270°
Create a Turtle
>>> from turtle import Turtle
>>> sleepy = Turtle()
Tell Python to add the
Turtle command to its
built-in commands
Some turtle Commands
home()
# Return to (0, 0) and 0° (east)
down()
# Enable drawing
up()
# Disable drawing
forward(distance)
# Go distance in current direction
goto(x, y)
# Go to (x, y)
left(degrees)
# Add degrees to current direction
setheading(degrees)
# Make degrees the new direction
width(width)
# Set width of pen in pixels
color(color)
# Red, green, blue, etc.
Move a Distance
>>> from turtle import Turtle
>>> sleepy = Turtle()
>>> sleepy.forward(50)
Move 50 pixels in the current direction.
There's also a backward() command.
Turning Left or Right
A turtle can turn left or right by a number of
degrees
e.g. sleepy.left(45)
This angle is offset from the turtle's current
forward direction.
left(45)
forward
left(45)
forward
18
Tell the turtle to draw a square
Square.py
19
Draw a House
House.py
20
A Blue Equilateral Triangle
Triangle.py
21
Why those Angle?
Why does the square need 4 turns of 90*?
Why does the triangle need 3 turns of 120*?
Look for a pattern...
4 * 90 == 3 * 120
== 360 degrees
How would you get the turtle to draw a fivesided shape (a pentagon)?
22
A Faster Turtle
Square.py
23
24
Filling the Shape with Color
Square.py
25
26
Drawing (Bits of) Circles
Circles.py
27
Writing Text
HelloWorld.py
28
goto(x,y)
Also used in setpos(x,y)
The usual turtle window has
-200 ≤ x ≤ 200 and
-150 ≤ y ≤ 150
29
Print Information about the Turtle
>>> from turtle import Turtle
>>> sleepy = Turtle()
>>> sleepy.goto(0, 50)
>>> print(sleepy.position(), sleepy.pencolor(), \
sleepy.heading(), sleepy.isdown())
(0.00,50.00) black 0.0 True
Information on turtle
Look under 't' in the Python Module Index
31
6. Windows Sound: winsound
32
Have you seen Star Wars?
Run the programs:
March.py
playSound.py
Python Gangnam Style:
Gangnam.py
33
7. Getting More Modules
https://pypi.python.org/pypi
I'll search for "GUI"
34
lots more off the bottom of this picture
35
the "easygui" looks good; click on it for
lots more details
36
Info on easygui
The easygui webpage:
http://easygui.sourceforge.net/
you can also download easygui from there
then use pip on the downloaded zip file
An easygui tutorial:
http://www.ferg.org/easygui/tutorial.html
37
8. Installing a PyPI Module
Use the pip program that comes as part of the
Python download.
Use it from the Command window.
38
Use pip to Search PyPI
39
Use pip to Install easygui
40
More pip commands
pip help
gives help on using pip
pip list
lists the extra (thitd-party) modules that you have
added to Python
pip install <downloaded file>
installs a module that you've downloaded yourself
41
9. Use easygui in Python
42
Use easygui with Less Typing
Tell Python to add all the
easygui commands to
Python's built-in commands
43
Getting the "OK" Input
44
Dialog Box with Buttons
IceButtons.py
45
(Ugly) Choice Box
IceChoice.py
46
Text Input
IceEnter.py
47
Response
48