如何为我的转化创建函数
How to create a function for my conversions
这些是我的项目的说明。
Define different functions for the conversions. Call these functions.
我已经创建了带有输入框和文本框的 window。我有一个函数,但我知道我应该使用 setText()
在我的文本框中显示计算结果。而且我想我需要为 "invalid input" 部分使用一个语句。
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)
我希望在我创建的文本框中显示克、公斤和盎司的换算结果,但我似乎无法弄明白。
*EDIT 我现在可以在 window 中显示我的转化。我的问题是让我的关闭按钮工作。如果我把它放在 While True 之前,用户必须双击才能转换。我已经更新了上面的代码。
I want my conversions for grams, kg and ounces to display in the text
boxes that I created
问题是,除了从未实际调用 poundsTo()
,您没有创建 文本框 ,您创建了 矩形 不响应 setText()
方法。我已经修改并清理了下面的代码以使其基本正常运行:
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()
现在您需要遵循以下任务要求:处理用户输入非数字的内容;为转换定义不同的函数,而不是一个常见的函数。 "Exit" 按钮会是一个很好的补充。此外,使 GUI 更令人愉悦也无妨...
这些是我的项目的说明。
Define different functions for the conversions. Call these functions.
我已经创建了带有输入框和文本框的 window。我有一个函数,但我知道我应该使用 setText()
在我的文本框中显示计算结果。而且我想我需要为 "invalid input" 部分使用一个语句。
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)
我希望在我创建的文本框中显示克、公斤和盎司的换算结果,但我似乎无法弄明白。
*EDIT 我现在可以在 window 中显示我的转化。我的问题是让我的关闭按钮工作。如果我把它放在 While True 之前,用户必须双击才能转换。我已经更新了上面的代码。
I want my conversions for grams, kg and ounces to display in the text boxes that I created
问题是,除了从未实际调用 poundsTo()
,您没有创建 文本框 ,您创建了 矩形 不响应 setText()
方法。我已经修改并清理了下面的代码以使其基本正常运行:
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()
现在您需要遵循以下任务要求:处理用户输入非数字的内容;为转换定义不同的函数,而不是一个常见的函数。 "Exit" 按钮会是一个很好的补充。此外,使 GUI 更令人愉悦也无妨...