一、代码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 stringfahrenheit = 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 stringfahrenheit = 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 presswindow = 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 errorslayout = [ [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 optionssg.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的学习。 |
|