New options for PopupGetFile (save_as, no_window), added key to buttons, New Demo_Fill_form from JFong
This commit is contained in:
parent
ab92627916
commit
16b1c8310d
2 changed files with 100 additions and 64 deletions
|
@ -1,52 +1,75 @@
|
|||
# from tkinter.filedialog import asksaveasfilename, askopenfilename
|
||||
import pickle
|
||||
import PySimpleGUI as sg
|
||||
|
||||
"""
|
||||
Show a complex form, save the initial values
|
||||
When the "Fill" button is clicked, the form will reset to the initial values
|
||||
"""
|
||||
|
||||
def save(values):
|
||||
sfilename = sg.PopupGetFile('Save Settings', save_as=True, no_window=True)
|
||||
if not sfilename:
|
||||
return
|
||||
with open(sfilename, 'wb') as sf:
|
||||
pickle.dump(values, sf)
|
||||
|
||||
|
||||
def load(form):
|
||||
dfilename = sg.PopupGetFile('Load Settings', no_window=True)
|
||||
if not dfilename:
|
||||
return
|
||||
with open(dfilename, 'rb') as df:
|
||||
form.Fill(pickle.load(df))
|
||||
|
||||
|
||||
def Everything():
|
||||
sg.ChangeLookAndFeel('Dark')
|
||||
sg.ChangeLookAndFeel('TanBlue')
|
||||
|
||||
|
||||
column1 = [[sg.Text('Column 1', background_color='black', 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')]]
|
||||
column1 = [
|
||||
[sg.Text('Column 1', background_color=sg.DEFAULT_BACKGROUND_COLOR, justification='center', size=(10, 1))],
|
||||
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 1', key='spin1')],
|
||||
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2', key='spin2')],
|
||||
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3', key='spin3')]]
|
||||
|
||||
layout = [
|
||||
[sg.Text('All graphic widgets in one form!', size=(30, 1), font=("Helvetica", 25))],
|
||||
[sg.Text('Here is some text.... and a place to enter text')],
|
||||
[sg.InputText('This is my text')],
|
||||
[sg.Checkbox('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 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.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, key='slider'),
|
||||
sg.Column(column1, background_color='black')],
|
||||
[sg.InputText('This is my text', key='in1', do_not_clear=True)],
|
||||
[sg.Checkbox('Checkbox', key='cb1'), sg.Checkbox('My second checkbox!', key='cb2', default=True)],
|
||||
[sg.Radio('My first Radio! ', "RADIO1", key='rad1', default=True),
|
||||
sg.Radio('My second Radio!', "RADIO1", key='rad2')],
|
||||
[sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3),
|
||||
key='multi1', do_not_clear=True),
|
||||
sg.Multiline(default_text='A second multi-line', size=(35, 3), key='multi2', do_not_clear=True)],
|
||||
[sg.InputCombo(('Combobox 1', 'Combobox 2'), key='combo', size=(20, 1)),
|
||||
sg.Slider(range=(1, 100), orientation='h', size=(34, 20), key='slide1', default_value=85)],
|
||||
[sg.InputOptionMenu(('Menu Option 1', 'Menu Option 2', 'Menu Option 3'), key='optionmenu')],
|
||||
[sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3), key='listbox'),
|
||||
sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25, key='slide2', ),
|
||||
sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75, key='slide3', ),
|
||||
sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10, key='slide4'),
|
||||
sg.Column(column1, background_color='gray34')],
|
||||
[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.ReadFormButton('Fill'), sg.Cancel()]
|
||||
sg.InputText('Default Folder', key='folder', do_not_clear=True), sg.FolderBrowse()],
|
||||
[sg.ReadFormButton('Exit'),
|
||||
sg.Text(' ' * 40), sg.ReadFormButton('SaveSettings'), sg.ReadFormButton('LoadSettings')]
|
||||
]
|
||||
|
||||
form = sg.FlexForm('Form Fill Demonstration', default_element_size=(40, 1))
|
||||
# button, values = form.LayoutAndRead(layout, non_blocking=True)
|
||||
form.Layout(layout)
|
||||
button, initial_values = form.ReadNonBlocking()
|
||||
|
||||
while True:
|
||||
button, values = form.Read()
|
||||
if button in (None, 'Cancel'):
|
||||
|
||||
if button is 'SaveSettings':
|
||||
save(values)
|
||||
elif button is 'LoadSettings':
|
||||
load(form)
|
||||
elif button in ['Exit', None]:
|
||||
break
|
||||
form.Fill(initial_values) # fill form with whatever initial values were
|
||||
|
||||
sg.Popup('Title', 'The results of the form.', 'The button clicked was "{}"'.format(button), 'The values are', values)
|
||||
# form.CloseNonBlockingForm()
|
||||
|
||||
Everything()
|
||||
|
||||
if __name__ == '__main__':
|
||||
Everything()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue