- 综合讨论区
- 帖子详情
可以多次转换的温度转换程序
itkeeper
发表于2017年05月03日
<p>拷贝老师给的温度转换程序,我已经注释掉了点击事件,退出程序的代码,只能转换一次,然后就没有反应了。</p><p><br ></p><p>我给改了一下,用一个while死循环,可以做到无数次的转换。可是关掉窗口后,Python Shell报错:</p><p>in getMouse</p><p> if self.isClosed(): raise GraphicsError("getMouse in closed window")</p><p>graphics.GraphicsError: getMouse in closed window</p><p><br ></p><p><code class="brush:python;toolbar:false" >from graphics import *
def convert(input):
celsius = eval(input.getText()) # 输入转换
fahrenheit = 9.0/5.0 * celsius + 32
return fahrenheit
def colorChange(win,input):
cnum = eval(input.getText())
weight = cnum / 100.0
newcolor = color_rgb( int(255*weight), int(66+150*(1-weight)), int(255*(1-weight)) )
win.setBackground(newcolor)
def main():
win = GraphWin("Celsius Converter", 400, 300)
win.setCoords(0.0, 0.0, 3.0, 4.0)
# 绘制输入接口
Text(Point(1,3)," Celsius Temperature:").draw(win)
Text(Point(2,2.7), " (Please input 0.0-100.0 )").draw(win)
Text(Point(1,1), "Fahrenheit Temperature:").draw(win)
input = Entry(Point(2,3), 5)
input.setText("0.0")
input.draw(win)
output = Text(Point(2,1),"")
output.draw(win)
button = Text(Point(1.5,2.0),"Convert It")
button.draw(win)
rect = Rectangle(Point(1,1.5), Point(2,2.5))
rect.draw(win)
while(True):
# 等待鼠标点击
win.getMouse()
result = convert(input) # 转换输入
output.setText(result) # 显示输出
# 改变颜色
colorChange(win,input)
# 改变按钮字体
#button.setText("Quit")
# 等待点击事件,退出程序
#win.getMouse()
#win.close()
if __name__ == '__main__':
main()</code></p>
1
回复