学习资料:The Quick Tour.
我们先看一个例子:
-
一般是将 PySimpleGUI 重命名为
sg
,sg.Text
用来输出文字到 UI。- 参数:
-
front
:设置字体 -
text_color
:设置文字颜色 -
background_color
:设置背景颜色
sg.InputText(默认值)
:获取键盘的输入。(可以设置默认值)sg.OK("是")
,sg.Cancel()
:按照字面意思理解即可。sg.Window('Window Title', layout)
:用来创建一个窗口。
sg.Popup
相当于print
。sg.FileBrowse()
:文件浏览器。
更丰富的可以使用 sg.main()
显示:
实现上图的界面有点复杂,下面我们先学习一个简单版本的:
import PySimpleGUI as sg
sg.ChangeLookAndFeel('GreenTan')
# ------ Menu Definition ------ #
menu_def = [['File', ['Open', 'Save', 'Exit', 'Properties']],
['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
['Help', 'About...'], ]
# ------ Column Definition ------ #
column1 = [[sg.Text('Column 1', background_color='#F7F3EC', justification='center', size=(10, 1))],
[sg.Spin(values=('Spin Box 1', '2', '3'),
initial_value='Spin Box 1')],
[sg.Spin(values=('Spin Box 1', '2', '3'),
initial_value='Spin Box 2')],
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')]]
layout = [
[sg.Menu(menu_def, tearoff=True)],
[sg.Text('All graphic widgets in one window!', size=(
30, 1), justification='center', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
[sg.Text('Here is some text.... and a place to enter text')],
[sg.InputText('This is my text')],
[sg.Frame(layout=[
[sg.Checkbox('Checkbox', size=(10, 1)), sg.Checkbox(
'My second checkbox!', default=True)],
[sg.Radio('My first Radio! ', "RADIO1", default=True, size=(10, 1)), sg.Radio('My second Radio!', "RADIO1")]], title='Options', title_color='red', relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],
[sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3)),
sg.Multiline(default_text='A second multi-line', size=(35, 3))],
[sg.InputCombo(('Combobox 1', 'Combobox 2'), size=(20, 1)),
sg.Slider(range=(1, 100), orientation='h', size=(34, 20), default_value=85)],
[sg.InputOptionMenu(('Menu Option 1', 'Menu Option 2', 'Menu Option 3'))],
[sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3)),
sg.Frame('Labelled Group', [[
sg.Slider(range=(1, 100), orientation='v',
size=(5, 20), default_value=25),
sg.Slider(range=(1, 100), orientation='v',
size=(5, 20), default_value=75),
sg.Slider(range=(1, 100), orientation='v',
size=(5, 20), default_value=10),
sg.Column(column1, background_color='#F7F3EC')]])],
[sg.Text('_' * 80)],
[sg.Text('Choose A Folder', size=(35, 1))],
[sg.Text('Your Folder', size=(15, 1), auto_size_text=False, justification='right'),
sg.InputText('Default Folder'), sg.FolderBrowse()],
[sg.Submit(tooltip='Click to submit this window'), sg.Cancel()]
]
window = sg.Window('Everything bagel', layout,
default_element_size=(40, 1), grab_anywhere=False)
event, values = window.Read()
sg.Popup('Title',
'The results of the window.',
'The button clicked was "{}"'.format(event),
'The values are', values)
window.Close()
创建菜单栏
直接看图示:
点击提交,便会依据 sg.Popup
弹出一个窗口:
我们将代码量进拆解: