一、代碼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的學習。
