These are the instructions for my project.
Define different functions for the conversions. Call these functions.
I have created my window with the entry box and text boxes. I have a function but I know I should use setText()
to display the calculations in my text boxes. And I think I need to use an statement for the "invalid input" part.
entry1= Entry(Point(win.getWidth()/2,100),25)
entry1.setFill("White")
entry1.draw(win)
gramstext= Text(Point(160,136), "Grams")
gramstext.setTextColor("black")
gramstext.draw(win)
gramsbox= Rectangle(Point(107,147),Point(294,175))
gramsbox.setFill("white")
gramsbox.draw(win)
kilotext= Text(Point(160,195), "Kilograms")
kilotext.setTextColor("black")
kilotext.draw(win)
kilobox= Rectangle(Point(107,207),Point(294,235))
kilobox.setFill("white")
kilobox.draw(win)
ouncetext= Text(Point(160, 250), "Ounces")
ouncetext.setTextColor("black")
ouncetext.draw(win)
ouncebox= Rectangle(Point(107, 262),Point(294,290))
ouncebox.setFill("white")
ouncebox.draw(win)
#From here on is the updated code
button1= Rectangle(Point(142,290),Point(206,310))
button1.setOutline("black")
button1.setFill("white")
button1.draw(win)
button= Text(Point(win.getWidth()/3.5,300),"Convert")
button.setOutline("black")
button.draw(win)
closebutton1= Rectangle(Point(362, 290),Point(438,310))
closebutton1.setOutline("black")
closebutton1.setFill("white")
closebutton1.draw(win)
closebutton = Text(Point(win.getWidth() / 1.5, 300), "Close")
closebutton.setOutline("black")
closebutton.draw(win)
Iwant my conversions for grams, kg and ounces to display in the text boxes that I created, but I can't seem to figure it out.
*EDIT I am now able to display my conversions in the window. My issue is getting my close button to work. If I put it before While True , User has to double click to convert. I have updated the code above.
I want my conversions for grams, kg and ounces to display in the text boxes that I created
The problem is, along with never actually calling poundsTo()
, you didn't create text boxes, you created rectangles which don't respond to a setText()
method. I've reworked and cleaned up your code below to get it to basically function:
from graphics import *
def poundsTo():
grams = number * 452.592
kgrams = number / 0.453592
ounces = number * 16
gramsbox.setText(grams)
kilobox.setText(kgrams)
ouncebox.setText(ounces)
win = GraphWin("Conversions", 600, 400)
poundstext = Text(Point(160, 50), "Pounds:")
poundstext.draw(win)
poundsbox = Entry(Point(win.getWidth() / 2, 50), 25)
poundsbox.setFill("White")
poundsbox.draw(win)
gramstext = Text(Point(160, 100), "Grams:")
gramstext.draw(win)
gramsbox = Text(Point(win.getWidth() / 2, 100), "")
gramsbox.draw(win)
kilotext = Text(Point(160, 150), "Kilograms:")
kilotext.draw(win)
kilobox = Text(Point(win.getWidth() / 2, 150), "")
kilobox.draw(win)
ouncetext = Text(Point(160, 200), "Ounces:")
ouncetext.draw(win)
ouncebox = Text(Point(win.getWidth() / 2, 200), "")
ouncebox.draw(win)
button = Text(Point(win.getWidth() / 2, 300), "Convert")
button.draw(win)
while True:
win.getMouse()
number = int(poundsbox.getText())
poundsTo()
Now you need to follow the assignment requirements about: dealing with the user entering something that's not a number; defining different functions for the conversions, not one common one. An "Exit" button would be a nice addition. Also, making the GUI more pleasant wouldn't hurt either...