• 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的学习。
  • 相关阅读:
    web10 动态action的应用
    web09 struts2配置 struts2入门
    web 08 struts2入门 struts2配置 struts包
    web07-jdbcBookStore
    web06-PanduanLogin
    web05-CounterServlet
    web04-LoginServlet
    web03-OutputInfo
    web02-welcomeyou
    web01-helloworld
  • 原文地址:https://www.cnblogs.com/ysysbky/p/12155810.html
Copyright © 2020-2023  润新知