Write a program that allows the user to draw a rectangle by dragging the mouse on a Can- vas. The drawing should begin when the user holds the left-mouse button down. With this button held down, the user should be able to resize the rectangle. The drawing ends when the user releases the left button. When the user next clicks on the Canvas, the rectangle should be deleted.

What will be an ideal response?


```
# Draw a rectangle on a Canvas using the mouse.

from Tkinter import *

class DrawRectangle( Frame ):
"""Demonstrate drawing on a Canvas with mouse"""

def __init__( self ):
"""Create Canvas and bind events"""

Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( "Drag to Draw a Rectangle" )
self.master.geometry( "300x200" )

self.display = Canvas( self, bg = "white" )
self.display.pack( expand = YES, fill = BOTH )

# coordinates of the rectangle
self.x1, self.x2 = 0, 0
self.x2, self.y2 = 0, 0

# bind events to the canvas
self.display.bind( "", self.setStart )
self.display.bind( "", self.setEnd )
self.display.bind( "", self.setEnd )

def setStart( self, event ):
"""Set start points for rectangle"""

self.x1 = event.x
self.y1 = event.y

def setEnd( self, event ):
"""Set end points for rectangle and draw"""

self.x2 = event.x
self.y2 = event.y
self.draw()

def draw( self ):
"""Remove rectangle and redraw"""

self.display.delete( "rectangle" )
self.display.create_rectangle( self.x1, self.y1, self.x2,
self.y2, fill = "white", tags = "rectangle" )

def main():
DrawRectangle().mainloop()

if __name__ == "__main__":
main()
```
![15069|219x159](upload://9vN9xmcekE0uX8F1LgUc8WLWcwB.png)

Computer Science & Information Technology

You might also like to view...

What is the default sort order used by the SQL ORDER BY clause?

a. ascending b. none c. descending d. pseudo-random

Computer Science & Information Technology

In the table pictured above, the last row of data shown ( 2734, Riker, William, 212-566-1701.) is a:

A) key. B) record. C) field. D) form.

Computer Science & Information Technology

Rebecca completed work on a computer and is verifying the functionality of the system when she finds a new problem. This problem is not urgent and can be easily fixed. The policy at Rebecca’s company is that all problems need to be reported and given a priority code before they can be assigned to someone.   What should Rebecca do about this new problem?

A. Report the new problem. B. Troubleshoot the new problem. C. Ignore the new problem. D. Determine whether fixing the first problem caused the second problem.

Computer Science & Information Technology

What type of firewall inspects network traffic at a higher level in the OSI model than a traditional stateful packet inspection firewall does?

A. DMZ firewall B. stateless firewall C. upper-layer firewall D. application-aware firewall

Computer Science & Information Technology