Python 如何为我的转换创建函数
这些是我的项目说明 为转换定义不同的函数。调用这些函数 我已经用输入框和文本框创建了我的窗口。我有一个函数,但我知道应该使用Python 如何为我的转换创建函数,python,zelle-graphics,Python,Zelle Graphics,这些是我的项目说明 为转换定义不同的函数。调用这些函数 我已经用输入框和文本框创建了我的窗口。我有一个函数,但我知道应该使用setText()在文本框中显示计算结果。我想我需要对“无效输入”部分使用一个语句 我想在我创建的文本框中显示克、千克和盎司的换算值,但我似乎无法理解 *编辑我现在可以在窗口中显示我的转换。我的问题是让我的关闭按钮工作。如果我将其置于True之前,则用户必须双击才能转换。我已经更新了上面的代码 我想在文本中显示克、千克和盎司的换算 我创建的盒子 问题是,除了从未真正调用po
setText()
在文本框中显示计算结果。我想我需要对“无效输入”部分使用一个语句
我想在我创建的文本框中显示克、千克和盎司的换算值,但我似乎无法理解
*编辑我现在可以在窗口中显示我的转换。我的问题是让我的关闭按钮工作。如果我将其置于True之前,则用户必须双击才能转换。我已经更新了上面的代码
我想在文本中显示克、千克和盎司的换算
我创建的盒子
问题是,除了从未真正调用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()
现在,您需要遵循以下任务要求:处理用户输入的不是数字的内容;为转换定义不同的函数,而不是一个通用函数。“退出”按钮将是一个不错的补充。而且,让GUI更令人愉快也不会有什么坏处
非常感谢您的澄清。我已经更新了代码并更改了整体外观。现在我有一个关闭按钮的问题。由于某种原因,如果用户不单击两次进行转换,我就无法使其正常工作。@pangie,因为您没有提供完整的代码,我只能猜测。我认为检查关闭按钮是否被按下的代码应该在
while
循环中,而不是在它之前。也就是说,您应该能够执行multpleConvert
运行,然后点击Close
——这是同一个循环的所有部分。如果这仍然是一个问题,请使用完整的代码为此问题开始一个新的OP问题。
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()