PyQt5很強大贤牛,但是并不那么簡單;
PySimpleGUI 說是很簡單则酝,所以來看看殉簸;
代碼1
import PySimpleGUI as sg
event, values = sg.Window('獲取文件舉例',
[[sg.Text('文件名'), sg.Input(), sg.FileBrowse('瀏覽')], [sg.OK('確定'), sg.Cancel('取消')]]).Read()
print(event, values)
這是一個用來獲取文件的示例,似于網(wǎng)頁表單提交文件沽讹;
[[sg.Text('文件名'), sg.Input(), sg.FileBrowse('瀏覽')], [sg.OK('確定'), sg.Cancel('取消')]]
是個列表般卑,列表可以分組,并用","隔開爽雄;
稍作修改:
import PySimpleGUI as sg
event, values = sg.Window('獲取文件舉例',
[[sg.Text('文件名')], [sg.Input(), sg.FileBrowse('瀏覽')], [sg.OK('確定'), sg.Cancel('取消')]]).Read()
print(event, values)
就變成了這個樣子蝠检;
可以理解為用列表分組來改變布局;
這里面的布局類似于網(wǎng)格化布局挚瘟;
用列表+","來換行叹谁;
接下來迟杂,當我們選擇文件并點擊確定后:
打印了輸出結(jié)果:
確定 {0: 'E:/Python_Pro/Test_Pro/PySimpleGUI_test/test.py', '瀏覽': 'E:/Python_Pro/Test_Pro/PySimpleGUI_test/test.py'}
需要注意的是:
“確定”對應“event”,是字符串類型本慕;
"{0: 'E:/Python_Pro/Test_Pro/PySimpleGUI_test/test.py', '瀏覽': 'E:/Python_Pro/Test_Pro/PySimpleGUI_test/test.py'}"對應values排拷,是字典類型;
可以看到字典里面的“0”的鍵值和“瀏覽”的鍵值是一樣的锅尘;
到目前為止监氢,真的非常簡單!
代碼2
import PySimpleGUI as sg
sg.theme('Dark Blue 5') # please make your creations colorful
layout = [[sg.Text('Filename')],
[sg.Input(), sg.FileBrowse()],
[sg.OK(), sg.Cancel()]]
window = sg.Window('Get filename example', layout)
event, values = window.Read()
window.close()
sg.theme('Dark Blue 5') 是用來設置主題藤违;
layout = [[sg.Text('Filename')],
[sg.Input(), sg.FileBrowse()],
[sg.OK(), sg.Cancel()]]
不再和窗體顯示混在一起浪腐;
當需要顯示窗體時,只需要傳入layout即可顿乒;
獲取文件名舉例(Get filename example)
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [[sg.Text('Some text on Row 1')],
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Cancel')]]
# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Cancel'): # if user closes window or clicks cancel
break
print('You entered ', values[0])
window.close()
效果:
sg.theme('DarkAmber') 名稱為‘DarkAmber’的配色主題议街;
sg.InputText() 是輸入框,如果需要可以在輸入框加入默認文字:sg.InputText('xxx')
while True:
event, values = window.read()
if event in (None, 'Cancel'): # if user closes window or clicks cancel
break
print('You entered ', values[0])
print('You entered ', values)
這個部分的意思是監(jiān)聽窗體璧榄;
從輸入框讀取內(nèi)容特漩;
目前只有一個輸入框;
輸入框作為字典骨杂,被讀入涂身;
點擊'OK',會打印輸出:
You entered xxx
You entered {0: 'xxx'}
我再加一個輸入框,來看看捕捉到了什么:
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [[sg.Text('Some text on Row 1'), sg.InputText('aaa')],
[sg.Text('Enter something on Row 2'), sg.InputText('bbb')],
[sg.Button('Ok'), sg.Button('Cancel')]]
# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Cancel'): # if user closes window or clicks cancel
break
print('You entered ', values)
window.close()
打印輸出:
You entered {0: 'aaa', 1: 'bbb'}
這樣就很清楚了搓蚪,第一個輸入框的鍵為‘0’蛤售,第二個是‘1’,以此類推妒潭;
獲取到輸入值悴能,我們就可以進行后續(xù)操作了;
上面代碼的layout再添加一項:
layout = [[sg.Text('Some text on Row 1'), sg.InputText('aaa')],
[sg.Text('Enter something on Row 2'), sg.InputText('bbb')],
[sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
[sg.Button('Ok'), sg.Button('Cancel')]]
效果:
好玩得很雳灾!
彈窗Popup匯總
sg.Popup('Popup') # Shows OK button
# sg.PopupOk('PopupOk') # Shows OK button
sg.PopupYesNo('PopupYesNo') # Shows Yes and No buttons
sg.PopupCancel('PopupCancel') # Shows Cancelled button
sg.PopupOKCancel('PopupOKCancel') # Shows OK and Cancel buttons
sg.PopupError('PopupError') # Shows red error button
sg.PopupTimed('PopupTimed') # Automatically closes
sg.PopupAutoClose('PopupAutoClose') # Same as PopupTimed
sg.PopupScrolled('okkk')
各種彈窗效果漠酿,自行嘗試;
需要特別說明的彈窗:
PopupScrolled
sg.PopupScrolled('okkk')
效果:
顯示多行文本佑女;
但是發(fā)現(xiàn)標題和文本內(nèi)容是一致的记靡;
修改為:
text = 'abcdefg'
sg.PopupScrolled(text, no_titlebar=True)
效果:
不可移動;
似乎用來顯示一些說明信息還行团驱;
更詳細的用法和參數(shù)信息參考官網(wǎng);
PopupGetFile
msg = '請選擇文件路徑:'
sg.PopupGetFile(msg)
效果:
這個結(jié)果類似于“代碼1”空凸;
但是怎么獲取選擇后的文件路徑呢嚎花?
進度條OneLineProgressMeter
import PySimpleGUI as sg
for i in range(1, 10000):
sg.OneLineProgressMeter('My Meter', i + 1, 10000, 'key', 'Optional message')
效果:
橫向
for i in range(1, 10000):
sg.OneLineProgressMeter('My Meter', i + 1, 10000, 'key', 'Optional message', orientation='h')
效果:
調(diào)試輸出Print
import PySimpleGUI as sg
for i in range(10000):
sg.Print(i)
等效于:
import PySimpleGUI as sg
print=sg.Print
for i in range(100):
print(i)
效果:
所有的PySimpleGUI程序都將使用兩種設計模式之一,具體取決于您要實現(xiàn)的窗口類型
模式1-“一次性窗口”-讀取一次窗口然后將其關閉
import PySimpleGUI as sg
sg.theme('Dark Blue 3') # please make your windows colorful
layout = [[sg.Text('SHA-1 and SHA-256 Hashes for the file')],
[sg.InputText(), sg.FileBrowse()],
[sg.Submit(), sg.Cancel()]]
window = sg.Window('SHA-1 & 256 Hash', layout)
event, values = window.read()
window.close()
source_filename = values[0] # the first input element is values[0]
效果:
模式2
A-持久窗口(使用事件循環(huán)多次讀妊街蕖)
import PySimpleGUI as sg
sg.theme('Dark Blue 3') # please make your windows colorful
layout = [[sg.Text('Persistent window')],
[sg.Input()],
[sg.Button('Read'), sg.Exit()]]
window = sg.Window('Window that stays open', layout)
while True:
event, values = window.read()
if event is None or event == 'Exit':
break
print(event, values)
window.close()
效果:
B-持久窗口(使用事件循環(huán)多次讀取+更新窗口中的數(shù)據(jù))
import PySimpleGUI as sg
sg.theme('Dark Blue 3') # please make your windows colorful
layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(12,1), key='_OUTPUT_')],
[sg.Input(key='_IN_')],
[sg.Button('Show'), sg.Button('Exit')]]
window = sg.Window('Window Title', layout)
while True: # Event Loop
event, values = window.read() # can also be written as event, values = window()
print(event, values)
if event is None or event == 'Exit':
break
if event == 'Show':
# change the "output" element to be the value of "input" element
window['_OUTPUT_'].update(values['_IN_'])
# above line can also be written without the update specified
window['_OUTPUT_'](values['_IN_'])
window.close()
效果: