Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools.

Similar presentations


Presentation on theme: "Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools."— Presentation transcript:

1 Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools

2 © Peter Andreae Intro Quick overview of what’s new in level 3 Programming Go through key ideas of L3 in python, making little programs Discussion of ideas for the classroom. 2

3 © Peter Andreae Level 3: GUI program must use some kind of event-driven input Object-oriented: defining a Class, making & using Objects of that class Staged development repeated cycles of design, code, test, debug on increasing scope of the program 3

4 © Peter Andreae Level 3:GUI Program must have its own window Not inside the “shell” Might have graphical output, but not critical Must respond to some kind of event-driven input eg buttons, text fields, mouse clicks Event-driven input changes the design and structure of the program The program is no longer in control. Fancy layout and good UI design is not required Code for fancy layout is tricky and fiddly and not important Programs should not be so complicated that UI design is significant two or three buttons would be sufficient (at least for achieved) Autogenerated code (drag and drop) is not banned, but doesn’t demonstrate that they understand it. 4

5 © Peter Andreae Example Peg puzzle has three buttons to control the actions has graphical output (draws the puzzle on the screen) 5

6 © Peter Andreae Level 3:using Classes and Objects An object is a way of wrapping up multiple bits of data and functions that use that data. “Fields” are variables in the object to hold values Each object of the class has its own copies of the fields Functions in the class operate on the particular object they were called on, access the fields of that object turtle1.forward(20) turtle2.turn(30) Using objects of predefined classes is unavoidable eg calling append on a list: names.append(“mary”) eg calling forward on a turtle: sam = turtle.Turtle() sam.forward(20) Standard expects them to define new classes, and create and use them 6

7 © Peter Andreae Example Bouncing balls: Each ball is represented by an object, storing the size, colour, and current speed and position of the ball. There is a list of the ball objects Reset creates a new list of ball objects The main loop runs through the list, telling each ball to move then redraws them all. 7

8 © Peter Andreae Level 3: Staged development design, code, test, debug small part of program repeatedly extend the design and the code to cover more features/aspects test and debug requires a task for which there is a simple “core”, but which can be extended in a series of stages 8

9 © Peter Andreae Graphical output and GUI Need tkinter library to do any graphical input/output or GUI from tkinter import * Simple graphical output: set up window and canvas def setup () : global canvas window = Tk() canvas = Canvas(window, width=600, height=450, bg='white') canvas.pack() call drawing commands on the canvas canvas.create_rectangle(left, top, right, bot,fill=“blue”) call canvas.update() to make changes visible.

10 © Peter Andreae To Do Write program to draw a flower on the canvas: (the center circle is optional ) Ensure you define a function to draw it at a given position, and call it from your main() function. def main() : set up the window, canvas, etc draw a flower at 100 100 def drawFlower(x, y) : draw it main() 10

11 © Peter Andreae Event driven input Buttons Create button and specify function to call when button is pressed. Button(window, text=“Flower", command=draw).pack() Button(window, text=“Clear”, command= clear).pack() Tell tk to listen for button/mouse events window.mainloop() define function that the button calls (no parameters) def draw() : def clear() : Mouse bind mouse event to function called when mouse event happens: canvas.bind(" ", command=plant) define function that responds to the mouse event: def plant(event) : ….. event.x …. event.y

12 © Peter Andreae To Do Extend the flower program Make a button that draws a flower in the middle of the canvas Make the program respond to the mouse by drawing a flower where it was clicked. 12

13 © Peter Andreae Objects Whenever you have some kind of entity in your program more than just a string more than just a number especially if your program has more than one of them Create a class describing the entity work out what information needs to be stored about the entity work out what actions can be performed on the entity Eg, flower: need to store its position, and possibly its colour need to be able to draw it 13

14 © Peter Andreae To create a class Define the name of the class convention: capitalised name class Flower() : Define the constructor The function that is called to set up /initialise an object of the class typically sets up the fields def __init__(self, …..other parameters it needs) self. Define the other functions that act on the objects all the functions have a first parameter of self. they can access the fields via self def grow(self, …. other parameters) self.size = self.size+5 14

15 © Peter Andreae Flower class Flower () : def __init__(self, ) def draw(self, ) def bloom (self ) 15

16 © Peter Andreae Example Programs Garden : contains a Flower class has a list of Flower objects respond to mouse by adding a new flower to the garden button to make all the flowers turn red Walk through the program.

17 © Peter Andreae To Do: Write program for planning a banquet layout Shows a collection of tables, Allow the user to place or move tables with the mouse Design: A Table class A main program with canvas which responds to mouse Extensions: buttons to allow the tables to be lined up Extend the table to have a list of names

18 © Peter Andreae Ideas for the classroom 18


Download ppt "Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools."

Similar presentations


Ads by Google