Search
  • Kaitlyn Flowers

Class 6 Recap!

Updated: 3 days ago

During Class 6, we finished up our programming challenge with the Python turtle library. We also looked at the python Tkinter library and discussed final projects.


As Always, here is the Class 6 powerpoint:

Class 6 powerpoint
.pptx
Download PPTX • 9.08MB

1. Creating a Starry Night Sky

Challenge #2:

Use your code from class 5 where we imported the turtle library and created an instance of the Turtle object and Screen object.


import turtle

t = turtle.Turtle()
s = turtle.Screen()

For, Starry Night Sky, we want to set our Screen to a dark blue. Use the bgcolor() function and set the instance of the Screen object (s in the previous code block) to dark blue using the instance name and the function bg.color() with dot notation.


s.bgcolor("dark blue")

To draw stars in your sky, define a new function, which takes parameters for the x and y coordinates (defining the location for your star) and the size of your star. Use the penup() function and goto() function to move your turtle without drawing on the screen to the desired x and y coordinate location. Then use the star() function (which we defined in our last class) to draw the star. Move around the screen and play around with different size stars at different locations. See below for the defined star() function, triangle() function, and the drawStarAt() function.


def triangle(size):
    t.width(3)
    t.color("yellow")
    t.fillcolor("yellow")
    t.begin_fill()
    for x in range (3):
        t.forward(size)
        t.left(120)
    t.end_fill()

def star(size):
    t.left(60)
    triangle(size)
    t.right(30)
    t.penup()
    t.forward(size*.6)
    t.pendown()
    t.left(90)
    triangle(size)
    
def drawStarAt(xcor, ycor, size):
    t.penup()
    t.goto(xcor, ycor)
    star(size)

If needed, you can review functions in our Class 3 blog post. You can also review the turtle functions in our Class 5 blog post. Notice how we first define the triangle() function, then use that function to define the star function, and then use the star() function to define the drawStarAt() function. By defining these functions, we can use them all throughout our code without rewriting the line-by-line instructions.


***Remember when defining your functions in python, your function's output commands must be indented in order for python to actually recognize them as the function's output. Conversely, any command outside of the function must not be indented in order to be considered separate from the output.


Next, call your drawStarAt() function and assign integer values for the xcor, ycor, and size arguments. These stars can be any size and be located anywhere on the background screen! Get creative and play around with the aesthetic of your Starry Night Sky!


drawStarAt(100, 80, 50)
drawStarAt(200, 300, 78) #draw as many stars as you desire

Last, use the write() function to display the message Starry Night Sky on your screen. Remember to use the penup() function and goto() function to move your message. Also, note that the center of your screen is (0,0).


Altogether, your code should look like this:


import turtle

t = turtle.Turtle()
s = turtle.Screen()

s.bgcolor("dark blue")

def triangle(size):
    t.width(3)
    t.color("yellow")
    t.fillcolor("yellow")
    t.begin_fill()
    for x in range (3):
        t.forward(size)
        t.left(120)
    t.end_fill()

def star(size):
    t.left(60)
    triangle(size)
    t.right(30)
    t.penup()
    t.forward(size*.6)
    t.pendown()
    t.left(90)
    triangle(size)
    
def drawStarAt(xcor, ycor, size):
    t.penup()
    t.goto(xcor, ycor)
    star(size)
    
drawStarAt(100, 80, 50)
drawStarAt(200, 300, 78) #draw as many stars as you desire

t.penup()
t.goto(0,0)
t.write("Starry Starry Night!", align="center", font=("Georgia", 30, "bold"))

2. Tkinter

Python Tkinter is short for Tk interface, another built in library in Python. Tk interface uses a GUI (Graphical User Interface) which enables communication between the user and computer in a more user-friendly way. GUI often use buttons, text boxes, etc. You can even use Tkinter with your turtle library (or other libraries for that matter).


In comparison, we have been using a CLI (Command line interface) which uses text-based commands (like the code above).


To start using tkinter, you will need to import the library.


from tkinter import*
from tkinter import ttk
root=Tk()
screen=Tk()

Line 1 imports the entire Tkinter library (including functions and variables) so that you can use it in your own python code. Line 2 imports widgets (the buttons or text boxes that will be displayed on the screen). And, line 3 opens a window so that you may view these widgets.


Create an instance of the Button and Label objects included in the ttk library. Your button object will simply create a new button and the label object will label that button. See below.


myButton = ttk.Button(root, text = "Click here")
myButton.pack()

myLabel=ttk.Label(root, text = "My first GUI").pack()

I used the function pack() to display the button and label. You can call the function using a separate line or create and instance variable and call the pack() function in the same line (shown above).


Define a function that changes the status of your button after being clicked.


*Make sure to include this function before creating the instance variables (otherwise the button won't be able to reference your function).


def on_click():
    myButton.config(text="Clicked!")
    
myButton=ttk.Button(root, text="Click here", command=on_click)
myButton.pack()

You can also import images using Tkinter. Simply create an instance variable of the PhotoImage object (but only use a PNG, GIF, PPM, or PGM file - the PhotoImage library has only been defined for these types of files).


***Make sure that your image is located in the same folder as your python file (this allows your python code to access the image without accessing the rest of the files on your computer). We recommend renaming the image to something simple (like lion.png) so that you can more easily reference it in your code. See below.


image1 = PhotoImage(file="lion.png")
myImage=ttk.Label(root, image=image1).pack()

To create a text box, use the code below. This allows you to get user input.

myEntry = ttk.Entry(root, width=30).pack()
myEntry.get()

Instead of the pack() function, you can use other functions to specifically position your widget.

  • grid(row=0, column=0) will place your widget on the screen as if the screen were a grid.

  • place (x=0, y=0) will place your widget on the screen as if the screen were a coordinate plane (this is similar to how we placed the stars in Challenge 2).

*Note that you can always change the values of x, y, row, and column) (they are customizable arguments).


We encourage you to work on your own and combine the tkinter and turtle libraries together. You may look at slide 14 of the presentation for an example.


3. Final Project

Congrats girls! You have learned so much python over the course of this summer (and coding is incredibly hard!). Kudos to you!


As we are approaching the final few weeks of our program, we encourage you to come up with a personal project to create and later present to the group (and your parents!). This project could be a calculator, a special drawing, a quiz, a virtual interview/conversation (anything you want!).


Brainstorm a project feasible of completing using functions, lists, definitions, loops, and libraries that we have been working with over the summer. Note what variables you might need, new libraries you might need to import, and whether you want your program to be user interactive.


Please fill out our project proposal form (it will be in your inbox soon!) by Sunday. We will review your proposal and give you the go-ahead and help you work out any kinks in you plan.


If you have any questions, don't hesitate to reach out.


Mana: nvale@exeter.edu

Rachael: skim1@exeter.edu

Minseo: mkim14@exeter.edu

Kaitlyn: kflowers@exeter.edu

11 views

Recent Posts

See All

End of our Summer Program

Our Summer Program has officially ended on August 19, 2020. Our girls presented their final projects that they have worked on for a couple of weeks. We were impressed by their creativity and improveme

Phillips Exeter Academy Girls Who Code

Phillips Exeter Academy

20 Main Street

Exeter, NH 03833

Quick Links

© 2020 by Exeter Girls Who Code

This site was designed with the
.com
website builder. Create your website today.
Start Now