Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*** NEW FEATURE ** No more window.FindElement needed!! *** #1827

Closed
MikeTheWatchGuy opened this issue Aug 17, 2019 · 2 comments
Closed

*** NEW FEATURE ** No more window.FindElement needed!! *** #1827

MikeTheWatchGuy opened this issue Aug 17, 2019 · 2 comments

Comments

@MikeTheWatchGuy
Copy link
Collaborator

MikeTheWatchGuy commented Aug 17, 2019

window['key'] can replace window.FindElement('key')

This is important enough to warrant an Issue being opened to get your attention

A change was made to all 4 ports to enable this capability. I saw the suggestion less than an hour ago. This is SO cool and makes writing the code so much cleaner!

100% of the credit goes to @nngogol for this. He provided the idea and some sample code. THIS is how PySimpleGUI should be doing it!! It's making use of a specific Python feature in a way that is EASY for programmers to use.

NOTE - you can continue to call window.FindElement and Window.Element. I will be keeping those around. However the readme/User Reference, Cookbook will both change to using this new method instead of the FindElement call. Also, like so many other SDK changes, the Demo Programs will be updated over time and the new ones will use the new technique.

I'm ELATED! It's cleaner, more Pythonic, and just plain rocks. You'll still get the same error message, by the way, should you provide a bad key.

In case the heading above doesn't explain it, this will.

Previously Updating An Element:

window.FindElement(key).Update(value)

NOW Updating An Element:

window[key].Update(value)

And here's an entire program using it:

import PySimpleGUI as sg

layout = [
            [sg.Text('My layout', key='_TEXT_')],
            [sg.Input(key='_INPUT_')],
            [sg.Button('Update')]]

window = sg.Window('My new window', layout)

while True:             # Event Loop
    event, values = window.Read()
    if event is None:
        break
    window['_TEXT_'].Update(values['_INPUT_'])
@MikeTheWatchGuy
Copy link
Collaborator Author

I'll also tack onto this Issue a potentially new way of accessing an Element's Update method.

This change has not yet gone into the other ports of PySimpleGUI (Qt, etc), whereas the FindElement one has been ported.

A new way of calling Update is to make a call to whatever is returned from a FindElement call (or brackets now).

In the above example, the code was compacted using [ ] to this:

    window['_TEXT_'].Update(values['_INPUT_'])

This new, addition compaction, involves removing these characters in the above statement .Update, making the new statement:

    window['_TEXT_'](values['_INPUT_'])

Yes, that's valid. And yes, it works!

How to explain this in comments and in the User's Manual is going to be crazy. Totally understand how to explain the [ ] change, but this one of skipping the Update word is a different thing.

@MikeTheWatchGuy
Copy link
Collaborator Author

OK, this has been up long enough... Have another announcement to make :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant