lecture11

lecture11 - Lecture 11: November 1, 2010 Graphics 3: More...

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 11 : November 1, 2010 G r a p h i c s 3 : More Event Handling
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 Graphics lines, circles, ovals event handling mainloop()
Background image of page 2
Caltech CS 1: Fall 2010 Event handling bind key presses, mouse clicks using events Graphical object "handles" A way to control objects already on the canvas Global variables
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 Last time we had: from Tkinter import * root = Tk() root.geometry('800x600') c = Canvas(root, width=800, height=600) c.pack() r = c.create_rectangle(0, 0, 50, 50, fill='red', outline='red') root.mainloop()
Background image of page 4
Caltech CS 1: Fall 2010 We added the root.mainloop() line in place of the raw_input line The drawing doesn't change Now, the only way to exit the program is to close the window or to quit Python So far, haven't added any code to handle any events
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 Let's add an event so that pressing the q key on the canvas quits the application We will add just one line before the mainloop line: root. bind ('<q>', quit) Let's look at this line in detail
Background image of page 6
Caltech CS 1: Fall 2010 root.bind('<q>', quit) This says: The graphical object that will handle the event is the root object The event to be handled is when the user presses the q key on the keyboard Tkinter represents this as the string '<q>' When the q key is pressed, the built-in quit function will be executed This causes Python to exit immediately
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 root. bind ('<q>', quit) We say that this method call binds the event (pressing the q key on the keyboard) to the function quit (which handles the event)
Background image of page 8
Caltech CS 1: Fall 2010 This works, but there is one odd thing When q is pressed, the program exits but before it does, it prints out something like: <Tkinter.Event instance at 0x776058> What does this mean?
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 The quit function usually gets no arguments If it gets an integer argument, it passes it to the operating system (the reason isn't important) If it gets a non-integer argument, it prints it and then exits Somehow, here it got an argument of type Tkinter.Event
Background image of page 10
Caltech CS 1: Fall 2010 What happened: When the q key on the keyboard was pressed 1. Tkinter created a Tkinter.Event object 2. Tkinter 's event loop ( mainloop ) checked to see if the q key was bound to any function 3. It found that q was bound to the quit function 4. It called quit with the Tkinter.Event object as its argument 5. quit printed the event and then made Python exit
Background image of page 11

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

View Full DocumentRight Arrow Icon
Another weird thing. .. Look at the line: root.bind('<q>', quit ) quit is the name of a Python function The bind method of the root object can take a function as an argument! Python allows functions to be treated as data
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.

Page1 / 72

lecture11 - Lecture 11: November 1, 2010 Graphics 3: More...

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