atlast I have managed to get some working code but the first problem is that I have is taht.. when I call draw(2,4) I have a column of 2 circles and 4 rows and I would actually like it to be a clomun of 2 and row of 4. Secodly although my code works I have a main function and an input for an colour how could I fill the circles with the requested colour?

like
This is what i currently have
00
00
00
00

I want it like

0000
0000

from graphics import *
 
def main():
    colour = raw_input("Enter the colour: ")
 
def drawCircle(win, centre, radius, colour, color):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setOutline(color)
    circle.draw(win)
 
def drawWindow(win, center, radius):
    drawCircle(win, center, radius, "white", "black")
 
 
def draw(rows, columns):
      radius = 50
      diameter = 2 * radius
      width = rows*diameter+1
      height = columns*diameter+1
      win = GraphWin("", width, height )
      for x in range( rows ):
         for y in range( columns ):
            xPosition = radius + 2 + diameter * x
            yPosition = radius + 2 + diameter * y
            center = Point( xPosition, yPosition )
            drawWindow( win, center, radius )
 
 
main()

Dani AI

Generated

Two problems are happening at once: the loop indices are being used the wrong way for x/y placement (so your grid looks transposed), and the colour value is read but never passed down to the drawing routine. was right about passing the value as a parameter rather than trying to shoehorn it into a local inside drawCircle. Below is a concise pattern that fixes both issues and shows how to call things from main().

from graphics import GraphWin, Circle, Point

def draw_grid(rows, cols, fill_color, outline='black', radius=20, gap=6):
    diameter = radius * 2
    spacing = diameter + gap
    margin = gap
    width = cols * spacing + margin * 2
    height = rows * spacing + margin * 2
    win = GraphWin("Circles", width, height)

    for row in range(rows):            # row -> vertical index
        for col in range(cols):        # col -> horizontal index
            x = margin + radius + col * spacing
            y = margin + radius + row * spacing
            c = Circle(Point(x, y), radius)
            c.setFill(fill_color)
            c.setOutline(outline)
            c.draw(win)

    win.getMouse()
    win.close()

def main():
    rows = int(raw_input("Rows: "))
    cols  = int(raw_input("Columns: "))
    color = raw_input("Fill colour (name): ")
    draw_grid(rows, cols, color)

Key points: rows correspond to vertical positions (y), columns to horizontal (x); compute window width from columns and height from rows; compute each circle center as margin + radius + index * spacing so circles don't overlap or clip. Read input in main() and pass it into draw_grid()—avoid trying to return the colour from a circle-drawing function.

Troubleshooting: cast inputs to int and guard with try/except if needed; for Python 3 replace raw_input() with input(); drawWindow is unnecessary—use one well-named draw function. If circles clip at edges, reduce radius or increase gap/margin. This addresses 's layout and colouring problems while following 's advice about parameter passing.

Recommended Answers

All 8 Replies

Something strange in your code. It would only input one string and not using the value. Figure out where it should be used, pass the value to function in the parameters.

Something strange in your code. It would only input one string and not using the value. Figure out where it should be used, pass the value to function in the parameters.

ok and any help about displaying the circles in rows and columns?

You obviously use the loop variables in opposite meaning than you planned. rows are columns and columns are rows. Correct that.

something like this?

def main():
    colr = raw_input("Enter the colour: ")
 
def drawCircle(win, centre, radius, colour, color):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setOutline(color)
    circle.draw(win)
 
def drawWindow(win, center, radius):
    drawCircle(win, center, radius, colr, "black")
 
 
def draw(rows, columns):
      colour=colr

No you have colr as local variable in function not as parameter. Main must call drawing routine with colr as added parameter.

Also drawWindow function serves no purpose. I would name parameters color and colour more descriptively.

so do you mean this.. but how could I get main to call this function?

def drawCircle(win, centre, theColour, radius, colour, color):
    circle = Circle(centre, radius)
    theColour = raw_input("Enter the colour: ")
    circle.setFill(colour)
    circle.setOutline(color)
    circle.draw(win)

 
 
def draw(rows, columns):
      colour=theColour

You have thcColour in parameter, but you are not getting the value of that before calling the function with user input as parameter.

This is how you call function:

def prod(a,b):
    return a*b

a= float(raw_input('Enter a: '))
b= float(raw_input('Enter b: '))
print 'The product is: ',prod(a, b)

You have thcColour in parameter, but you are not getting the value of that before calling the function with user input as parameter.

This is how you call function:

def prod(a,b):
    return a*b

a= float(raw_input('Enter a: '))
b= float(raw_input('Enter b: '))
print 'The product is: ',prod(a, b)

I believe that the above is an example and does not fully apply to my code, I mean the multiplication of a and b. I have returned the "theColour" and then user inputs it and then colour= theColour?

def drawCircle(win, centre, theColour, radius, colour, color):
      circle = Circle(centre, radius)
      return theColour
      theColour = raw_input("Enter the colour: ")
      circle.setFill(colour)
      circle.setOutline(color)
      circle.draw(win)
 


      def draw(rows, columns):
      colour=theColour
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.