Moving the model

Currently, our data model object is only created in the on_save() method, and is recreated every time the user saves. Some of the other callbacks we're going to write will need access to the model as well, so instead we'll create a single data model instance that can be shared by all the methods when the Application class is started or whenever a new filename is chosen. Let's take a look at the following steps:

  1. First, edit the Application.__init__() method right after the default_filename is created:
        self.filename = tk.StringVar(value=default_filename)
        self.data_model = m.CSVModel(filename=self.filename.get())
  1. Next, the on_file_select() method needs to recreate the data_model object whenever the filename is changed.

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.