I have to creat a program that draws squares instead of circles. It also has to draw additional squares for each mouse click, and at the end print a message "Click again to quit" after the loop, and wait for a final clikc before closing the window. This is what I have so far, but I think there is something wrong, because instead of drawing circles it stays on the same place. Please help, thank you.

def main():
win = GraphWin()
shape= Rectangle(Point(300,300),Point(250,250))
shape.setOutline("red")
shape. setFill("red")
shape.draw(win)
for i in range(10):
p=win.getMouse()
c=shape.getCenter()
dx=p.getX()-c.getX()
dy=p.getY()-c.getY()
shape.move(dx,dy)
message = Text(Point(0, 0, "Click anywhere to Quit")
message.draw(win)
win.close()
main()

Recommended Answers

All 2 Replies

Maybe I am getting old, but I do not see any circles nor any Python indention (with code tags).

You may want to modify something like that ...

# using the Zelle graphics module (derived from Tkinter)
# http://mcsp.wartburg.edu/zelle/python/graphics.py

from graphics import *

def main():
    w = 350
    h = 250
    win = GraphWin("Click to move shape (10 clicks max)", w, h)    
    #shape = Rectangle(Point(200,200),Point(250,250))
    shape = Circle(Point(100,100), 20)
    shape.setOutline("red")
    shape. setFill("red")
    shape.draw(win)
    for k in range(10):
        message = Text(Point(40, 10), "clicks = %s" % (k+1))
        message.draw(win)
        p = win.getMouse()
        c = shape.getCenter()
        dx = p.getX() - c.getX()
        dy = p.getY() - c.getY()
        shape.move(dx,dy)
        # reset text
        message.setText("")

    win.getMouse()
    win.close()

main()
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.