真正用python语言风格设计的gui库,熟悉python语言的朋友基本上看这么三个例子基本就能看懂PySimpleGUI设计范式。
pip3 install PySimpleGUI
设计gui就像写python代码一样令人得心应手。表单设计步骤:
案例一
第一行只是文本提示作用,使用sg.Text
[ sg.Text('Enter a number') ]
第二行有一个输入字段
[ sg.Input() ]
第三行是一个OK按钮
[ sg.OK() ]
现在我们已经将这三行都定义了,那么将这三行定义好的代码放到一个列表中就代表了整个窗口的组织结构。
layout = [ [sg.Text('Enter a Number')],
[sg.Input()],
[sg.OK()] ]
完整代码如下
import PySimpleGUI as sg
layout = [[sg.Text('Enter a Number')],
[sg.Input()],
[sg.OK()] ]
with sg.FlexForm('简单的PySimpleGUI设计') as form:
button, (number,) =form.LayoutAndRead(layout)
sg.MsgBox(button, number)
代码如下
import PySimpleGUI as sg
layout = [[sg.Text('Filename')],
[sg.Input(), sg.FileBrowse()],
[sg.OK(), sg.Cancel()] ]
with sg.FlexForm('Get filename example') as form:
button, (number,) =form.LayoutAndRead(layout)
sg.MsgBox(button, number)
import PySimpleGUI as sg
with sg.FlexForm('Everything bagel', auto_size_text=True, default_element_size=(40, 1)) as form:
layout = [
[sg.Text('All graphic widgets in one form!', size=(30, 1), font=("Helvetica", 25), text_color='blue')],
[sg.Text('Here is some text.... and a place to enter text')],
[sg.InputText()],
[sg.Checkbox('My first checkbox!'), sg.Checkbox('My second checkbox!', default=True)],
[sg.Radio('My first Radio! ', "RADIO1", default=True), sg.Radio('My second Radio!', "RADIO1")],
[sg.Multiline(default_text='This is the default Text shoulsd you decide not to type anything',
scale=(2, 10))],
[sg.InputCombo(['Combobox 1', 'Combobox 2'], size=(20, 3)),
sg.Slider(range=(1, 100), orientation='h', size=(35, 20), default_value=85)],
[sg.Listbox(values=['Listbox 1', 'Listbox 2', 'Listbox 3'], size=(30, 6)),
sg.Slider(range=(1, 100), orientation='v', size=(10, 20), default_value=25),
sg.Slider(range=(1, 100), orientation='v', size=(10, 20), default_value=75),
sg.Slider(range=(1, 100), orientation='v', size=(10, 20), default_value=10)],
[sg.Text('_' * 100, size=(70, 1))],
[sg.Text('Choose Source and Destination Folders', size=(35, 1))],
[sg.Text('Source Folder', size=(15, 1), auto_size_text=False, justification='right'), sg.InputText('Source'), sg.FolderBrowse()],
[sg.Text('Destination Folder', size=(15, 1), auto_size_text=False, justification='right'), sg.InputText('Dest'),
sg.FolderBrowse()],
[sg.Submit(), sg.Cancel(), sg.SimpleButton('Customized', button_color=('white', 'green'))]
]
button, values = form.LayoutAndRead(layout)