title: How to maintain the value of a variable that was changed by the program it was ran by?
body: <p>I am currently trying to build my first GUI application through python's standard Tkinter. I soon came to grips with the computers coordinate system, and indeed I found I could pan things out as I so wish, but I came to the realisation that a drag and drop feature would be far superior then specifying coordinates explicitly. I am close, but I have one major problem; Whilst I can keep the value of the coords of a single widget in relation to where I dragged it last, I cannot do this for multiple widgets.</p>
  
  <p>This is the code I have created so far:</p>
  
  <pre><code>from tkinter import *
  
  root = Tk()
  
  
  class Move_Shape:
      data = {'x': 0, 'y': 0}
      canvas = Canvas(width = root.winfo_screenwidth(), height = root.winfo_screenheight())
      shape_coords = open('Shape_coords.py', 'r')
  
  
  
      def __init__(self, shape, fill = 'White', *coords):
  
          new_coords = self.shape_coords.readline().split(',')
  
          if coords == (): coords = new_coords
  
          if shape == 'line': 
              tag = 'line'
              self.id = self.canvas.create_line(coords, tags = tag, fill = fill)
  
          elif shape == 'rectangle': 
              tag = 'rect'
              self.id = self.canvas.create_rectangle(coords, tags = tag, fill = fill)
  
          ... More code
  
          self.canvas.tag_bind(tag, '&lt;Button-1&gt;', self.click)
          self.canvas.tag_bind(tag, '&lt;Button1-Motion&gt;', self.track)
          self.canvas.tag_bind(tag, '&lt;ButtonRelease-1&gt;', self.release)
          self.canvas.grid()
  
  
      def click(self, event):
          self.data.update({'x': event.x, 'y': event.y})
          self.item = self.canvas.find_closest(self.data['x'], self.data['y'])
  
      def track(self, event):
          x, y = event.x - self.data['x'], event.y - self.data['y']
          self.canvas.move(self.item, x, y)
          self.data.update({'x': event.x, 'y': event.y})
  
      def release(self, event):
          self.data.update({'x': event.x, 'y': event.y})
          coords = str(self.canvas.coords(self.item))
          coords = coords[1:-1]
          shape_coords = open ('Shape_coords.py', 'a')
          shape_coords.write(coords)
          shape_coords.write('\n')
          shape_coords.close()
  
  
  
  Move_Shape('rectangle', 'blue', 50, 50, 100, 100)
  Move_Shape( 'oval', 'green', 50, 50, 100, 100)
  Move_Shape( 'arc', 'red', 50, 50, 100, 100)
  mainloop()
  </code></pre>
  
  <p>If I was to start with an initial pair of coords, I would very much like to be able to delete the coords and pick up where I left of, or rather, where the shape left of. Appending the coordinates to a file does not work, the main reason being that I cannot return the final value of an updated dictionary, after exiting the mainloop. </p>
  
  <p>I did some research before hand, and looked into data persistence. So I came across the Module, pickle, for the first time. Through others examples online, I managed to 'dump' the values into another file, however, if some variable, call it a, changes multiple times, those values are all appended within the file (which leads us back to square one). I would like to know if there is a way to make it so that only the last value assigned to a object through a variable, is stored.</p>
  
  <p>I would go through the pickle module myself, but it's terminology overwhelms me, and I do not know what to look up in specific when it comes to data persistence.</p>