The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Im Stuck In Python
Discuss Im Stuck In Python in the Python Programming forum on Dev Shed. Im Stuck In Python Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 28th, 2012, 10:42 AM
|
|
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.
I already have this much..
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)
Text(Point(50,60), "Answer:").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..
|

November 28th, 2012, 10:53 AM
|
 |
Contributing User
|
|
Join Date: May 2012
Location: 39N 104.28W
Posts: 90
Time spent in forums: 1 Day 13 h 37 m 44 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?
|

November 28th, 2012, 12:02 PM
|
|
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?
|

November 28th, 2012, 12:28 PM
|
 |
Contributing User
|
|
|
|
__________________
[code] Code tags[/code] are essential for python code!
|

November 28th, 2012, 12:34 PM
|
|
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
|

November 28th, 2012, 12:51 PM
|
 |
Contributing User
|
|
|
|
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!
|

November 28th, 2012, 12:57 PM
|
|
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
|

November 28th, 2012, 01:02 PM
|
|
Contributing User
|
|
Join Date: May 2009
Posts: 291
  
Time spent in forums: 3 Days 18 h 19 m 46 sec
Reputation Power: 6
|
|
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 01:14 PM.
|

November 28th, 2012, 01:13 PM
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|