lecture12

lecture12 - Lecture 12: November 3, 2010 Classes Caltech CS...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 12 : November 3, 2010 Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Event handling in graphical programs Key events Mouse button events Callback functions bind Global variables
Background image of page 2
Caltech CS 1: Fall 2010 Finish off discussion of global variables Classes and object-oriented programming The class statement creating your own objects Constructors Defining new methods
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 The midterm is due on Friday at 5 PM You have six hours to complete it (should take only four) See the cover sheet for all details
Background image of page 4
Caltech CS 1: Fall 2010 At the end of the last program from previous lecture: if __name__ == '__main__': root = Tk() root.geometry('800x600') canvas = Canvas(root, width=800, height=600) canvas.pack() squares = [] <rest of code as before> We intend to store all handles to squares in the (global) list called squares
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 We modified the button_handler callback function: def button_handler(event): '''Handle left mouse button click events.''' square = draw_random_square(canvas, event.x, event.y, 50, 150) squares.append(square) All newly-created handles to squares are appended to the global squares list
Background image of page 6
Caltech CS 1: Fall 2010 Change the callback function for key presses: def key_handler(event): '''Handle key press events.''' key = event.keysym if key == 'q': quit() elif key == 'c': for square in squares: canvas.delete(square) # Later in the code: root.bind('<Key>', key_handler)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 One problem with this approach: The squares in the squares list were deleted using the canvas.delete method But the squares list itself wasn't emptied out Let's try to do that now
Background image of page 8
Caltech CS 1: Fall 2010 def key_handler(event): ... elif key == 'c': for square in squares: canvas.delete(square) squares = [] Here, we're trying to change the global variables squares by assigning the empty list to it Only one problem: it won't work!
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 def key_handler(event): ... elif key == 'c': for square in squares: canvas.delete(square) squares = [] Python has no way of knowing that squares is supposed to represent a global variable It could just as well be a local variable that we just decided to create at that point in the function
Background image of page 10
Caltech CS 1: Fall 2010 def key_handler(event): ... elif key == 'c': for square in squares: canvas.delete(square) squares = [] When in doubt, Python assumes that a variable is a local variable, not a global variable How do we tell it " squares is supposed to be a global variable, not a local variable"?
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 def key_handler(event): global squares ... elif key == 'c':
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 66

lecture12 - Lecture 12: November 3, 2010 Classes Caltech CS...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online