Hello guys, I really need help with this one. Can you please help me create a program in Python that displays information about a rectangle of any size.
Input: two mouse clicks for the opposite corners of a rectangle
Output: Draw the rectangle. Print the perimeter and area of the rectangle
Formulas: Area = (length)(width)
Perimeter =2(length+width)

Please help I just started using Python,

Recommended Answers

All 4 Replies

We can help you if you make a start on the code to show your commited to your project :)
I would recommend that you look into using a program called PyGame. Start of making some basic programs such as how to open a window with PyGame and then how to draw a rectangle. Then you can use PyGame to detect the mouse position and pass it to the functions. PyGame has some excellent documentation pages:

from graphics import *

def main():

    win.setCoords(0.0, 0.0, 10.0, 10.0)
    message = Text (Point(5, 0.5), "Click on two points")

    #Get and draw 4 vertices of rectangle
    p1 = win.getMouse()
    p2 = win.getMouse()
    p3 = win.getMouse()
    p4 = win.getMouse()

# Used Polygon object to draw the rectangle
    rectangle = Polygon(p1, p2, p3, p4)

    # Wait for another click to exit
    message.setText("Click anywhere to exit.")


I have this but I dont know how to code it so that it works with only two clicks, 4 works but not 2.

I have this but I dont know how to code it so that it works with only two clicks, 4 works but not 2.

There is a line missing somewhere, such as

    win = GraphWin("My Rectangle", 300, 300)

Apart from that, this is good python code

You can do it with 2 clicks if you realize that 2 points give you 2 values for x and 2 values for y. With these values, you can build 4 different points and draw the rectangle.

As this thread is 3 years old and Fo.katia is probably not waiting for an answer, you could write the complete running solution with 2 clicks in a code snippet. (select the type Code Snippet after you click Start a new discussion).

You can do it if the two points are at the ends of the diagonal.
See ...

If you use the Tkinter GUI toolkit, the two points you need to create a rectangle are the upper left corner and lower right corner.

Be a part of the DaniWeb community

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