In: Computer Science
Python: I am making a program that prints a circle of random size, random location, with user_inputed (using entry) color, when 'DRAW' button is clicked.
I am stuck on passing variables on the while loop. Please Help
from graphics import*
import sys
import random
from time import sleep
def main():
win = GraphWin ('What Color', 500, 600)
win.setCoords(0, 0, 500, 600)
DB = Rectangle(Point(350, 580), Point(400, 560))
DB.setFill('light Green')
DB.draw(win)
QB = Rectangle(Point(430, 580), Point(480, 560))
QB.setFill('red')
QB.draw(win)
DB_t = Text(Point(375,570),'DRAW')
DB_t.setSize(10)
DB_t.draw(win)
QB_t = Text(Point(455,570),'QUIT')
QB_t.setSize(10)
QB_t.draw(win)
Ins_t = Text(Point(125, 570), 'Color for next Circle:')
Ins_t.setSize(10)
Ins_t.draw(win)
E1 = Entry(Point(215, 570), 5)
E1.setFill('white')
E1.draw(win)
size = random.randrange (10,25)
x = random.randrange(5, 495)
y = random.randrange(5, 595)
while True:
for i in circle(colors):
if circle(color_ui) == i:
c = Circle(Point(x,y), size)
c.setFill(i)
while True:
m = win.checkMouse()
if m:
if 430 < m.getX() < 480 and 560 < m.getY() < 580:
win.close()
if 350 < m.getX() < 400 and 560 < m.getY() < 580:
if E1.getText():
c.draw(win)
def circle(c, E1):
colors =
['red','blue','green','yellow','black','purple','pink']
color_ui = E1.getText
while True:
for i in colors:
if color_ui == i:
return i
colors, color_ui
# do comment if any problem arises
# Code
from graphics import*
import sys
import random
from time import sleep
def main():
win = GraphWin('What Color', 500, 600)
win.setCoords(0, 0, 500, 600)
DB = Rectangle(Point(350, 580), Point(400, 560))
DB.setFill('light Green')
DB.draw(win)
QB = Rectangle(Point(430, 580), Point(480, 560))
QB.setFill('red')
QB.draw(win)
DB_t = Text(Point(375, 570), 'DRAW')
DB_t.setSize(10)
DB_t.draw(win)
QB_t = Text(Point(455, 570), 'QUIT')
QB_t.setSize(10)
QB_t.draw(win)
Ins_t = Text(Point(125, 570), 'Color for next Circle:')
Ins_t.setSize(10)
Ins_t.draw(win)
E1 = Entry(Point(215, 570), 5)
E1.setFill('white')
E1.draw(win)
oldc = None
while True:
size = random.randrange(10, 25)
x = random.randrange(5, 495)
y = random.randrange(5, 595)
c = Circle(Point(x, y), size)
m = win.checkMouse()
if m:
if oldc != None:
oldc.undraw()
if 430 < m.getX() < 480 and 560 < m.getY() < 580:
win.close()
if 350 < m.getX() < 400 and 560 < m.getY() < 580:
if E1.getText():
c.setFill(E1.getText())
c.draw(win)
# save old circle
oldc = c
def circle(c, E1):
colors = ['red', 'blue', 'green', 'yellow', 'black', 'purple', 'pink']
color_ui = E1.getText
for i in colors:
if color_ui == i:
return i
return -1
main()
Screenshot:
Output: