一、代码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的学习。

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

  1. Python学习之温度转换实例分析篇

    #TempConvert.py Tempstr=input('请输入要转换的温度值:') if Tempstr[-1] in ['C','c']: F=1.8*eval(Tempstr[0:-1])+ ...

  2. [Python3 练习] 001 温度转换1

    题目:温度转换 I (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转换 ...

  3. [Python3 练习] 002 温度转换2

    题目:温度转换 II (1) 描述 温度的刻画有两个不同体系:摄氏度 (Celsius) 和华氏度 (Fabrenheit) 请编写程序将用户输入的华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度 转 ...

  4. 计算机二级Python学习笔记(一):温度转换

    今天通过一个温度转换的十行代码,理解了一些Python的基本元素. 所谓温度转换,就是摄氏度和华氏度的转换,要求输入摄氏度,可以输出华氏度,反之一样能实现.代码如下: #TempConvert.py ...

  5. ytu 2029: C语言实验——温度转换(水题)

    2029: C语言实验——温度转换 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 12  Solved: 10[Submit][Status][Web B ...

  6. 【笔记】嵩天.Python语言程序设计.完成两个简单实例(温度转换和绘图)

    [博客导航] [Python相关] 目标 使用PyCharm,完成两个小实例的编写和运行.一个是温度转换,一个是蟒蛇图形绘制. 过程 1.先设置project目录,虽然命名不是很正式,主要不太习惯软件 ...

  7. 温度转换-java

    java 温度转换 题目内容: 写一个将华氏温度转换成摄氏温度的程序,转换的公式是: °F = (9/5)*°C + 32 其中C表示摄氏温度,F表示华氏温度. 程序的输入是一个整数,表示华氏温度.输 ...

  8. Python3.x:基础学习

    Python3.x:基础学习 1,Python有五种标准数据类型 1.数字 2.字符串 3.列表 4.元组 5.字典 (1).数字 数字数据类型存储数字值.当为其分配值时,将创建数字对象. var1 ...

  9. 1001. 温度转换 (Standard IO)

    1001. 温度转换 (Standard IO) 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   题目描述 将输入的华氏温度转换为摄氏温度.由华氏温度F与摄氏温度C的转换 ...

随机推荐

  1. [lua]紫猫lua教程-命令宝典-L1-03-01. 闭包

    L1[闭包]01. 函数的传递赋值 没什么说的 1.函数作为变量来看 可以轻松的声明 相互赋值 2.函数变量本质是 一个内存指针 所以函数变量的相互赋值不是传递的函数本身 而是指向这个函数的内存地址 ...

  2. 网络知识杂谈 - https - 原理简述

    概述 简单描述 https 尽量介绍它的原理 实际的机制, 可能会更加复杂一些... 背景 这玩意, 困扰我好多年了 今天开始, 想做个了断 之前工作也接触过, 但从我的角度来说, 认识很浅 会配置 ...

  3. 每天进步一点点------ModelSim仿真Altera的ROM

    1. 在QuartusII中生成rom的初始化文件,可以是hex,也可以是mif.MIF文件的格式很简单明了,所以我一向都是用MIF. 2.下载convert_hex2ver.dll文件,conver ...

  4. Eqaulize Prices

    There are n products in the shop. The price of the ii-th product is aiai. The owner of the shop want ...

  5. XML学习笔记1

    一.XML与HTML的差异 XML 不是 HTML 的替代:XML 和 HTML 为不同的目的而设计: XML 被设计用来传输和存储数据,其焦点是数据的内容: HTML 被设计用来显示数据,其焦点是数 ...

  6. Bugku-CTF之这是一个神奇的登陆框

    Day32 这是一个神奇的登陆框 http://123.206.87.240:9001/sql/ flag格式flag{}  

  7. pods " xxxx" not found错误

    pods " xxxx" not found错误 待办 https://linuxacademy.com/community/show/29447-pod-is-not-found ...

  8. dk7和jdk8的一些新特性

    本文是我学习了解了j 的一些资料,有兴趣的大家可以浏览下下面的内容. 官方文档:http://www.oracle.com/technetwork/java/javase/jdk7-relnotes- ...

  9. django model 高级进阶

    十.model高级用法: 10.1 ORM映射: Object Relational Mapping: orm映射的任务: 将list ,object 的操作转化为sql语句,根据对象生成数据表,将查 ...

  10. Ubuntu执行sudo apt-get update报错E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用) E: 无法对目录 /var/lib/apt/lists/ 加锁

    一.强制解锁,执行语句 sudo rm /var/lib/apt/lists/lock 二.终端输入 ps -aux | grep apt-get 查看一下apt-get的相关进程.然后sudo ki ...