
安装
pip install pysimplegui
两种设计模式
- One-shot window:“单发”窗口是一个弹出窗口,它会收集一些数据,然后消失。它是一种“form”,旨在快速获取一些信息然后将其关闭。
- Persistent window:“持久”窗口是一个固定可见的窗口。使用这些程序,您可以循环,读取和处理“事件”,例如单击按钮。它更像是典型的Windows / Mac / Linux程序。
使用单发窗口
- 触发时,读一个window的内容,返回(event,values)元组,然后关闭,形成一次跳转。
- event是触发跳转的事件,可能是点击按钮、选择表单项等,或是点窗口上的关闭图标×。
- values是读到的所有输入样式元素的值的字典。字典使用keys来定义条目。如果你的元素没有指定key,后台会为你提供一个。这些自动提供/编码的键是0,1,2,3这种整数。
import PySimpleGUI as sg
sg.theme('DarkBlue1')
layout = [[sg.Text('My one-shot window.')],
[sg.InputText()],
[sg.Submit(), sg.Cancel()]]
window = sg.Window('Window Title', layout)
event, values = window.read()
window.close()
text_input = values[0]
sg.popup('You entered', text_input)
如果你想指定InputText对应的key,可以如下:
import PySimpleGUI as sg
sg.theme('DarkBlue1')
layout = [[sg.Text('My one-shot window.')],
[sg.InputText(key='-IN-')],
[sg.Submit(), sg.Cancel()]]
window = sg.Window('Window Title', layout)
event, values = window.read()
window.close()
text_input = values['-IN-']
sg.popup('You entered', text_input)


使用持久窗口
- 核心是读取(event,values)循环。
- 然后在用户想退出的时候及时break掉循环即可。对于单击关闭,event的返回值为None。对于Exit按钮,返回值为Exit。
import PySimpleGUI as sg
sg.theme('DarkAmber') # Remove line if you want plain gray windows
layout = [[sg.Text('Persistent window')],
[sg.Input(key='-IN-')],
[sg.Button('Read'), sg.Exit()]]
window = sg.Window('Window that stays open', layout)
while True: # The Event Loop
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
window.close()

更新窗口
import PySimpleGUI as sg
sg.theme('BluePurple')
layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')],
[sg.Input(key='-IN-')],
[sg.Button('Show'), sg.Button('Exit')]]
window = sg.Window('Pattern 2B', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
break
if event == 'Show':
# Update the "output" text element to be the value of "input" element
window['-OUTPUT-'].update(values['-IN-'])
window.close()
- 首先在Layout时定义一个空Text
- 在读取到Input后,将空Text更新
window[key]
表示在窗口中找到一个元素。该语句所有之后内容都意味着你正在直接使用元素。也许该元素除了上面调用的update之外还有其他方法,例如set_tooltip()
。


例子1:获取数据
import PySimpleGUI as sg
sg.theme('Topanga') # Add some color to the window
# Very basic window. Return values using auto numbered keys
layout = [
[sg.Text('Please enter your Name, Address, Phone')],
[sg.Text('Name', size=(15, 1)), sg.InputText()],
[sg.Text('Address', size=(15, 1)), sg.InputText()],
[sg.Text('Phone', size=(15, 1)), sg.InputText()],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('Simple data entry window', layout)
event, values = window.read()
window.close()
print(event, values[0], values[1], values[2])

例子2:文件浏览与读取
import PySimpleGUI as sg
import sys
import cv2
layout = [[sg.Text('File to open')],
[sg.In(key='-INFILE-'), sg.FileBrowse()],
[sg.Open(), sg.Exit()]]
window = sg.Window('Picture Browser', layout)
fname = '/home/zhanglr/Documents/HUAWEI_HCIA_AI/python高级实验/GUI/test.jpg'
while True:
event, values = window.read()
fname = values['-INFILE-']
print(event, values)
if event in (None, 'Exit'):
break
img = cv2.imread(fname,0)
img = cv2.resize(img,(480,320))
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
window.close()

当然你也可以不用自己造轮子:
import PySimpleGUI as sg
import sys
if len(sys.argv) == 1:
fname = sg.popup_get_file('Document to open')
else:
fname = sys.argv[1]
if not fname:
sg.popup("Cancel", "No filename supplied")
raise SystemExit("Cancelling: no filename supplied")
else:
sg.popup('The filename you chose was', fname)
直接就可以唤醒窗口:

例子3:多窗口
import PySimpleGUI as sg
# Design pattern 1 - First window does not remain active
layout = [[ sg.Text('Window 1'),],
[sg.Input()],
[sg.Text('', key='_OUTPUT_')],
[sg.Button('Launch 2')]]
win1 = sg.Window('Window 1', layout)
win2_active=False
while True:
ev1, vals1 = win1.Read(timeout=100)
if ev1 is None:
break
win1['_OUTPUT_'].update(vals1[0])
if ev1 == 'Launch 2' and not win2_active:
win2_active = True
win1.Hide()
layout2 = [[sg.Text('Window 2')],
[sg.Button('Exit')]]
win2 = sg.Window('Window 2', layout2)
while True:
ev2, vals2 = win2.Read()
if ev2 is None or ev2 == 'Exit':
win2.Close()
win2_active = False
win1.UnHide()
break