python3.8的PySimpleGUI学习的温度转换(℃转℉)

一、代码1:

#导出模块
import PySimpleGUI as sg
#总体布局,sg.InputText(),默认size=(45,1)。
layout = [
         [sg.Text(‘Celcius(摄氏温度)‘), sg.InputText(size=(15,1)),sg.Text(‘℃‘)], #第1行的3个布局
         [sg.Submit()], #第2行
         ]

#定义窗口即标题
#window = sg.Window(‘Temperature Converter‘).Layout(layout) #方法一layout布局
window = sg.Window(‘Temperature Converter‘,layout)  #方法二layout布局
#get value (part of a list)
button, value = window.Read()
#定义按钮
if button is None:
    exit(0)
#convert and create string
fahrenheit = round(9/5*float(value[0]) +32, 1)   #公式,1为保留小数点后面1位
result =  ‘Temperature in Fahrenheit is(华氏温度是): ‘ + str(fahrenheit)+‘℉‘ #定义
#display in Popup ,显示popup弹出框
sg.Popup(‘Result‘, result)                     

二、代码2:

#导出模块
import PySimpleGUI as sg
#自定义颜色,有点麻烦,也可以默认主题色,或者设置总体主题色
sg.SetOptions (background_color = ‘LightBlue‘,
              element_background_color = ‘LightBlue‘,
              text_element_background_color = ‘LightBlue‘,
              font = (‘Arial‘, 10, ‘bold‘),
              text_color = ‘Blue‘,
              input_text_color =‘Blue‘,
              button_color = (‘White‘, ‘Blue‘) #按钮颜色,白色字,蓝色背景颜色
               )
#总体布局
layout = [
         [sg.Text(‘Celcius(摄氏温度:)‘, size =(18,1)), sg.InputText(size = (15,1)),sg.Text(‘℃‘)],
         [sg.Submit()]
         ]
#定义窗口即标题
#window = sg.Window(‘Temperature Converter‘).Layout(layout) #方法一layout布局
window = sg.Window(‘Temperature Converter‘,layout)  #方法二layout布局
#读出win的数值
button, value = window.Read()
#定义按钮
if button is None:
    exit(0)
#convert and create string
fahrenheit = round(9/5*float(value[0]) +32, 1)   #公式,1为保留小数点后面1位
result =  ‘Temperature in Fahrenheit is(华氏温度是): ‘ + str(fahrenheit)+‘℉‘ #定义
#display in Popup ,显示popup弹出框
sg.Popup(‘Result‘, result)                     

三、代码3:

#导出模块
import PySimpleGUI as sg
#自定义颜色
sg.SetOptions (background_color = ‘LightBlue‘,
               element_background_color = ‘LightBlue‘,
               text_element_background_color = ‘LightBlue‘,
               font = (‘Arial‘, 10, ‘bold‘),
               text_color = ‘Blue‘,
               input_text_color =‘Blue‘,
               button_color = (‘White‘, ‘Blue‘)
               )
#update (via list) values and and display answers
#value[0] is celcius input, value[1] is input to place result.
#Use ReadButton with while true: - keeps window open.
#认识persistent form and bind key的学习

layout = [
         [sg.Text(‘Enter a Temperature in Celcius‘)],
         [sg.Text(‘Celcius‘, size =(8,1)), sg.InputText(size = (15,1))],
         [sg.Text(‘Result‘, size =(8,1)), sg.InputText(size = (15,1))],
         [sg.ReadButton(‘Submit‘, bind_return_key = True)]
         ]
#Return = button press
window = sg.Window(‘Converter‘).Layout(layout) 

while True:
    #get result
    button, value = window.Read()
    #break out of loop is button not pressed.
    if button is not None:
        fahrenheit = round(9/5*float(value[0]) +32, 1)
        #put result in 2nd input box
        window.FindElement(1).Update(fahrenheit)  

    else:
        break

四、代码4:

#导出模块
import PySimpleGUI as sg
#自定义颜色
sg.SetOptions (background_color = ‘LightBlue‘,
               element_background_color = ‘LightBlue‘,
               text_element_background_color = ‘LightBlue‘,
               font = (‘Arial‘, 10, ‘bold‘),
               text_color = ‘Blue‘,
               input_text_color =‘Blue‘,
               button_color = (‘White‘, ‘Blue‘)
               )
#name inputs (key) uses dictionary- easy to see updating of results
#value[input] first input value te c...
#学习named input keys and catch errors

layout = [
         [sg.Text(‘Enter a Temperature in Celcius‘)],
         [sg.Text(‘Celcius‘, size =(8,1)), sg.InputText(size = (15,1),key = ‘_input_‘)],
         [sg.Text(‘Result‘, size =(8,1)), sg.InputText(size = (15,1),key = ‘_result_‘)],
         [sg.ReadButton(‘Submit‘, bind_return_key = True)]
         ]  

window = sg.FlexForm(‘Temp Converter‘).Layout(layout) 

while True:
    button, value = window.Read()
    if button is not None:
        #catch program errors for text or blank entry:
        try:
            fahrenheit = round(9/5*float(value[‘_input_‘]) +32, 1)
            #put result in text box
            window.FindElement(‘_result_‘).Update(fahrenheit)
        except ValueError:
            sg.Popup(‘Error‘,‘Please try again‘)
    else:
        break

五、代码5:

#导出模块
import PySimpleGUI as sg
#个性化设置,可以不设置,那就是默认的银河灰
#Can use a variety of themes - plus individual options
sg.ChangeLookAndFeel(‘SandyBeach‘)
sg.SetOptions (font = (‘Arial‘, 10, ‘bold‘))
#布局
layout = [
         [sg.Text(‘Enter a Temperature in Celcius‘)], #第1行
         [sg.Text(‘Celcius‘, size =(8,1)), sg.InputText(size = (15,1),key = ‘_input_‘)], #第2行
         [sg.Text(‘Result‘, size =(8,1)), sg.InputText(size = (15,1),key = ‘_result_‘)], #第3行
         [sg.ReadButton(‘Submit‘, bind_return_key = True)]  #第4行
         ]
#定义窗口的标题和布局
window = sg.Window(‘Temp Converter‘).Layout(layout)
#循环设置
while True:
    button, value = window.Read()
    if button is not None:
        #catch program errors for text, floats or blank entry:
        #Also validation for range [0, 50],这是多指人体的温度范围,当然35℃都考虑低温了,很危险。
        #input的key值的学习
        #validation(验证) and look and feel的学习
        try:
            if float(value[‘_input_‘]) > 50 or float(value[‘_input_‘]) <0:
                sg.Popup(‘Error‘,‘Out of range‘)
            else:
                fahrenheit = round(9/5*int(value[‘_input_‘]) +32, 1)
                window.FindElement(‘_result_‘).Update(fahrenheit) #FindElement和Update的学习
        except ValueError:
                sg.Popup(‘Error‘,‘Please try again‘)        

    else:
        break

总结:

这是一个温度转换的Python的代码,用PySimpleGUI编写,注意其中几个不同之处。

1.layout的布局学习及在Window中的方式。

2.自定义背景颜色和默认背景颜色。

3.FindElement和Update的学习。

4.input的key值的学习。

5.validation(验证) and look and feel的学习。

原文地址:https://www.cnblogs.com/ysysbky/p/12155810.html

时间: 2024-07-31 07:49:10

python3.8的PySimpleGUI学习的温度转换(℃转℉)的相关文章

[Python3 练习] 001 温度转换1

题目:温度转换 I (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转换公式如下,C 表示摄氏度,F 表示华氏度???????????????????????????????????????????????????????????????????????????????????????????????? C = ( F - 32 ) / 1.8?????????????

[Python3 练习] 002 温度转换2

题目:温度转换 II (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转换公式如下,C 表示摄氏度,F 表示华氏度???????????????????????????????????????????????????????????????????????????????????????????????? C = ( F - 32 ) / 1.8????????????

Python3中bytes和HexStr之间的转换

1 Python3中bytes和HexStr之间的转换 ByteToHex的转换 def ByteToHex( bins ): """ Convert a byte string to it's hex string representation e.g. for output. """ return ''.join( [ "%02X" % x for x in bins ] ).strip() HexToByte的转换 de

C++学习笔记33 转换运算符

有时候我们想将一个类类型转换为另一个类类型,同时,这两个类并不存在继承关系,这时候我们就需要一种叫做转换运算符的运算符. 一个简单的例子.要将类A转换为int类型 #include <iostream> #include <string> using namespace std; class A{ private: int n; string str; public: A(int m,string s):n(m),str(s){ } }; int main(){ A a(10,&q

单片机练习 - DS18B20温度转换与显示

DS18B20 数字温度传感器(参考:智能温度传感器DS18B20的原理与应用)是DALLAS 公司生产的1-Wire,即单总线器件,具有线路简单,体积小的特点.因此用它来组成一个测温系统,具有线路简单,在一根通信线,可以挂很多这样的数字温度计.DS18B20 产品的特点: (1).只要求一个I/O 口即可实现通信.(2).在DS18B20 中的每个器件上都有独一无二的序列号.(3).实际应用中不需要外部任何元器件即可实现测温.(4).测量温度范围在-55 到+125℃之间; 在-10 ~ +8

Python3.x:基础学习

Python3.x:基础学习 1,Python有五种标准数据类型 1.数字 2.字符串 3.列表 4.元组 5.字典 (1).数字 数字数据类型存储数字值.当为其分配值时,将创建数字对象. var1 = 10 var2 = 20 可以使用del语句删除对数字对象的引用. del语句的语法是 del var1[,var2[,var3[....,varN]]]] 可以使用del语句删除单个对象或多个对象. del var del var_a, var_b Python支持三种不同的数值类型 - in

Python入门习题1.温度转换

这一节的课堂例题为: 例1.编写一个Python程序,完成摄氏度到华氏度,华氏度到摄氏度的温度转换. 解: (1)分析问题:利用程序实现温度转换,由用户输入温度值,程序给出输出结果. (2)划分边界:IPO描述如下 输入(Input):带华氏或摄氏标志的温度值 处理(Process):根据温度标志选择适当的温度转换算法 输出(Output):带有华氏或摄氏标志的温度值 (3)设计算法:C表示摄氏度,F表示华氏度. C = (F - 32) / 1.8 F = C*1.8 + 32 (4)编写程序

练习2-4 温度转换 (5 分)

练习2-4 温度转换 (5 分) 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5*(F-32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 输入格式: 本题目没有输入. 输出格式: 按照下列格式输出 fahr = 150, celsius = 计算所得摄氏温度的整数值 #include <stdio.h>#include <stdlib.h> /* run this program using the console pauser or

python温度转换代码

#TempConvert.py TempStr=input("请输入带有符号的温度值:")#赋值TempStr,括号里面的是提示 if TempStr[-1] in ['F','f']:#假如字符串最后一个字符是F或者f C=(eval(TempStr[0:-1])-32)/1.8#第一个字符到最后一个字符之前的所有字符,也就是温度值,eval函数是脱掉字符串结构,运行公式 print("转换后的温度是{:.2f}C".format(C)).#输出结果,保留最后两位