# python学习-摄氏温度转换华氏温度小程序(graphics)

from graphics import *
# 定义温度转换函数
def convert(input):
celsius = eval(input.getText())
fahrenheit = 9.0/5.0 * celsius + 32.0
return fahrenheit

# 根据输入温度，改变窗口背景颜色（温度越高，颜色越红）
def changecolor(input,win):
num = eval(input.getText())/100
red_1 = round(255 * num)  # round()函数将浮点数变为整数（四舍五入）
green_1 = round(66 + 150*(1-num))
blue_1 = round(255 * (1-num))
newcolor = color_rgb(red_1,green_1,blue_1)
win.setBackground(newcolor)

# 程序主体
def main():
win = GraphWin("Celsius Converter", 400, 300)
win.setCoords(0, 0, 3, 4)
# 绘制接口
Text(Point(1,3), " Celsius Temperature:").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)
Rectangle(Point(1,1.5), Point(2,2.5)).draw(win)
# 等待鼠标点击
win.getMouse()
# 调用函数，转换温度，改变背景颜色
fahrenheit = convert(input)
changecolor(input,win)
# 显示输出，改变按钮
output.setText(fahrenheit)
button.setText("Quit")
# 等待响应鼠标点击，退出程序
win.getMouse()
win.close()

# 程序执行
if __name__ == '__main__':
main()

12-08
07-06 5855
08-05 1万+
08-04 1845
05-13 4417
09-10 3778
12-19 7032
11-10 2万+
04-22 1万+
12-24 3684
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客