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

Slides:



Advertisements
Similar presentations
Create a Simple Game in Scratch
Advertisements

Microsoft® Small Basic
Chapter 8 Improving the User Interface
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
1 Computer Graphics Chapter 2 Input Devices. RM[2]-2 Input Devices Logical Input Devices  Categorized based on functional characteristics.  Each device.
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CA 121 Intro to Programming Tariq Aziz and Kevin Jones GUI Programming in Visual Studio.NET Chapter 1 Tariq Aziz and Kevin Jones.
Graphical User Interfaces A Quick Outlook. Interface Many methods to create and “interface” with the user 2 most common interface methods: – Console –
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Welcome to CIS 083 ! Events CIS 068.
Peter Andreae Python Workshop for Teachers #2, 3.
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
PYTHON GUI PROGRAMMING
Introduction to Windows Programming
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Computer Science 112 Fundamentals of Programming II Command Buttons and Responding to Events.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 20 Graphical User Interface (GUI)
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 9 GUI Programming Using Tkinter 1.
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Test 2 Review. General Info. All tests are comprehensive. You are still responsible for the material covered prior to the first exam. You will be tested.
Creating visual interfaces in python
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Guide to Programming with Python
How to create a sprite Delete a sprite (e.g. the cat) by right clicking on it and choosing delete. Go to the bottom right hand corner and choose a new.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Getting Started With Python Brendan Routledge
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
MOM! Phineas and Ferb are … Aims:
Topics Graphical User Interfaces Using the tkinter Module
Introduction to Event-Driven Programming
FOP: Buttons and Events
Event loops 16-Jun-18.
Chapter 1 - An Introduction to Computers and Problem Solving
Lesson 1: Buttons and Events – 12/18
Fundamentals of Python: From First Programs Through Data Structures
EE 422C Java FX.
This Week: Tkinter for GUI Interfaces Some examples
Tkinter GUIs Computer Science and Software Engineering
Event loops.
CS 100: Roadmap to Computing
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Fundamentals of Programming I Windows, Labels, and Command Buttons
Let’s begin our game!.
Topics Graphical User Interfaces Using the tkinter Module
Event loops 8-Apr-19.
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

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

© 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

© 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

© 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

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

© 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

© 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

© 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

© 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.

© 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 def drawFlower(x, y) : draw it main() 10

© 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

© 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

© 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

© 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

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

© 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.

© 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

© Peter Andreae Ideas for the classroom 18