CMU 15-112 Fall 2015 Quiz 7 Practice: Code Tracing
(Due never)



# Code Tracing #1 of 2 # Draw a picture on a piece of paper showing what this draws when it runs. from tkinter import * def draw(canvas, width, height): # assume (width,height) == (300,300) canvas.create_rectangle(10, 10, width-10, height-10, fill=None) (cx, cy, d) = (width*2/3, height/3, width/10) L = [ ] for i in range(-2,3): (dx, dy, r) = (cx+d*i, cy+abs(d*i), d/2) canvas.create_line(dx, dy, cx, cy+100) canvas.create_oval(dx-r, dy-r, dx+r, dy+r, fill="gray") L.append((dx, 30+10*i**2)) canvas.create_polygon(L, fill="gray") (x0, y0, x1, y1) = (20, 20, 100, 200) canvas.create_rectangle(x0, y0, x1, y1, fill="gray") canvas.create_oval(x0, y0, x1, y1, fill="white") textY = height-60 canvas.create_line(20, textY, width-20, textY) canvas.create_text(width/2, textY, text="CT1", font="Arial 16", anchor=S) canvas.create_text(width/2, textY, text="(What will this draw?)", font="Arial 16", anchor=N) def runDrawing(width=300, height=300): root = Tk() canvas = Canvas(root, width=width, height=height) canvas.pack() draw(canvas, width, height) root.mainloop() print("bye!") runDrawing(300, 300)

# Code Tracing #2 of 2 # Draw the view (on a sheet of paper) after these events happen in this order: # 1) the user runs the program # 2) the user clicks the mouse a few times in the middle of the window # 3) the user types 'abcdef' # 4) the user waits until nothing is moving anymore # Assume the canvas is 300x300. from tkinter import * def init(data): data.x = data.y = 10 data.dx = -10 data.bounces = 0 data.timerDelay = 50 def mousePressed(event, data): data.y = event.y def keyPressed(event, data): if (event.keysym == 'd'): data.x = data.dx = 10 data.bounces = -5 elif ((ord(event.keysym) % 2) == (ord('a')%2)): data.y = min(data.y + 50, data.height - 50) def timerFired(data): if (data.bounces == -1): return x = data.x + data.dx if ((x < 10) or (x > (data.width-10))): data.dx = -data.dx data.bounces += 1 else: data.x = x def redrawAll(canvas, data): (cx, cy, r) = (data.x, data.y, 50) canvas.create_oval(cx-r, cy-r, cx+r, cy+r, fill="cyan") canvas.create_text(cx, cy, text=str(data.bounces), font="Arial 24") #################################### # use the run function as-is #################################### def run(width=300, height=300): def redrawAllWrapper(canvas, data): canvas.delete(ALL) redrawAll(canvas, data) canvas.update() def mousePressedWrapper(event, canvas, data): mousePressed(event, data) redrawAllWrapper(canvas, data) def keyPressedWrapper(event, canvas, data): keyPressed(event, data) redrawAllWrapper(canvas, data) def timerFiredWrapper(canvas, data): timerFired(data) redrawAllWrapper(canvas, data) # pause, then call timerFired again canvas.after(data.timerDelay, timerFiredWrapper, canvas, data) # Set up data and call init class Struct(object): pass data = Struct() data.width = width data.height = height data.timerDelay = 100 # milliseconds init(data) # create the root and the canvas root = Tk() canvas = Canvas(root, width=data.width, height=data.height) canvas.pack() # set up events root.bind("<Button-1>", lambda event: mousePressedWrapper(event, canvas, data)) root.bind("<Key>", lambda event: keyPressedWrapper(event, canvas, data)) timerFiredWrapper(canvas, data) # and launch the app root.mainloop() # blocks until window is closed print("bye!") run(300, 300)