Modify Exercise 11.5. Allow the user to fill the rectangle with a color. Create a popup menu of possible colors. The popup menu should appear when the user presses the right-mouse button.

What will be an ideal response?


```
# Draw a rectangle on a Canvas using the mouse, allow fill.

from Tkinter import *

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

def __init__( self ):
"""Create canvas, create menu 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 )

colors = [ "White", "Blue", "Yellow", "Red" ]
self.fillColor = StringVar()
self.fillColor.set( colors[ 0 ] )

# create the menu
self.menu = Menu( self, tearoff = 0 )

for item in colors:
self.menu.add_radiobutton( label = item,
variable = self.fillColor,
command = self.setFillColor )

# coordinates and fill color 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 )
self.display.bind( "", self.popupMenu )

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 with specified fill"""

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

def popupMenu( self, event ):
"""Create menu at click position"""

self.menu.post( event.x_root, event.y_root )

def setFillColor( self ):
"""Set rectangle fill color"""

self.display.itemconfig( "rectangle",
fill = self.fillColor.get() )

def main():
drawFillRectangle().mainloop()

if __name__ == "__main__":
main()
```
![15070|460x152](upload://7D7qcG7876pNzbEuye0uPQnH9w3.png)

Computer Science & Information Technology

You might also like to view...

LinkedIn was launched in ________ and currently has more than 250 million registered users

Fill in the blank(s) with correct word

Computer Science & Information Technology

Modify the example program immediately preceding these practice problems to sum the integers from 6 to 10.

What will be an ideal response?

Computer Science & Information Technology

The Towers of Hanoi problem is best solved with a recursive algorithm.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

A ____ is the Visual Basic equivalent of a worksheet.

A. module B. macro C. chart D. code

Computer Science & Information Technology