title: Where can I declare my StringVar to trace it and use it with an OptionBox
body: <p>I have been trying to declare this StringVar so that when I change the contents of one option box the contents of another one is updated. While this has been asked many times, I cannot seem to make it work. </p>
  
  <p>Usually it is said that you should declare it before the combo box, write the 'update' routine and declare the trace below that on the same line as the 'def'. This worked, however I also want to access the value of my string var in many other routines and so declared it with 'self.' I don't seem to be able to make both things work at once. </p>
  
  <p>Below is some of my code, take note of <code>self.varRun</code> and the bottom routine as these are what must work with each other (while still being able to use <code>varRuns</code> in other routines in this class). </p>
  
  <pre><code>class createViewer(Tk.Frame):
  
      def __init__(self, parent, controller):
          Tk.Frame.__init__(self,parent)
  
          leftFrame = Tk.Frame(self)
          leftFrame.pack(side="left")
  
          botLeftFrame = Tk.Frame(leftFrame)
          botLeftFrame.pack(side="bottom")
  
          Import = Tk.Button(botLeftFrame)
          Import["text"] = "Import Frame"
          Import["command"] = self.importRun
          Import.pack(side="left", padx = 5, pady = 10)
  
          Load = Tk.Button(botLeftFrame)
          Load["text"] = "Load Frame"
          Load["command"] = self.loadRun
          Load.pack(side="left", padx = 5, pady = 10)
  
          runLabel = Tk.Label(botLeftFrame, text = "Select Run")
          runLabel.pack(side="left", padx = 5, pady = 10)
  
  
          cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\dbsDetectorBookingSystem.accdb')
          cursor = cnxn.cursor()
          self.Runs = []
          cursor.execute("SELECT RunFilePath FROM tblRuns")
          rows = cursor.fetchall()
          for row in rows:
              self.Runs.append(row.RunFilePath)
  
          self.varRun = Tk.StringVar()
          self.varRun.set("None selected")
          self.varRun.trace('w', self.runSelectionChanged)
  
          self.SelectRun = Tk.OptionMenu(botLeftFrame, self.varRun, *self.Runs)
          self.SelectRun.pack(side="left", padx = 5, pady = 10)
  
          frameLabel = Tk.Label(botLeftFrame, text = "Select Frame")
          frameLabel.pack(side="left", padx = 5, pady = 10)
  
          self.varFrame = Tk.StringVar()
          self.Frames = ["holder","holder2"]
          self.SelectFrame = Tk.OptionMenu(botLeftFrame, self.varFrame, *self.Frames)
          self.varFrame.set("None selected")
          self.SelectFrame.pack(side="left", padx = 5, pady = 10)
  
          Home = Tk.Button(botLeftFrame, text="Home",command=lambda: controller.show_frame(createHome))
          Home.pack(side="left", padx = 5, pady = 10)
  
  
          topLeftFrame = Tk.Frame(leftFrame)
          topLeftFrame.pack(side="top", expand = True, fill = "both")
  
          img = Image.open("C:\Users\Jacob\workspace2\Blank2.png")
          blkImg = ImageTk.PhotoImage(img)
          self.ImageViewer = Tk.Label(topLeftFrame, image = blkImg)
          self.ImageViewer.image = blkImg
          self.ImageViewer.pack(side = "left", padx = 5, pady = 10)
  
          rightFrame = Tk.Frame(self, bg = "white")
          rightFrame.pack(side="right", padx = 5, pady = 10)
  
          self.alphaCount = 0
          self.betaCount = 0
          self.gammaCount = 0
          self.alphaText = Tk.StringVar()
          self.betaText = Tk.StringVar()
          self.gammaText = Tk.StringVar()
  
          self.alphaText.set("Alpha: 0")
          self.betaText.set("Beta: 0")
          self.gammaText.set("Gamma: 0")
  
          self.alpha = Tk.Label(rightFrame, textvariable = self.alphaText, bg = "white")
          self.alpha.pack(side="top", padx = 5, pady = 5)
          self.beta = Tk.Label(rightFrame, textvariable = self.betaText, bg = "white")
          self.beta.pack(side="top", padx = 5, pady = 5)
          self.gamma = Tk.Label(rightFrame, textvariable = self.gammaText, bg = "white")
          self.gamma.pack(side="top", padx = 5, pady = 5)
  
      def runSelectionChanged(self,*args):
          cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\dbsDetectorBookingSystem.accdb')
          cursor = cnxn.cursor()
          cursor.execute("SELECT RunFilePath, RunID FROM tblRuns")
          rows = cursor.fetchall()
          for row in rows:
              if row.RunFilePath == self.varRun.get():
                  chosenRunID = row.RunID
  
          cursor.execute("SELECT LocalFilePath FROM tblFrames WHERE RunID=?") (chosenRunID)
          self.userFrames = cursor.fetchall()
          for frame in self.userFrames:
              self.Frames.append(frame)
  </code></pre>