New PopupNoButtons box, added "key" to all elements, form.FindElement method, Combo.Update can update list of allowed values,

This commit is contained in:
MikeTheWatchGuy 2018-09-03 23:58:56 -04:00
parent 716511b887
commit 50a69f25fc
3 changed files with 69 additions and 31 deletions

View File

@ -385,7 +385,7 @@ def MediaPlayer():
sg.Text(' ' * 2), sg.SimpleButton('Exit', button_color=(background, background),
image_filename=image_exit, image_size=(50, 50), image_subsample=2,
border_width=0)],
[sg.Text('_' * 30)],
[sg.Text('_' * 20)],
[sg.Text(' ' * 30)],
[
sg.Slider(range=(-10, 10), default_value=0, size=(10, 20), orientation='vertical',
@ -721,6 +721,9 @@ def TableSimulation():
def TightLayout():
"""
Turn off padding in order to get a really tight looking layout.
"""
import PySimpleGUI as sg
sg.ChangeLookAndFeel('Dark')
@ -765,7 +768,6 @@ fig_dict = {'Simple Data Entry':SimpleDataEntry, 'Simple Entry Return Data as Di
# multiline_elem = sg.Multiline(size=(70,35),pad=(5,(3,90)))
# define the form layout
listbox_values = [key for key in fig_dict.keys()]
multiline_elem = sg.Multiline(size=(70,35), do_not_clear=True)
while True:
sg.ChangeLookAndFeel('Dark')
@ -773,7 +775,7 @@ while True:
[sg.SimpleButton('Run'), sg.Exit()]]
layout = [[sg.Text('PySimpleGUI Coookbook', font=('current 18'))],
[sg.Column(col_listbox, pad=(5,(3,2))), multiline_elem],
[sg.Column(col_listbox, pad=(5,(3,2))), sg.Multiline(size=(70,35), do_not_clear=True, key='multi')],
]
# create the form and show it without the plot
@ -792,7 +794,7 @@ while True:
continue
if button is '':
multiline_elem.Update(inspect.getsource(func))
form.FindElement('multi').Update(inspect.getsource(func))
button, values = form.Read()
elif button is 'Run':
sg.ChangeLookAndFeel('SystemDefault')

View File

@ -7,11 +7,11 @@ import PySimpleGUI as sg
form = sg.FlexForm("Font size selector")
fontSize = 12
sampleText = sg.Text("Aa", size=(2, 1), font="Helvetica " + str(fontSize))
slider = sg.Slider(range=(6,50), orientation='h', size=(10,20), change_submits=True, key='slider')
spin = sg.Spin([sz for sz in range(4,72)], font=('Helvetica 20'), initial_value=fontSize, change_submits=True, key='spin')
layout = [
[sampleText, spin, slider],
[sg.Text("Aa", size=(2, 1), font="Helvetica " + str(fontSize), key='text'),
sg.Spin([sz for sz in range(6, 72)], font=('Helvetica 20'), initial_value=fontSize, change_submits=True,
key='spin'), sg.Slider(range=(6,50), orientation='h', size=(10,20), change_submits=True, key='slider')],
[sg.OK(), sg.Cancel()]
]
@ -25,11 +25,10 @@ while True:
sz_slider = int(values['slider'])
sz = sz_spin if sz_spin != fontSize else sz_slider
if sz != fontSize:
print(sampleText.Font, sampleText.Size)
fontSize = sz
font = "Helvetica " + str(fontSize)
sampleText.Update(font=font)
slider.Update(sz)
spin.Update(sz)
form.FindElement('text').Update(font=font)
form.FindElement('slider').Update(sz)
form.FindElement('spin').Update(sz)
print("Done.")

View File

@ -170,6 +170,7 @@ MSG_BOX_CANCELLED = 2
MSG_BOX_ERROR = 3
MSG_BOX_OK_CANCEL = 4
MSG_BOX_OK = 0
MSG_BOX_NO_BUTTONS = 5
# ---------------------------------------------------------------------- #
# Cascading structure.... Objects get larger #
@ -305,7 +306,14 @@ class InputCombo(Element):
super().__init__(ELEM_TYPE_INPUT_COMBO, scale=scale, size=size, auto_size_text=auto_size_text, background_color=bg, text_color=fg, key=key, pad=pad)
def Update(self, value):
def Update(self, value=None, values=None):
if values is not None:
try:
self.TKCombo['values'] = values
self.TKCombo.current(0)
except: pass
self.Values = values
for index, v in enumerate(self.Values):
if v == value:
try:
@ -590,7 +598,7 @@ class Multiline(Element):
# Text #
# ---------------------------------------------------------------------- #
class Text(Element):
def __init__(self, text, scale=(None, None), size=(None, None), auto_size_text=None, font=None, text_color=None, background_color=None,justification=None, pad=None):
def __init__(self, text, scale=(None, None), size=(None, None), auto_size_text=None, font=None, text_color=None, background_color=None,justification=None, pad=None, key=None):
'''
Text Element - Displays text in your form. Can be updated in non-blocking forms
:param text: The text to display
@ -609,7 +617,7 @@ class Text(Element):
bg = DEFAULT_TEXT_ELEMENT_BACKGROUND_COLOR
else:
bg = background_color
super().__init__(ELEM_TYPE_TEXT, scale, size, auto_size_text, background_color=bg, font=font if font else DEFAULT_FONT, text_color=self.TextColor, pad=pad)
super().__init__(ELEM_TYPE_TEXT, scale, size, auto_size_text, background_color=bg, font=font if font else DEFAULT_FONT, text_color=self.TextColor, pad=pad, key=key)
return
def Update(self, new_value = None, background_color=None, text_color=None, font=None):
@ -756,7 +764,7 @@ class Output(Element):
# Button Class #
# ---------------------------------------------------------------------- #
class Button(Element):
def __init__(self, button_type=BUTTON_TYPE_CLOSES_WIN, target=(None, None), button_text='', file_types=(("ALL Files", "*.*"),), image_filename=None, image_size=(None, None), image_subsample=None, border_width=None, scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None):
def __init__(self, button_type=BUTTON_TYPE_CLOSES_WIN, target=(None, None), button_text='', file_types=(("ALL Files", "*.*"),), image_filename=None, image_size=(None, None), image_subsample=None, border_width=None, scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
'''
Button Element - Specifies all types of buttons
:param button_type:
@ -787,7 +795,7 @@ class Button(Element):
self.BorderWidth = border_width if border_width is not None else DEFAULT_BORDER_WIDTH
self.BindReturnKey = bind_return_key
self.Focus = focus
super().__init__(ELEM_TYPE_BUTTON, scale=scale, size=size, font=font, pad=pad)
super().__init__(ELEM_TYPE_BUTTON, scale=scale, size=size, font=font, pad=pad, key=key)
return
def ButtonReleaseCallBack(self, parm):
@ -934,7 +942,7 @@ class ProgressBar(Element):
# Image #
# ---------------------------------------------------------------------- #
class Image(Element):
def __init__(self, filename=None, data=None,scale=(None, None), size=(None, None), pad=None):
def __init__(self, filename=None, data=None,scale=(None, None), size=(None, None), pad=None, key=None):
'''
Image Element
:param filename:
@ -947,7 +955,7 @@ class Image(Element):
if data is None and filename is None:
print('* Warning... no image specified in Image Element! *')
super().__init__(ELEM_TYPE_IMAGE, scale=scale, size=size, pad=pad)
super().__init__(ELEM_TYPE_IMAGE, scale=scale, size=size, pad=pad, key=key)
return
def Update(self, filename=None, data=None):
@ -971,11 +979,11 @@ class Image(Element):
# Canvas #
# ---------------------------------------------------------------------- #
class Canvas(Element):
def __init__(self, canvas=None, background_color=None, scale=(None, None), size=(None, None), pad=None):
def __init__(self, canvas=None, background_color=None, scale=(None, None), size=(None, None), pad=None, key=None):
self.BackgroundColor = background_color if background_color is not None else DEFAULT_BACKGROUND_COLOR
self.TKCanvas = canvas
super().__init__(ELEM_TYPE_CANVAS, background_color=background_color, scale=scale, size=size, pad=pad)
super().__init__(ELEM_TYPE_CANVAS, background_color=background_color, scale=scale, size=size, pad=pad, key=key)
return
def __del__(self):
@ -986,11 +994,11 @@ class Canvas(Element):
# Frame #
# ---------------------------------------------------------------------- #
class Frame(Element):
def __init__(self, frame=None, background_color=None, scale=(None, None), size=(None, None), pad=None):
def __init__(self, frame=None, background_color=None, scale=(None, None), size=(None, None), pad=None, key=None):
self.BackgroundColor = background_color if background_color is not None else DEFAULT_BACKGROUND_COLOR
self.TKFrame = frame
super().__init__(ELEM_TYPE_FRAME, background_color=background_color, scale=scale, size=size, pad=pad)
super().__init__(ELEM_TYPE_FRAME, background_color=background_color, scale=scale, size=size, pad=pad, key=key)
return
def __del__(self):
@ -1348,6 +1356,10 @@ class FlexForm:
def Fill(self, values_dict):
FillFormWithValues(self, values_dict)
def FindElement(self, key):
return _FindElementFromKeyInSubForm(self, key)
def GetScreenDimensions(self):
if self.TKrootDestroyed:
return None, None
@ -1559,20 +1571,20 @@ def No(button_text='No', scale=(None, None), size=(None, None), auto_size_button
# ------------------------- GENERIC BUTTON Element lazy function ------------------------- #
# this is the only button that REQUIRES button text field
def SimpleButton(button_text, image_filename=None, image_size=(None, None), image_subsample=None, border_width=None, scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None):
return Button(BUTTON_TYPE_CLOSES_WIN, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, button_text=button_text, border_width=border_width, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad)
def SimpleButton(button_text, image_filename=None, image_size=(None, None), image_subsample=None, border_width=None, scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
return Button(BUTTON_TYPE_CLOSES_WIN, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, button_text=button_text, border_width=border_width, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key)
# ------------------------- GENERIC BUTTON Element lazy function ------------------------- #
# this is the only button that REQUIRES button text field
def ReadFormButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None):
return Button(BUTTON_TYPE_READ_FORM, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad)
def ReadFormButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
return Button(BUTTON_TYPE_READ_FORM, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key)
def RealtimeButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None):
return Button(BUTTON_TYPE_REALTIME, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad)
def RealtimeButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
return Button(BUTTON_TYPE_REALTIME, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key)
# ------------------------- GENERIC BUTTON Element lazy function ------------------------- #
# this is the only button that REQUIRES button text field
def DummyButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None):
return Button(BUTTON_TYPE_CLOSES_WIN_ONLY, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad)
def DummyButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
return Button(BUTTON_TYPE_CLOSES_WIN_ONLY, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key)
##################################### ----- RESULTS ------ ##################################################
@ -1745,6 +1757,18 @@ def FillSubformWithValues(form, values_dict):
elif element.Type == ELEM_TYPE_INPUT_SPIN:
element.Update(value)
def _FindElementFromKeyInSubForm(form, key):
for row_num, row in enumerate(form.Rows):
for col_num, element in enumerate(row):
if element.Type == ELEM_TYPE_COLUMN:
matching_elem = _FindElementFromKeyInSubForm(element, key)
if matching_elem is not None:
return matching_elem
if element.Key == key:
return element
# ------------------------------------------------------------------------------------------------------------------ #
# ===================================== TK CODE STARTS HERE ====================================================== #
# ------------------------------------------------------------------------------------------------------------------ #
@ -2395,6 +2419,8 @@ def Popup(*args, button_color=None, button_type=MSG_BOX_OK, auto_close=False, au
elif button_type is MSG_BOX_OK_CANCEL:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True),
PopupButton('Cancel', size=(5, 1), button_color=button_color))
elif button_type is MSG_BOX_NO_BUTTONS:
pass
else:
form.AddRow(Text('', size=(pad, 1), auto_size_text=False), PopupButton('OK', size=(5, 1), button_color=button_color, focus=True, bind_return_key=True))
@ -2413,6 +2439,12 @@ def Popup(*args, button_color=None, button_type=MSG_BOX_OK, auto_close=False, au
# MsgBox is the legacy call and show not be used any longer
MsgBox = Popup
# --------------------------- PopupNonBlocking ---------------------------
def PopupoNoButtons(*args, button_color=None, auto_close=False, auto_close_duration=None, font=None):
Popup(*args, button_type=MSG_BOX_NO_BUTTONS, button_color=button_color, auto_close=auto_close, auto_close_duration=auto_close_duration, font=font)
return
# --------------------------- PopupNonBlocking ---------------------------
def PopupoNonBlocking(*args, button_color=None, auto_close=False, auto_close_duration=None, font=None):
Popup(*args, non_blocking=True, button_color=button_color, auto_close=auto_close, auto_close_duration=auto_close_duration, font=font)
@ -3133,6 +3165,11 @@ def ChangeLookAndFeel(index):
'PROGRESS': DEFAULT_PROGRESS_BAR_COLOR, 'BORDER': 1, 'SLIDER_DEPTH': 0,
'PROGRESS_DEPTH': 0},
'Dark2': {'BACKGROUND': 'gray25', 'TEXT': 'white', 'INPUT': 'white',
'TEXT_INPUT': 'black', 'SCROLL': 'gray44', 'BUTTON': ('white', '#004F00'),
'PROGRESS': DEFAULT_PROGRESS_BAR_COLOR, 'BORDER': 1, 'SLIDER_DEPTH': 0,
'PROGRESS_DEPTH': 0},
'Black': {'BACKGROUND': 'black', 'TEXT': 'white', 'INPUT': 'gray30',
'TEXT_INPUT': 'white', 'SCROLL': 'gray44', 'BUTTON': ('black', 'white'),
'PROGRESS': DEFAULT_PROGRESS_BAR_COLOR, 'BORDER': 1, 'SLIDER_DEPTH': 0,