|
Advertisement
|
|
Advertisement
No logs - Anonymous IP
|
 |

05-04-2012, 05:56 AM
|
 |
Acolyte
|
|
Join Date: Jan 2009
Location: PNW
Thanks: 56
Thanked 706 Times in 493 Posts
|
|
Simple Python program help
This program is from a book (same on website below) in class and is using the graphics.py library:
http://mcsp.wartburg.edu/zelle/python/
Now the problem is, like many in "educational" books, is the actual hands-on projects hardly relate to any damn thing you read in the chapter. Basically everything is set up the way it is supposed to if you build it and based off what partial examples were found in the book that I could relate to, a two input problem, I assume what I've done so far is correct.
However, when it comes to following the books example at the bottom where it says output.setText(principal) should calculate the previous inputs I saved in the variables "principal" and "interest" where below that shows the new principal calculated behind the scenes.
I mean, the shit worked when in there example it was only one input box, not two like the fucking book wants you to do without explaining jack and shit as to the syntax regarding more than one. I'm sure this is dead ass simple and I'm just over-thinking it, but from my logic I just can't see what the hell is going wrong.
Take to mind this is a very intro course and we just finished discussing loops which for fuck alls sake I think we could spend a quarter with until people actually got comfortable, but that's besides the point. Any help would be appreciated and if possible keeping it as simple as you see as I don't want to pull some magic out of my ass that we definitely aren't working with from a newbies perspective.
Code:
# Program will give two user input boxes to enter values. Calculate those values
# and present the resulting calculation below based off principal and interest
from graphics import *
def main():
win = GraphWin("Investment Growth Chart", 320, 240)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# Draw the interface
Text(Point(1,3), "Enter the principal:").draw(win)
Text(Point(1,1), "Annual interest rate:").draw(win)
input = Entry(Point(2,3),5)
input.setText("0.0")
input.draw(win)
# Get the top entry box for Principal and saving it to variable "principal"
principal = eval(input.getText())
input = Entry(Point(2,1),5)
input.setText(".0")
input.draw(win)
# Get Interest and Calculate the new Principal to be shown in output.setText() below
interest = eval(input.getText())
principal = principal * (1 + interest)
button = Text(Point(1.5,2.0),"Calculate")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
Text(Point(1,.5), "New Total:").draw(win)
output = Text(Point(2.0,.5), "0.0")
output.draw(win)
# wait for mouse click
win.getMouse()
# Display output and change the button
output.setText(principal)
button.setText("Quit")
# Everything works up to this point except in the book output.setText(principal) did a calculation based off a formula behind the scenes and a value of one input. Here I'm trying to do it based off two inputs and it does nothing
# wait for click and then quit
win.getMouse()
win.close()
main()
The second image it should show the calculated "New Total" but it just stays 0.0 as I set it to default show in the script before doing anything instead of updating to the two previous inputs being calculated.
Someone save this fucking n00b. I spent 4 hours trying to find the stupid tiny thing I fucked up, but it brings comfort knowing my professor who apparently coded for 20 years couldn't find out the problem after sitting down with me for 10 minutes. If I can't find any solution by Sunday I'm just gonna say fuck it and turn it in regardless.
__________________
http://www.whatreallyhappened.com
U.S. #1 Financier of Terrorism
|

05-04-2012, 06:38 AM
|
|
Wealthy Merchant
|
|
Join Date: Mar 2009
Location: United States
Thanks: 45
Thanked 92 Times in 74 Posts
|
|
Re: Hai
From a similar nightmare I experienced when learning GUI programming, I might think that the changes DID occur, but you're not drawing them to the window and thus will keep showing 0.0. I pasted your entire main definition below (the one addition has been bolded near the bottom):
Code:
def main():
win = GraphWin("Investment Growth Chart", 320, 240)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# Draw the interface
Text(Point(1,3), "Enter the principal:").draw(win)
Text(Point(1,1), "Annual interest rate:").draw(win)
input = Entry(Point(2,3),5)
input.setText("0.0")
input.draw(win)
# Get the top entry box for Principal and saving it to variable "principal"
principal = eval(input.getText())
input = Entry(Point(2,1),5)
input.setText(".0")
input.draw(win)
# Get Interest and Calculate the new Principal to be shown in output.setText() below
interest = eval(input.getText())
principal = principal * (1 + interest)
button = Text(Point(1.5,2.0),"Calculate")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
Text(Point(1,.5), "New Total:").draw(win)
output = Text(Point(2.0,.5), "0.0")
output.draw(win)
# wait for mouse click
win.getMouse()
# Display output and change the button
output.setText(principal)
button.setText("Quit")
#try redrawing output since its value has changed
output.draw(win)
# wait for click and then quit
win.getMouse()
win.close()
If this works, welcome to the world of GUI (this particular issue is not python specific).
__________________
Bai
|
|
The following users say "It is so good to hear it!":
|
|

05-04-2012, 07:15 AM
|
 |
Acolyte
|
|
Join Date: Jan 2009
Location: PNW
Thanks: 56
Thanked 706 Times in 493 Posts
|
|
Re: Simple Python program help
LOL, I think I tried that and about a million other things and it never seems to work. I didn't have my hopes up for success and in return wasn't disappointed when I was greeted with:
Code:
>>>
Traceback (most recent call last):
File "C:/Users/Admin/Documents/test.py", line 47, in <module>
main()
File "C:/Users/Admin/Documents/test.py", line 41, in main
output.draw(win)
File "C:\Python31\lib\site-packages\graphics.py", line 398, in draw
if self.canvas and not self.canvas.isClosed(): raise GraphicsError(OBJ_ALREADY_DRAWN)
graphics.GraphicsError: Object currently drawn
>>>
I don't even understand the errors much less half of what this chapter is talking about. Honestly I give props to those who do programming because this is just a brain drain. GUI definitely seems to complicate things and I think just adding fuel to my anger at this point. Shouldn't be this hard when it's meant for novices. I just really can't see what is missing and you'd think that would work what you suggested. Instead Python is shitting on me like a flock of birds flying over head targeting your car.
Thanks for the quick reply and try though. I'll take any more takers or retries if they're up for the challenge lol.
*EDIT*
I think you're on the right track though. If I remove:
output = Text(Point(2.0,.5), "0.0")
output.draw(win)
and use your output.draw(win) instead and then set the first line to output = Text(Point(2.0,.5), "") < quotes instead of setting it as a default value. When I get to the end I did a little test to see if output.setText(principal) which normally would calculate the input saved to variables and display to the screen. Instead I manually typed in the values output.setText(2500 * .03) and it finally would output the actually result. So I'm thinking that the way I have the input boxes set up now it isn't actually grabbing them and saving them to separate variables because if I leave it how it is it'll just keep showing 0.0 I think mimicking the principal field. Is the second input. for the Interest Rate not registering? I'm guessing it might be interfering because I have two separate "input" calls and the program can only register one as a variable.
So I'm thinking that even though "input" as the book defines would grab the text entered in the field, because I have two somehow it's causing conflict but the goddamn book doesn't do any examples except with using the input.get field for one text box not two.
Fuck my life.
__________________
http://www.whatreallyhappened.com
U.S. #1 Financier of Terrorism
Last edited by LSA King; 05-04-2012 at 07:38 AM.
|

05-04-2012, 10:40 PM
|
|
Wealthy Merchant
|
|
Join Date: Mar 2009
Location: United States
Thanks: 45
Thanked 92 Times in 74 Posts
|
|
Re: Hai
I read the documentation on it and have fixed your issue.
The initial problem I laid out is not happening as your graphics library is set to auto update the window if any of its objects are updated. It does this automatically (how convenient...).
What you wrote after my post is actually right on the problem. However, this is the result of TWO problems, and they're both related to Program Flow:
- The program is currently designed to IMMEDIATELY assign principal and interest variables the text from the boxes. There is no way a human can enter the text before the data gets assigned. 0.0 sound familar?
I have altered the code to wait at the correct time for user input before continuing on with variable assignment and calculation.
- You use one input object for two inputs. While I bet you CAN make it work, it probably won't be worth it. I have separated this variable into two new input objects: principalInput and interestInput. This way the interest input won't override the principal input after the button press.
Also, I noticed that your buttons are not actually buttons. I made them more into buttons. The program will check to see if the mouse press was in the actual area of the button!
I've included the sys library because that way you can dump whatever info you want to stdout as shown in the modified code. This makes it MUCH easier to look into problems with GUIs.
Full modified code:
Code:
# Program will give two user input boxes to enter values. Calculate those values
# and present the resulting calculation below based off principal and interest
from graphics import *
import sys
def main():
win = GraphWin("Investment Growth Chart", 320, 240)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# Draw the interface
Text(Point(1,3), "Enter the principal:").draw(win)
Text(Point(1,1), "Annual interest rate:").draw(win)
# HIGUY - Draw the button up here so it shows
button = Text(Point(1.5,2.0),"Calculate")
button.draw(win)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# HIGUY - Rename input to principalInput so it uniquely covers just the principal input. No having to worry about it being overridden.
principalInput = Entry(Point(2,3),5)
principalInput.setText("0.0")
principalInput.draw(win)
# Get the top entry box for Principal and saving it to variable "principal"
# HIGUY - Create new input INSTEAD OF REUSING THE EXISTING ONE just for interest
interestInput = Entry(Point(2,1),5)
interestInput.setText(".0")
interestInput.draw(win)
# HIGUY - Now we'll create an infinite loop to hold up the program until the mouse is pressed AND if the mouse press is in a certain area (the 'button').
# Also, it's bad practice to use these 'magic numbers' (hardcoded). These should be stored in a variable or a button class!
while True:
clickLocation = win.getMouse()
if clickLocation.getX()>=1 and clickLocation.getX()<=2:
if clickLocation.getY() >= 1.5 and clickLocation.getY() <= 2.5:
break
# Get Interest and Calculate the new Principal to be shown in output.setText() below
# HIGUY - I moved assigning the principal variable down here.
principal = eval(principalInput.getText())
interest = eval(interestInput.getText())
# HIGUY - The reason the above is just after the button press is because this way the user actually has time to put in a value before the program grabs it
principal = principal * (1 + interest)
Text(Point(1,.5), "New Total:").draw(win)
output = Text(Point(2.0,.5), "0.0")
output.draw(win)
# HIGUY - Example debug stuff
sys.stdout.write("principal variable: "+str(principal) + "\n"); #need to convert numbers into string objects
sys.stdout.write("interest variable: " + str(principal) + "\n");
# wait for mouse click
# win.getMouse()
# Display output and change the button
output.setText(principal)
button.setText("Quit")
# wait for click and then quit
# HIGUY - copypasta time! Loop infinitely until a valid mouse click is reached.
while True:
clickLocation = win.getMouse()
if clickLocation.getX()>=1 and clickLocation.getX()<=2:
if clickLocation.getY() >= 1.5 and clickLocation.getY() <= 2.5:
break
win.close()
main()
I still can't comment to save my life, so let me know if something isn't clear
__________________
Bai
Last edited by Hi-Guy; 05-04-2012 at 10:42 PM.
|
|
The following users say "It is so good to hear it!":
|
|

05-04-2012, 11:23 PM
|
 |
Acolyte
|
|
Join Date: Jan 2009
Location: PNW
Thanks: 56
Thanked 706 Times in 493 Posts
|
|
Re: Simple Python program help
You........are a fucking GOD! I'll have to pour over that code (I see you cleaned some things up and organized it better, thanks. I was going to do that myself but this Chapter just started on the GUI and prior to that placement was always a big deal, didn't want to further break what I had lol.
+1 on the documentation, I'll have to save it and pour over it until I get what exactly is going on line by line. The thing is, we never in-class or in the book went over looping in the GUI (intro chapter) or doing the import sys library. Still kind of new but it seems the sys library gives some balls into manipulation in the way I can only relate to from my Unix class last quarter with SED/AWK. I'm not really concerned since I'll just explain to him why I used something that wasn't discussed since he couldn't figure out what was wrong in-class with what I had.
The book didn't go over two inputs whatsoever and I wasn't sure if I could change the two text boxes just because the original "input" that I used for each went blue which usually means it's a built in function in Python as far as this n00b can tell.
Also the button was simply meant as an illusion to force the user to click the mouse in order for it to calculate, not an actual button as even the books example clarified because it was going over intro level GUI stuff. Still, you fixed that lol.
I was seriously going to go insane if I didn't know what was wrong and at this point I didn't even care if I got marked down for it. All those hours of trying drove me mad. It really is a goddamn shame books fail in so many areas when they ask you to do "examples" that hardly relate to what they taught you up to that point, and Instructors who should be there to go in-depth and explain things can't or are unable to themselves. I think that really just turns people like me who were at one point interested in learning to program, completely the fuck off from doing so.
Again, thanks a ton and hopefully I don't have to come back with such silly ass problems any time soon.
__________________
http://www.whatreallyhappened.com
U.S. #1 Financier of Terrorism
|
 |
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
|
|
|
All times are GMT. The time now is 10:14 AM.
|
|
Hot Topics |
| | | | | | | | | | | | | | |
On IRC |
Users: 4
Messages/minute: 0
Topic: "http://www.zoklet.net/..."
|
Users: 22
Messages/minute: 0
Topic: "buttpee"
|
Users: 10
Messages/minute: 0
Topic: "11:37 < mib_i8mfin> so wie ich die website hier sehe las..."
|
Advertisements |
|
Your ad could go right HERE! Contact us!
|
|