lecture10

lecture10 - Lecture 10: October 27, 2010 More Graphics and...

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

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 10 : October 27, 2010 More G r a p h i c s and 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 Testing Graphics Introductory concepts GUIs, pixels, pixel coordinates Python graphics with Tkinter
Background image of page 2
Caltech CS 1: Fall 2010 More graphics Additional graphics elements Event handling Event loops
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 # File: tkinter1.py from Tkinter import * root = Tk() root.geometry('800x600') raw_input('Press <return> to quit.')
Background image of page 4
Caltech CS 1: Fall 2010
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 Much as we love blank windows, we want to do more than this! The root window is basically just a container in which we can put other things We will put a drawing surface called a canvas inside it canvas: analogy to painter's canvas
Background image of page 6
Caltech CS 1: Fall 2010 Let's add two new lines to the example: from Tkinter import * root = Tk() root.geometry('800x600') c = Canvas(root, width=800, height=600) c.pack() raw_input('Press <return> to quit.')
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 from Tkinter import * root = Tk() root.geometry('800x600') c = Canvas(root, width=800, height=600) c.pack() This creates a new canvas object called c Its parent is the root object it will be located entirely inside that object on screen Its dimensions will be 800x600 pixels note keyword arguments: width , height
Background image of page 8
Caltech CS 1: Fall 2010 from Tkinter import * root = Tk() root.geometry('800x600') c = Canvas(root, width=800, height=600) c.pack() A canvas is a Python object too, so it has methods The pack method positions the canvas inside its parent (the root object) Since they are both the same size, the canvas completely covers the root object
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 from Tkinter import * root = Tk() root.geometry('800x600') c = Canvas(root, width=800, height=600) c.pack() Without this line, the canvas will never show up on the screen! So don't leave it out!
Background image of page 10
Caltech CS 1: Fall 2010 Now we've created the root window the canvas It's time to do some actual drawing on the canvas
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 # . .. as before . .. c = Canvas(root, width=800, height=600) c.pack() r = c.create_rectangle(0, 0, 50, 50, \ fill='red', outline='red') This creates a rectangle r on the canvas c create_rectangle is a method of the canvas object c
Background image of page 12
Caltech CS 1: Fall 2010 # . .. as before . .. c = Canvas(root, width=800, height=600) c.pack() r = c.create_rectangle(0, 0, 50, 50, \ fill='red', outline='red') The first four arguments: 0 , 0 , 50 , 50 mean: rectangle's upper left-hand corner is at location ( 0 , 0 ) rectangle's lower right-hand corner is at location ( 50 , 50 ) so it's actually a square of dimensions 50x50 pixels
Background image of page 13

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

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 # . .. as before . .. c = Canvas(root, width=800, height=600)
Background image of page 14
Image of page 15
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 / 65

lecture10 - Lecture 10: October 27, 2010 More Graphics and...

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

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