title: Tkinter returning a variable to main
body: <p>Hi I have a program that runs the following way. When a user enters a number between 1-10 and hits the GO button 3 checboxes appear Left, Middle and Right. Upon the selection of only 1 checkbox a button Next appears. I disable the other ones after 1 is selected. I would like to return to the main program which checkbox was selected. How do I do this. I can do it through the Next button or through the checkbox selection. My code is below</p>
  
  <pre><code>import Tkinter as Tk
  import ctypes
  
  
  def setup_fc_error_message():
      lines = ['fc is out of range: Please Enter a value from 1-10:']
      MessageBox = ctypes.windll.user32.MessageBoxA
      MessageBox(None, "\n".join(lines), 'Setup Info', 0)
  
  def check_fc_range(fc_user):
     # fc_float = si_units(fc_string_input)
  #    fc_float = float(fc_string_input)
      fc_string_user = str(fc_user)
      if (fc_user)&gt;=1 and (fc_user&lt;=10):
           return (float(fc_user),fc_string_user)
      else:
           setup_fc_error_message()
           return check_fc_range()
  
  
  class MainWindow(Tk.Frame):
  
      def __init__(self, parent):
          Tk.Frame.__init__(self,parent)
          self.parent = parent
          self.parent.title('Test')
          self.initialize()
  
  
      def var_states_d(self):
          return self.dvar
  
      def var_states_r(self):
          return self.rvar
  
      def var_states_l(self):
          return self.lvar
  
  
      def cdb(self):
  #        print "variable is", self.dvar.get()
          self.dvar_state = self.dvar.get()
  #        self.band_type = StringVar()
  #        self.band_type.set('D')
  
          if self.dvar_state:
              self.cr = Tk.Checkbutton(self.parent, text='Middle', state='disable', variable=self.rvar,command = self.crb)
              self.cr.grid(row = 2, column = 1)
  
              self.cl = Tk.Checkbutton(self.parent, text='Right', state='disable', variable=self.lvar,command = self.clb)
              self.cl.grid(row = 2, column = 2)
  
              self.nextbutton = Tk.Button(self.parent, text='NEXT', command= self.var_states_d)
              self.nextbutton.grid(row=2,column=3)
  
          else:
              self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = self.crb)
              self.cr.grid(row = 2, column = 1)
  
              self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = self.clb)
              self.cl.grid(row = 2, column = 2)
  
              self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
              self.nextbutton.grid(row=2,column=3)
  
  
  
  
      def crb(self):
  #        print "variable is", self.rvar.get()
  
          self.rvar_state = self.rvar.get()
  #        self.band_type = StringVar()
  #        self.band_type.set('R')
  
          if self.rvar_state:
              self.cd = Tk.Checkbutton(self.parent, text='Left', state='disable', variable=self.dvar,command = self.cdb)
              self.cd.grid(row = 2, column = 0)
  
              self.cl = Tk.Checkbutton(self.parent, text='Right', state='disable', variable=self.lvar,command = self.clb)
              self.cl.grid(row = 2, column = 2)
  
              self.nextbutton = Tk.Button(self.parent, text='NEXT', command= self.var_states_r)
              self.nextbutton.grid(row=2,column=3)
          else:
              self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = self.cdb)
              self.cd.grid(row = 2, column = 0)
  
              self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = self.clb)
              self.cl.grid(row = 2, column = 2)
  
              self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
              self.nextbutton.grid(row=2,column=3)
  
      def clb(self):
  #       print "variable is", self.lvar.get()
  
          self.lvar_state = self.lvar.get()
  #        self.band_type = StringVar()
  #        self.band_type.set('L')
  
          if self.lvar_state:
              self.cd = Tk.Checkbutton(self.parent, text='Left', state='disable', variable=self.dvar,command = self.cdb)
              self.cd.grid(row = 2, column = 0)
  
              self.cr = Tk.Checkbutton(self.parent, text='Middle', state='disable', variable=self.rvar,command = self.crb)
              self.cr.grid(row = 2, column = 1)
  
              self.nextbutton = Tk.Button(self.parent, text='NEXT', command= self.var_states_l)
              self.nextbutton.grid(row=2,column=3)
  
          else:
              self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = self.cdb)
              self.cd.grid(row = 2, column = 0)
  
              self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = self.crb)
              self.cr.grid(row = 2, column = 1)
  
              self.nextbutton = Tk.Button(self.parent, text='NEXT', state='disable')
              self.nextbutton.grid(row=2,column=3)
  
  
  
  
  
  
  
      def initialize(self):
        #  self.frame = Tk.Frame(parent)
        #  self.frame.pack()
  
          self.fc_gui = Tk.DoubleVar(self.parent)
  
          self.fclabel1 = Tk.Label(self.parent, text = 'Please Enter a value between 1 and 10', fg = 'black', bg = 'yellow')
          self.fclabel1.grid(row = 0, column = 0)
  
          self.fcedit1 = Tk.Entry(self.parent, textvariable = self.fc_gui, bd = 5  )
          self.fcedit1.grid(row = 1, column = 0)
  
          self.fcbutton1 = Tk.Button(self.parent, text='GO', command = self.get_fc)
          self.fcbutton1.grid(row = 1, column = 1)
  
  
      def add_checkbox(self):
  
          self.dvar = Tk.IntVar()
  
          self.cd = Tk.Checkbutton(self.parent, text='Left', variable=self.dvar,command = self.cdb)
          self.cd.grid(row = 2, column = 0)
  
  
          self.rvar = Tk.IntVar()
  
          self.cr = Tk.Checkbutton(self.parent, text='Middle', variable=self.rvar,command = self.crb)
          self.cr.grid(row = 2, column = 1)
  
          self.lvar = Tk.IntVar()
  
          self.cl = Tk.Checkbutton(self.parent, text='Right', variable=self.lvar,command = self.clb)
          self.cl.grid(row = 2, column = 2)
  
  
  
      def get_fc(self):
  
          self.fc_user = self.fc_gui.get()
          if self.fc_user:
              if check_fc_range(self.fc_user):
                  self.add_checkbox()
              return self.fc_user
  ######################################################GUI portion ##############################################
  
  def main():
  
      root = Tk.Tk()
      app = MainWindow(root)
      fc_gui_user = app.get_fc()
  
      app.mainloop()
  
  
  if __name__ == "__main__":
      main()
  </code></pre>