Complete the following incomplete program (begin by copy-pasting that into a new program file). The completed program will provide a user interface to draw as many circles as the user wants only by clicking in the window. After getting the number of circles (n) to draw as input from the user through the provided interface, the program should iterate n times. In each iteration it should wait for the user to click twice and draw a circle with center at the point where the user clicked first and with radius calculated as its distance from the second point where the user clicked. It should then fill the circle with a random color. Hints: Distance between two points with coordinates x1 & y1 and x2 & y2 and can be calculated as sqrt((x1-x2)2 + (y1-y2)2 ). To generate a random color, use randint(0,255) from the random libray to generate a random number between 0 and 255 (both inclusive). Use it three times to get the r, g and b values to generate a random color from graphics import * from math import sqrt from random import randint def main() : win = GraphWin("Circles", 500, 500) text1 = Text(Point(152,20),"How many circles do you want to draw:") text2 = Text(Point(120,60),"Click anywhere after entering.") text1.draw(win) text2.draw(win) nbox = Entry(Point(40,40),4) nbox.draw(win) win.getMouse() n = int(nbox.getText()) text2.undraw() nbox.undraw() text1.setText("Click twice to draw each circle.") # complete the rest of the program # to end the window text1.setText("Click anywhere to quit.") win.getMouse() win.close() main()
Complete the following incomplete program (begin by copy-pasting that into a new program file). The completed program will provide a user interface to draw as many circles as the user wants only by clicking in the window. After getting the number of circles (n) to draw as input from the user through the provided interface, the program should iterate n times. In each iteration it should wait for the user to click twice and draw a circle with center at the point where the user clicked first and with radius calculated as its distance from the second point where the user clicked. It should then fill the circle with a random color.
Hints:
- Distance between two points with coordinates x1 & y1 and x2 & y2 and can be calculated as sqrt((x1-x2)2 + (y1-y2)2 ).
- To generate a random color, use randint(0,255) from the random libray to generate a random number between 0 and 255 (both inclusive). Use it three times to get the r, g and b values to generate a random color
from graphics import *
from math import sqrt
from random import randint
def main() :
win = GraphWin("Circles", 500, 500)
text1 = Text(Point(152,20),"How many circles do you want to draw:")
text2 = Text(Point(120,60),"Click anywhere after entering.")
text1.draw(win)
text2.draw(win)
nbox = Entry(Point(40,40),4)
nbox.draw(win)
win.getMouse()
n = int(nbox.getText())
text2.undraw()
nbox.undraw()
text1.setText("Click twice to draw each circle.")
# complete the rest of the program
# to end the window
text1.setText("Click anywhere to quit.")
win.getMouse()
win.close()
main()
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images