Can someone help me with this code.. I get an error when I try to execute it

from graphics import *
 
def main():
    colour = raw_input("Enter the colour: ")
    win = GraphWin("Patch", 200, 200)
    drawCircle(win, 50, 50, colour)

def drawCircle(win, x, y, colour):
    for i in range(5):
        for j in range(5):
            if (i + j) % 2 == 0:
                topLeftX = x + i * 20
                topLeftY = y + j * 20
                circle = Circle(Point(topLeftX, topLeftY),
                                      Point(topLeftX + 20, topLeftY + 20))
                circle.setFill(colour)
                circle.draw(win)
                
main()

Dani AI

Generated

— the problem is that Circle() in the Zelle/graphics.py library expects a center point plus a numeric radius, not two corner points. As noted, passing a second Point makes the library treat that object as the radius and leads to a TypeError when it tries to do numeric math with it.

Fix: compute the circle center and a numeric radius from your 20×20 tile, then call Circle(center_point, radius). For example:

cell = 20
radius = cell / 2.0
topLeftX = x + i * cell
topLeftY = y + j * cell
center = Point(topLeftX + radius, topLeftY + radius)
circle = Circle(center, radius)
circle.setFill(colour)
circle.draw(win)

Notes and troubleshooting

  • If you really want to define a shape with two corner points, use Oval(Point1, Point2) — that function accepts two opposite corners.
  • If the bounding box might not be square, pick radius = min(width, height) / 2.0 so the circle fits.
  • To debug types/values, temporarily print topLeftX, topLeftY, radius, type(radius) (or use print() in Python 3).
  • If you run this on Python 3, use input() instead of raw_input().

Small extras often forgotten: add win.getMouse() (or a pause) and then win.close() so the window stays visible after drawing. Thanks to for prompting the error output and to for spotting the constructor mismatch — the corrected center/radius approach above will remove the TypeError.

Recommended Answers

All 3 Replies

What error do you get?

What error do you get?

Traceback (most recent call last):
File "C:/Documents and Settings/Compaq_Owner/Desktop/0", line 19, in <module>
main()
File "C:/Documents and Settings/Compaq_Owner/Desktop/0", line 6, in main
drawCircle(win, 50, 50, colour)
File "C:/Documents and Settings/Compaq_Owner/Desktop/0", line 15, in drawCircle
Point(topLeftX + 20, topLeftY + 20))
File "C:\Python26\lib\site-packages\", line 611, in __init__
p1 = Point(center.x-radius, center.y-radius)
TypeError: unsupported operand type(s) for -: 'int' and 'instance'

The correct arguments for Circle() are (center_point, radius) and you gave two points.

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.