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)

这是一个用来获取文件的示例,似于网页表单提交文件;
[[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)

就变成了这个样子;
可以理解为用列表分组来改变布局;
这里面的布局类似于网格化布局;
用列表+","来换行;
接下来,当我们选择文件并点击确定后:

打印了输出结果:
确定 {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)
这个部分的意思是监听窗体;
从输入框读取内容;
目前只有一个输入框;
输入框作为字典,被读入;
点击'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’,以此类推;
获取到输入值,我们就可以进行后续操作了;
上面代码的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')
效果:

显示多行文本;
但是发现标题和文本内容是一致的;
修改为:
text = 'abcdefg'
sg.PopupScrolled(text, no_titlebar=True)
效果:

不可移动;
似乎用来显示一些说明信息还行;
更详细的用法和参数信息参考官网;
PopupGetFile
msg = '请选择文件路径:'
sg.PopupGetFile(msg)
效果:

这个结果类似于“代码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')
效果:

调试输出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程序都将使用两种设计模式之一,具体取决于您要实现的窗口类型
模式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-持久窗口(使用事件循环多次读取)
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-持久窗口(使用事件循环多次读取+更新窗口中的数据)
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()
效果:

网友评论