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
#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


Messages In This Thread
RE: [PySimpleGUI] Tutorial - Name, Address, Phone GUI - by PySimpleGUI - Aug-10-2018, 05:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PySimpleGUI Try Except jamesaarr 1 2,029 Nov-18-2021, 02:02 PM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

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