Global change of FindElement to Element

This commit is contained in:
MikeTheWatchGuy 2019-05-02 11:01:21 -04:00
parent ced4e14359
commit e9428ae95b
3 changed files with 106 additions and 104 deletions

View File

@ -120,7 +120,7 @@ while True: # Event Loop
break
if event == 'Show':
# change the "output" element to be the value of "input" element
window.FindElement('_OUTPUT_').Update(values['_IN_'])
window.Element('_OUTPUT_').Update(values['_IN_'])
window.Close()
```
@ -148,7 +148,7 @@ Same GUI screen except the return values are in a list instead of a dictionary a
window = sg.Window('Simple data entry window', layout)
event, values = window.Read()
window.Close()
window.Close()
print(event, values[0], values[1], values[2])
```
@ -343,7 +343,7 @@ while True:
break
elif event == 'Start/Stop':
timer_running = not timer_running
window.FindElement('_OUTPUT_').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
window.Element('_OUTPUT_').Update('{:02d}:{:02d}.{:02d}'.format((i // 100) // 60, (i // 100) % 60, i % 100))
```
<iframe height="800px" width="100%" src="https://repl.it/@PySimpleGUI/Cookbook-Timer?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
@ -517,7 +517,7 @@ def MediaPlayerGUI():
break
# If a button was pressed, display it on the GUI by updating the text element
if event != sg.TIMEOUT_KEY:
window.FindElement('output').Update(event)
window.Element('output').Update(event)
MediaPlayerGUI()
```
@ -660,7 +660,7 @@ for i in range(1000):
if event == 'Cancel' or event is None:
break
# update bar with loop value +1 so that bar eventually reaches the maximum
window.FindElement('progbar').UpdateBar(i + 1)
window.Element('progbar').UpdateBar(i + 1)
# done with loop... need to destroy the window as it's still open
window.Close()
```
@ -762,7 +762,7 @@ This simple program keep a window open, taking input values until the user termi
except:
calc = 'Invalid'
window.FindElement('output').Update(calc)
window.Element('output').Update(calc)
else:
break
```
@ -820,7 +820,7 @@ while True:
ev1, vals1 = win1.Read(timeout=100)
if ev1 is None:
break
win1.FindElement('_OUTPUT_').Update(vals1[0])
win1.Element('_OUTPUT_').Update(vals1[0])
if ev1 == 'Launch 2' and not win2_active:
win2_active = True
@ -863,7 +863,7 @@ While it's fun to scribble on a Canvas Widget, try Graph Element makes it a down
window = sg.Window('Canvas test', layout)
window.Finalize()
canvas = window.FindElement('canvas')
canvas = window.Element('canvas')
cir = canvas.TKCanvas.create_oval(50, 50, 100, 100)
while True:
@ -893,7 +893,7 @@ Just like you can draw on a tkinter widget, you can also draw on a Graph Element
window = sg.Window('Graph test', layout)
window.Finalize()
graph = window.FindElement('graph')
graph = window.Element('graph')
circle = graph.DrawCircle((75,75), 25, fill_color='black',line_color='white')
point = graph.DrawPoint((75,75), 10, color='green')
oval = graph.DrawOval((25,300), (100,280), fill_color='purple', line_color='purple' )
@ -965,9 +965,9 @@ There are a number of features used in this Recipe including:
keys_entered += event # add the new digit
elif event == 'Submit':
keys_entered = values['input']
window.FindElement('out').Update(keys_entered) # output the final string
window.Element('out').Update(keys_entered) # output the final string
window.FindElement('input').Update(keys_entered) # change the window to reflect current key string
window.Element('input').Update(keys_entered) # change the window to reflect current key string
```
<iframe height="800px" width="100%" src="https://repl.it/@PySimpleGUI/Cookbook-Keypad-Touchscreen-Entry?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
@ -1006,7 +1006,7 @@ layout = [[sg.Text('Animated Matplotlib', size=(40, 1), justification='center',
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI', layout)
window.Finalize() # needed to access the canvas element prior to reading the window
canvas_elem = window.FindElement('canvas')
canvas_elem = window.Element('canvas')
graph = FigureCanvasTkAgg(fig, master=canvas_elem.TKCanvas)
canvas = canvas_elem.TKCanvas
@ -1070,9 +1070,9 @@ In other GUI frameworks this program would be most likely "event driven" with ca
window = sg.Window("Time Tracker", layout, default_element_size=(12,1), text_justification='r', auto_size_text=False, auto_size_buttons=False,
default_button_element_size=(12,1))
window.Finalize()
window.FindElement('Stop').Update(disabled=True)
window.FindElement('Reset').Update(disabled=True)
window.FindElement('Submit').Update(disabled=True)
window.Element('Stop').Update(disabled=True)
window.Element('Reset').Update(disabled=True)
window.Element('Submit').Update(disabled=True)
recording = have_data = False
while True:
event, values = window.Read()
@ -1080,29 +1080,29 @@ In other GUI frameworks this program would be most likely "event driven" with ca
if event is None:
exit(69)
if event is 'Start':
window.FindElement('Start').Update(disabled=True)
window.FindElement('Stop').Update(disabled=False)
window.FindElement('Reset').Update(disabled=False)
window.FindElement('Submit').Update(disabled=True)
window.Element('Start').Update(disabled=True)
window.Element('Stop').Update(disabled=False)
window.Element('Reset').Update(disabled=False)
window.Element('Submit').Update(disabled=True)
recording = True
elif event is 'Stop' and recording:
window.FindElement('Stop').Update(disabled=True)
window.FindElement('Start').Update(disabled=False)
window.FindElement('Submit').Update(disabled=False)
window.Element('Stop').Update(disabled=True)
window.Element('Start').Update(disabled=False)
window.Element('Submit').Update(disabled=False)
recording = False
have_data = True
elif event is 'Reset':
window.FindElement('Stop').Update(disabled=True)
window.FindElement('Start').Update(disabled=False)
window.FindElement('Submit').Update(disabled=True)
window.FindElement('Reset').Update(disabled=False)
window.Element('Stop').Update(disabled=True)
window.Element('Start').Update(disabled=False)
window.Element('Submit').Update(disabled=True)
window.Element('Reset').Update(disabled=False)
recording = False
have_data = False
elif event is 'Submit' and have_data:
window.FindElement('Stop').Update(disabled=True)
window.FindElement('Start').Update(disabled=False)
window.FindElement('Submit').Update(disabled=True)
window.FindElement('Reset').Update(disabled=False)
window.Element('Stop').Update(disabled=True)
window.Element('Start').Update(disabled=False)
window.Element('Submit').Update(disabled=True)
window.Element('Reset').Update(disabled=False)
recording = False
```
@ -1151,7 +1151,7 @@ Use the upper half to generate your hash code. Then paste it into the code in t
sha1hash = hashlib.sha1()
sha1hash.update(password_utf)
password_hash = sha1hash.hexdigest()
window.FindElement('hash').Update(password_hash)
window.Element('hash').Update(password_hash)
except:
pass
@ -1218,7 +1218,7 @@ You can easily change colors to match your background by changing a couple of pa
def Launcher():
def print(line):
window.FindElement('output').Update(line)
window.Element('output').Update(line)
sg.ChangeLookAndFeel('Dark')
@ -1331,7 +1331,7 @@ while (True):
else:
event, values = window.Read()
if event == 'button':
event = window.FindElement(event).GetText()
event = window.Element(event).GetText()
# --------- Do Button Operations --------
if event is None or event == 'Exit': # ALWAYS give a way out of program
break
@ -1342,16 +1342,16 @@ while (True):
elif event == 'Pause':
paused = True
paused_time = int(round(time.time() * 100))
element = window.FindElement('button')
element = window.Element('button')
element.Update(text='Run')
elif event == 'Run':
paused = False
start_time = start_time + int(round(time.time() * 100)) - paused_time
element = window.FindElement('button')
element = window.Element('button')
element.Update(text='Pause')
# --------- Display timer in window --------
window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
window.Element('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
(current_time // 100) % 60,
current_time % 100))
```
@ -1399,7 +1399,7 @@ while (True):
# --------- Display timer in window --------
window.FindElement('text').Update(f'CPU {cpu_percent:02.0f}%')
window.Element('text').Update(f'CPU {cpu_percent:02.0f}%')
# Broke out of main loop. Close the window.
window.Close()
@ -1468,7 +1468,7 @@ import PySimpleGUI as sg
layout = [[sg.Graph(canvas_size=(400, 400), graph_bottom_left=(-105,-105), graph_top_right=(105,105), background_color='white', key='graph', tooltip='This is a cool graph!')],]
window = sg.Window('Graph of Sine Function', layout, grab_anywhere=True).Finalize()
graph = window.FindElement('graph')
graph = window.Element('graph')
# Draw axis
graph.DrawLine((-100,0), (100,0))

View File

@ -1048,7 +1048,7 @@ while True: # Event Loop
break
if event == 'Show':
# change the "output" element to be the value of "input" element
window.FindElement('_OUTPUT_').Update(values['_IN_'])
window.Element('_OUTPUT_').Update(values['_IN_'])
window.Close()
```
@ -1567,7 +1567,8 @@ There are a few methods (functions) that you will see in this document that act
window.Close() - To close your window, if a button hasn't already closed it
window.Disable() - Use to disable the window inputwhen opening another window on top of the primnary Window
window.Enable() - Re-enable a Disabled window
window.FindElement(key, silent_on_error=False) - Returns the element that has a matching key value
window.FindElement(key, silent_on_error=False) - Returns the element that has a matching key value
window.Element(key, silent_on_error=False) - EXACTLY the same as calling FindElement
window.Move(x,y) - Moves window to location x,y on screen'
window.SetAlpha(alpha) - Changes window transparency
window.BringToFront() - Brings the window to the top of other windows on the screen
@ -1820,24 +1821,24 @@ Many of the main method calls and Element names have shortcuts. This enables yo
The most basic element is the Text element. It simply displays text. Many of the 'options' that can be set for a Text element are shared by other elements.
```python
Text(text,
size=(None, None),
auto_size_text=None,
click_submits=False,
size=(None, None),
auto_size_text=None,
click_submits=False,
enable_events=False,
relief=None,
font=None,
text_color=None,
background_color=None,
justification=None,
pad=None,
key=None,
right_click_menu=None,
tooltip=None,
visible=True)
```
.
Text - The text that's displayed
font=None,
text_color=None,
background_color=None,
justification=None,
pad=None,
key=None,
right_click_menu=None,
tooltip=None,
visible=True)
```
.
Text - The text that's displayed
size - Element's size
click_submits - if clicked will cause a read call to return the key value of the button
enable_events - same as click_submits
@ -1920,8 +1921,8 @@ Multiline( default_text='',
tooltip=None
right_click_menu=None,
visible=True)
'''
```
```
default_text - Text to display in the text box
change_submits - Bool. If True, pressing Enter key submits window
@ -2840,7 +2841,7 @@ Another way of using a Progress Meter with PySimpleGUI is to build a custom wind
# create the window`
window = sg.Window('Custom Progress Meter', layout)
progress_bar = window.FindElement('progressbar')
progress_bar = window.Element('progressbar')
# loop that would normally do something useful
for i in range(10000):
# check to see if the cancel button was clicked and exit loop if clicked
@ -3052,7 +3053,7 @@ The order of operations to obtain a tkinter Canvas Widget is:
# add the plot to the window
fig_photo = draw_figure(window.FindElement('canvas').TKCanvas, fig)
fig_photo = draw_figure(window.Element('canvas').TKCanvas, fig)
# show it all again and get buttons
event, values = window.Read()
@ -3890,7 +3891,7 @@ See the sample code on the GitHub named Demo Media Player for another example of
event, values = window.Read(timeout=10)
current_time = int(round(time.time() * 100)) - start_time
# --------- Display timer in window --------
window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
window.Element('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
(current_time // 100) % 60,
current_time % 100))
```
@ -4000,9 +4001,9 @@ In some programs these updates happen in response to another Element. This prog
if sz != fontSize:
fontSize = sz
font = "Helvetica " + str(fontSize)
window.FindElement('text').Update(font=font)
window.FindElement('slider').Update(sz)
window.FindElement('spin').Update(sz)
window.Element('text').Update(font=font)
window.Element('slider').Update(sz)
window.Element('spin').Update(sz)
print("Done.")
@ -4012,15 +4013,15 @@ For example, `values['slider']` is the value of the Slider Element.
This program changes all 3 elements if either the Slider or the Spinner changes. This is done with these statements:
window.FindElement('text').Update(font=font)
window.FindElement('slider').Update(sz)
window.FindElement('spin').Update(sz)
window.Element('text').Update(font=font)
window.Element('slider').Update(sz)
window.Element('spin').Update(sz)
Remember this design pattern because you will use it OFTEN if you use persistent windows.
It works as follows. The call to `window.FindElement` returns the Element object represented by they provided `key`. This element is then updated by calling it's `Update` method. This is another example of Python's "chaining" feature. We could write this code using the long-form:
text_element = window.FindElement('text')
text_element = window.Element('text')
text_element.Update(font=font)
The takeaway from this exercise is that keys are key in PySimpleGUI's design. They are used to both read the values of the window and also to identify elements. As already mentioned, they are used as targets in Button calls.
@ -4215,7 +4216,7 @@ win1 = sg.Window('Window 1', layout)
win2_active = False
while True:
ev1, vals1 = win1.Read(timeout=100)
win1.FindElement('_OUTPUT_').Update(vals1[0])
win1.Element('_OUTPUT_').Update(vals1[0])
if ev1 is None or ev1 == 'Exit':
break
@ -4252,7 +4253,7 @@ while True:
ev1, vals1 = win1.Read(timeout=100)
if ev1 is None:
break
win1.FindElement('_OUTPUT_').Update(vals1[0])
win1.Element('_OUTPUT_').Update(vals1[0])
if ev1 == 'Launch 2' and not win2_active:
win2_active = True
@ -4964,7 +4965,7 @@ It seemed quite natural to use Python's powerful list constructs when possible.
**Dictionaries**
Want to view your form's results as a dictionary instead of a list... no problem, just use the `key` keyword on your elements. For complex forms with a lot of values that need to be changed frequently, this is by far the best way of consuming the results.
You can also look up elements using their keys. This is an excellent way to update elements in reaction to another element. Call `form.FindElement(key)` to get the Element.
You can also look up elements using their keys. This is an excellent way to update elements in reaction to another element. Call `form.Element(key)` to get the Element.
**Named / Optional Parameters**
This is a language feature that is featured **heavily** in all of the API calls, both functions and classes. Elements are configured, in-place, by setting one or more optional parameters. For example, a Text element's color is chosen by setting the optional `text_color` parameter.

View File

@ -1048,7 +1048,7 @@ while True: # Event Loop
break
if event == 'Show':
# change the "output" element to be the value of "input" element
window.FindElement('_OUTPUT_').Update(values['_IN_'])
window.Element('_OUTPUT_').Update(values['_IN_'])
window.Close()
```
@ -1567,7 +1567,8 @@ There are a few methods (functions) that you will see in this document that act
window.Close() - To close your window, if a button hasn't already closed it
window.Disable() - Use to disable the window inputwhen opening another window on top of the primnary Window
window.Enable() - Re-enable a Disabled window
window.FindElement(key, silent_on_error=False) - Returns the element that has a matching key value
window.FindElement(key, silent_on_error=False) - Returns the element that has a matching key value
window.Element(key, silent_on_error=False) - EXACTLY the same as calling FindElement
window.Move(x,y) - Moves window to location x,y on screen'
window.SetAlpha(alpha) - Changes window transparency
window.BringToFront() - Brings the window to the top of other windows on the screen
@ -1820,24 +1821,24 @@ Many of the main method calls and Element names have shortcuts. This enables yo
The most basic element is the Text element. It simply displays text. Many of the 'options' that can be set for a Text element are shared by other elements.
```python
Text(text,
size=(None, None),
auto_size_text=None,
click_submits=False,
size=(None, None),
auto_size_text=None,
click_submits=False,
enable_events=False,
relief=None,
font=None,
text_color=None,
background_color=None,
justification=None,
pad=None,
key=None,
right_click_menu=None,
tooltip=None,
visible=True)
```
.
Text - The text that's displayed
font=None,
text_color=None,
background_color=None,
justification=None,
pad=None,
key=None,
right_click_menu=None,
tooltip=None,
visible=True)
```
.
Text - The text that's displayed
size - Element's size
click_submits - if clicked will cause a read call to return the key value of the button
enable_events - same as click_submits
@ -1920,8 +1921,8 @@ Multiline( default_text='',
tooltip=None
right_click_menu=None,
visible=True)
'''
```
```
default_text - Text to display in the text box
change_submits - Bool. If True, pressing Enter key submits window
@ -2840,7 +2841,7 @@ Another way of using a Progress Meter with PySimpleGUI is to build a custom wind
# create the window`
window = sg.Window('Custom Progress Meter', layout)
progress_bar = window.FindElement('progressbar')
progress_bar = window.Element('progressbar')
# loop that would normally do something useful
for i in range(10000):
# check to see if the cancel button was clicked and exit loop if clicked
@ -3052,7 +3053,7 @@ The order of operations to obtain a tkinter Canvas Widget is:
# add the plot to the window
fig_photo = draw_figure(window.FindElement('canvas').TKCanvas, fig)
fig_photo = draw_figure(window.Element('canvas').TKCanvas, fig)
# show it all again and get buttons
event, values = window.Read()
@ -3890,7 +3891,7 @@ See the sample code on the GitHub named Demo Media Player for another example of
event, values = window.Read(timeout=10)
current_time = int(round(time.time() * 100)) - start_time
# --------- Display timer in window --------
window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
window.Element('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
(current_time // 100) % 60,
current_time % 100))
```
@ -4000,9 +4001,9 @@ In some programs these updates happen in response to another Element. This prog
if sz != fontSize:
fontSize = sz
font = "Helvetica " + str(fontSize)
window.FindElement('text').Update(font=font)
window.FindElement('slider').Update(sz)
window.FindElement('spin').Update(sz)
window.Element('text').Update(font=font)
window.Element('slider').Update(sz)
window.Element('spin').Update(sz)
print("Done.")
@ -4012,15 +4013,15 @@ For example, `values['slider']` is the value of the Slider Element.
This program changes all 3 elements if either the Slider or the Spinner changes. This is done with these statements:
window.FindElement('text').Update(font=font)
window.FindElement('slider').Update(sz)
window.FindElement('spin').Update(sz)
window.Element('text').Update(font=font)
window.Element('slider').Update(sz)
window.Element('spin').Update(sz)
Remember this design pattern because you will use it OFTEN if you use persistent windows.
It works as follows. The call to `window.FindElement` returns the Element object represented by they provided `key`. This element is then updated by calling it's `Update` method. This is another example of Python's "chaining" feature. We could write this code using the long-form:
text_element = window.FindElement('text')
text_element = window.Element('text')
text_element.Update(font=font)
The takeaway from this exercise is that keys are key in PySimpleGUI's design. They are used to both read the values of the window and also to identify elements. As already mentioned, they are used as targets in Button calls.
@ -4215,7 +4216,7 @@ win1 = sg.Window('Window 1', layout)
win2_active = False
while True:
ev1, vals1 = win1.Read(timeout=100)
win1.FindElement('_OUTPUT_').Update(vals1[0])
win1.Element('_OUTPUT_').Update(vals1[0])
if ev1 is None or ev1 == 'Exit':
break
@ -4252,7 +4253,7 @@ while True:
ev1, vals1 = win1.Read(timeout=100)
if ev1 is None:
break
win1.FindElement('_OUTPUT_').Update(vals1[0])
win1.Element('_OUTPUT_').Update(vals1[0])
if ev1 == 'Launch 2' and not win2_active:
win2_active = True
@ -4964,7 +4965,7 @@ It seemed quite natural to use Python's powerful list constructs when possible.
**Dictionaries**
Want to view your form's results as a dictionary instead of a list... no problem, just use the `key` keyword on your elements. For complex forms with a lot of values that need to be changed frequently, this is by far the best way of consuming the results.
You can also look up elements using their keys. This is an excellent way to update elements in reaction to another element. Call `form.FindElement(key)` to get the Element.
You can also look up elements using their keys. This is an excellent way to update elements in reaction to another element. Call `form.Element(key)` to get the Element.
**Named / Optional Parameters**
This is a language feature that is featured **heavily** in all of the API calls, both functions and classes. Elements are configured, in-place, by setting one or more optional parameters. For example, a Text element's color is chosen by setting the optional `text_color` parameter.