Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 28th, 2012, 11:42 AM
 Chrianna286
Registered User

Join Date: Nov 2012
Posts: 9
Time spent in forums: 2 h 42 m 52 sec
Reputation 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
November 28th, 2012, 11:53 AM
 rrashkin
Contributing User

Join Date: May 2012
Location: 39N 104.28W
Posts: 142
Time spent in forums: 2 Days 1 h 3 sec
Reputation Power: 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
November 28th, 2012, 01:02 PM
 Chrianna286
Registered User

Join Date: Nov 2012
Posts: 9
Time spent in forums: 2 h 42 m 52 sec
Reputation 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
November 28th, 2012, 01:28 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
tkinter.mainloop() handles this problem.
__________________
[code]Code tags[/code] are essential for python code!

#5
November 28th, 2012, 01:34 PM
 Chrianna286
Registered User

Join Date: Nov 2012
Posts: 9
Time spent in forums: 2 h 42 m 52 sec
Reputation Power: 0
i would use that but i cant... i have to use the rectangles

#6
November 28th, 2012, 01:51 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
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 01:52 PM. Reason: fix my brackets!

#7
November 28th, 2012, 01:57 PM
 Chrianna286
Registered User

Join Date: Nov 2012
Posts: 9
Time spent in forums: 2 h 42 m 52 sec
Reputation Power: 0
okay thanks for the help

#8
November 28th, 2012, 02:02 PM
 dwblas
Contributing User

Join Date: May 2009
Posts: 423
Time spent in forums: 5 Days 8 h 10 m 44 sec
Reputation Power: 32
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
Quote:
 "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 02:14 PM.

#9
November 28th, 2012, 02:13 PM
 Chrianna286
Registered User

Join Date: Nov 2012
Posts: 9
Time spent in forums: 2 h 42 m 52 sec
Reputation 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

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Im Stuck In Python