Retrieving data from our form

Now that we're finished with the form, we need a way to retrieve data from it so it can be processed by the application. We'll create a method that returns a dictionary of the form's data and, as we did with our LabelInput objects, maintain the Tkinter convention of calling it get().

Add the following method to your form class:

    def get(self):        data = {}        for key, widget in self.inputs.items():            data[key] = widget.get()        return data

The code is simple: we loop through our instance's inputs object containing our LabelInput objects and build a new dictionary by calling get() on each variable.

This code demonstrates the power of both iterable objects and consistent naming schemes. If we had stored our inputs as discrete ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.