### Thread: Im Stuck In Python

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
9
Rep Power
0

#### Im Stuck In Python

i need help asap or some feedback!!
I have no idea how to go on.. im stuck

I have to write a graphics program that simulates a calculator. Which ive already done. The window that the user sees must have a label and an area for the first operand, the second operand, and the result. The user needs to be able to choose whether to add, subtract, multiply, and divide the operands to produce the result by clicking on the appropriate button. The operation will only occur when the user clicks in the box labeled with that operation.The result will be updated as long as the user continues to choose an operation. The result will be displayed to two decimal point accuracy.

from graphics import *

def main():
#Create graphics window
win = GraphWin("Mathemathics", 400, 400)

#Make the background grey
win.setBackground("grey")

#Set Coordinates
win.setCoords(0, 0, 90, 100)

r1 = Rectangle(Point(5,5), Point(20,20))
r1.draw(win)

r2 = r1.clone()
r2.move(20,0)
r2.draw(win)

r3 = r1.clone()
r3.move(40,0)
r3.draw(win)

r4 = r1.clone()
r4.move(60,0)
r4.draw(win)

Text(Point(12.5,12.5), "+").draw(win)
Text(Point(32,12.5), "-").draw(win)
Text(Point(52,12), "*").draw(win)
Text(Point(72,12.5), "/").draw(win)

Text(Point(15,80), "First Number:").draw(win)
e1 = Entry(Point(20,75), 8)
e1.draw(win)

Text(Point(15,60), "Second Number:").draw(win)
e2 = Entry(Point(20,55), 8)
e2.draw(win)

win.getMouse()
win.close()
main()

How would I make a while loop so that when the user clicks between the points of the add rectantgle that it adds the two entry numbers..? and etc..
2. I'm not at all familiar with any module named "graphics" so I can't really help you with any specifics (now, Tkinter or pyGTK would be another kettle of horses of a different color).

That said, let's imagine that when the user clicks anywhere on the window, "win", an event is generated. You don't have any "listeners" created so I'm guessing the "GraphWin" constructor takes care of that. Even so, when the click event happens, there should be some attributes associated with pointer position that can be accessed. They would be like %x and %y or something. You would want to check if %x is between 5 and 20 AND if %y is between 5 and 20 (based on what I infer are the coordinates of the "add" or "+" rectangle: r1 = Rectangle(Point(5,5), Point(20,20)).

My question to you is why do you think you want to use a while loop here?
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
9
Rep Power
0
I want to do a while loop because i want the user to be able to click add, subtract, multipy, or divide as many times as they want until they click exit. Do you know how to do that?
4. tkinter.mainloop() handles this problem.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
9
Rep Power
0
i would use that but i cant... i have to use the rectangles
6. The graphics module is here, and it's based on tkinter, so you really could use
tk.Button(_root,text='click me',command=my_callback_function)
and _root.update() or _root.mainloop()
but thanks, like rrashkin, I'm not going to support the graphics module.
Last edited by b49P23TIvg; November 28th, 2012 at 12:52 PM. Reason: fix my brackets!
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
9
Rep Power
0
okay thanks for the help
8. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
May 2009
Posts
509
Rep Power
33
I would guess that the name "getMouse()" was chosen for a reason. The docs for the Graphics module (I Googled "graphics module documentation") say this
"getMouse() Pauses for the user to click a mouse in the window and returns where the mouse was clicked as a Point object."
You would then have to get the x and y values from the Point object (i.e. an instance of the Point class), how you do this should also be in the docs, and then test for if/which rectangle the mouse was in.
Last edited by dwblas; November 28th, 2012 at 01:14 PM.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
9
Rep Power
0
I understand what i have to do. I just cant figure out how to put it in Code form is what i need help with