AJN AJN - 1 year ago 178
Python Question

tkinter: How to serialize a treeview?

I am working on a python tkinter application that reads initial data from yaml file into a hierarchical TreeView to be edited further by the user.

To implement "save data" and "undo" functions, should I walk the treeview and reconstruct the data into a python object to be serialized (pickle)?
Or is there a python module allowing, for example, to specify the treeview and the output file to be saved on?

Answer Source

I doubt there's any Python module that does what you want, and even if there was, I don't think you'd want to structure your application around using it. Instead you would probably be better off decoupling things and storing the primary data in something independent of the human interface (which may or may not be graphical and might vary or otherwise be changed in the future). This is sometimes called the application "Model".

Doing so will allow you to load and save it regardless of what constitutes the current human interface. So, for example, you would then be free to use pickle if the internal Model is comprised of one or more Python objects. Alternatively you could save the data back into a yaml format file which would make loading it back in again later a cinch since the program can already do that.

Likewise, as the user edits the TreeView, equivalent changes should be made to the Model to keep the two in sync.

Take some time out from coding and familiarize yourself with the Model–View–Controller (MVC) design pattern.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download