Python Forum
[PySimpleGUI] Tutorial - Name, Address, Phone GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PySimpleGUI] Tutorial - Name, Address, Phone GUI
#31
(Aug-09-2018, 03:40 AM)jfong Wrote: Both have the same error.

I had try it on three different system, Win8.1, Win7 and Vista, using Python v3.4.4. All has the same error. But it's fine when using Python v3.6.3 on the Win7. So it seems to me that this problem is of Python:-)

...
WOW, it's fast. Nice work!
Why thank you! I hope you like the new changes.

PLEASE keep me up to date on anything you find with the 3.4.4 problem.
I will try and get 3.4.4 loaded on my Win10 system to do some troubleshooting. It's odd that it's not happening on my Raspberry Pi.
Reply
#32
(Aug-04-2018, 11:05 AM)jfong Wrote: Can you add a "focus" option for some elements? just like some widgets the tkinter has.

I just added a new "focus" option on 3 of the elements:
Input single line
Input multi line
Buttons

If no focus is manually set, then the first input or button field will be used as the focus, same as today.


I also added a new "bind_return_key" option for buttons. This allows the user to set exactly which button will react to the return key being pressed.

These changes will roll into the 2.8 release on PyPI. Prior to the official post, I am uploading to GitHub so it can be tested a little first.

Hoping it doesn't break anything!
Reply
#33
A change to how dictionaries work.

You no longer need to specify when creating the form that it should return a dictionary. If ANY element in the form that has a key specified will cause all of the elements to return a dictionary entry.

If one element has a key and others do not, then the other fields are simply numbered starting at 0. That number is used as the key for the field.

For example:

import PySimpleGUI as sg

layout = [
          [sg.Text('Please enter your Name, Address, Phone')],
          [sg.Text('Name', size=(15, 1)), sg.InputText('1')],
          [sg.Text('Address', size=(15, 1)), sg.InputText('2', key='address')],
          [sg.Text('Phone', size=(15, 1)), sg.InputText('3', key='phone')],
          [sg.Submit(), sg.Cancel()]
         ]

button, values = form.LayoutAndRead(layout)

sg.MsgBox(button, values, values[0], values['address'], values['phone'])
Two of the Input fields have keys but the first one doesn't. That means that the first field will be returned with a dictionary key of 0.
Reply
#34
(Aug-09-2018, 03:42 PM)PySimpleGUI Wrote: A change to how dictionaries work.

You no longer need to specify when creating the form that it should return a dictionary. If ANY element in the form that has a key specified will cause all of the elements to return a dictionary entry.

If one element has a key and others do not, then the other fields are simply numbered starting at 0. That number is used as the key for the field.

For example:

import PySimpleGUI as sg

layout = [
          [sg.Text('Please enter your Name, Address, Phone')],
          [sg.Text('Name', size=(15, 1)), sg.InputText('1')],
          [sg.Text('Address', size=(15, 1)), sg.InputText('2', key='address')],
          [sg.Text('Phone', size=(15, 1)), sg.InputText('3', key='phone')],
          [sg.Submit(), sg.Cancel()]
         ]

button, values = form.LayoutAndRead(layout)

sg.MsgBox(button, values, values[0], values['address'], values['phone'])
Two of the Input fields have keys but the first one doesn't. That means that the first field will be returned with a dictionary key of 0.

Inspired by your new change, allow me to push it one step further.

If none of the elements has a key, then all fields are simply numbered starting at 0.

This means that values is always returned as a dictionary, get rid of the confusion between list and dictionary. With this change, for those already written scripts which treat values as a list, the only required modification is where items were accessed through unpack, such as:

name, address, phone = values

modified to

name, address, phone = values.values()

If item was accessed by index, then no change at all.

name = values[0]

it's valid on both list and dictionary.
Reply
#35
(Aug-10-2018, 01:31 AM)jfong Wrote: Inspired by your new change, allow me to push it one step further.

If none of the elements has a key, then all fields are simply numbered starting at 0.

This means that values is always returned as a dictionary, get rid of the confusion between list and dictionary. With this change, for those already written scripts which treat values as a list, the only required modification is where items were accessed through unpack, such as:

name, address, phone = values

modified to

name, address, phone = values.values()

If item was accessed by index, then no change at all.

name = values[0]

it's valid on both list and dictionary.

And you're certainly inspiring me to keep pushing this package.

I got a little help from a buddy on Pythonista Cafe. He came up with a "ListDict" class which is based on Ordered Dict.

Check out this bit of Python magic!
class ListDict(OrderedDict):
    def __iter__(self):
        for v in self.values():
            yield v
            
    def __getitem__(self, item):
        if isinstance(item, slice):
            return list(self.values())[item]
        else:
            return super().__getitem__(item)
This way there is no need to call value.values. I can access the return values in both dictionary style and as a list! I think it's kinda brilliant. I can't take credit for it however :-) But I can freely use it!
Reply
#36
(Aug-10-2018, 05:44 PM)PySimpleGUI Wrote: Check out this bit of Python magic!
class ListDict(OrderedDict):
    def __iter__(self):
        for v in self.values():
            yield v
            
    def __getitem__(self, item):
        if isinstance(item, slice):
            return list(self.values())[item]
        else:
            return super().__getitem__(item)
This way there is no need to call value.values. I can access the return values in both dictionary style and as a list! I think it's kinda brilliant. I can't take credit for it however :-) But I can freely use it!

Why bring complexity into this situation when you have a simple soultion? The ListDict may be a cute design but is verbose here and creates extra obstacle to the user.
Reply
#37
(Aug-11-2018, 02:05 AM)jfong Wrote: [ Why bring complexity into this situation when you have a simple soultion? The ListDict may be a cute design but is verbose here and creates extra obstacle to the user.

It actually doesn't bring any additional complexity from a caller's standpoint. They never even know it's a not a dictionary or a list. If the caller specifies a key, then the return values act like a dictionary. If no keys are specified, it acts like a list.

When printed, for example, it will print as either a list or a dict. When it comes to syntax, the code looks exactly the same for both the user wishing for a list and for the user wanting to use a dict. Try out the latest version on GitHub and you'll see what I mean. Try to ignore the implementation detail and instead focus on the syntax.
Reply
#38
Run the latest version (downloaded from github where files were dated 2018/08/11 16:48) under Python 3.4, I got the following error.

D:\Works\Python\PySimpleGUI-master>python demo_dictionary.py
....
....
File "D:\Works\Python\PySimpleGUI-master\PySimpleGUI.py", line 1149, in __iter__
for v in self.values():
File "C:\Python34\lib\_collections_abc.py", line 512, in __iter__
for key in self._mapping:
File "D:\Works\Python\PySimpleGUI-master\PySimpleGUI.py", line 1149, in __iter__
for v in self.values():
File "C:\Python34\lib\_collections_abc.py", line 444, in values
return ValuesView(self)
RuntimeError: maximum recursion depth exceeded while calling a Python object

But it's fine for previous version ( dated 2018/08/08 09:42) on the same Python 3.4.

Edit: it's fine if running the latest version on Python 3.6.
Reply
#39
Oh no! Sorry about that... I clearly broke it. I didn't properly test 3.4 best I can tell.

I have rolled back the GitHub version of PySimpleGUI.py to one before the ListDict class was used. It's from Aug 10th. It should work properly for you now. Ugh.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020