PythonGUI---graphics温度转换及颜色预警

  • 项目描述:

    •  温度越高,颜色越偏红
    •  温度越低,颜色越偏蓝
      • setBackground(Newcolor)设置窗口背景颜色。
      • 计算温度值设定窗口颜色:
      • 假定输入温度范围为0-100,
      • 颜色权重weight=输入温度/100
      • newcolor的rgb计算:
      • 红色分量=255*weight
      • 绿色分量=66+150(1-weight)
      • 蓝色分量=255*(1-weight)
  • 环境:

    • python3.5
    • win10
    • graphics模块
  • 完整代码如下:

    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),
      "Celsuis Temperature:").draw(win)
        Text(Point(2,2.7),
      "(Plese 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)
        #等待鼠标点击
        win.getMouse()
        result = convert(input)# 转换输入
        output.setText(result)#显示输出
        #改变颜色
        colorChange(win,input)
        #改变按钮颜色
        button.setText("Quit")
        #等待点击事件,退出程序
        win.getMouse()
        win.close()
    if __name__ == "__main__":
        main()
    
  • 执行情况:输入不同的数字
    0,20,40,60,80,100

温度改变颜色

你可能感兴趣的:(PythonGUI---graphics温度转换及颜色预警)