Inserting a date

Inserting the current date is an easy place to start. The place to do this is in the DataRecordForm.reset() method, which sets up the form for entering a new record.

Update that method as follows:

    def reset(self):
        """Resets the form entries"""

        # clear all values
        for widget in self.inputs.values():
            widget.set('')

        current_date = datetime.today().strftime('%Y-%m-%d')
        self.inputs['Date'].set(current_date)

Just as we do in the Application.save() method, we get the current date from datetime.today() and format it as an ISO date. Then, we set the Date widget's input to that value.

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.