title: Bind a button to a callback from Kivy to python
body: <p>I've been banging my head on this for days now.  I made it work in Python only (not using the .kv file) but now I just can't figure this out.</p>
  
  <p>Basically, I'll have a series of button to control GPIOs on a Raspberry Pi.  In the Python only version, I have one call back with a serie of IFs looking for the right button "text" to determine wich pin to activate.  Now in the kivy version, I dumbed it down to a single button, moved the callback to many places in the code, etc but nothing seems to work.</p>
  
  <p>Here's the pyton code:</p>
  
  <pre><code># Set up GPIO:
  GPIO.setwarnings(False)
  GPIO.setmode(GPIO.BCM)
  defrost = 27
  GPIO.setup(defrost, GPIO.OUT)
  GPIO.output(defrost, GPIO.LOW)
  
  
  class ConsoleUI(BoxLayout):
      def press_callback(obj):
  
              if obj.state == "down":
                 GPIO.output(defrost, GPIO.HIGH)
              else:
                 GPIO.output(defrost, GPIO.LOW)
  
      pass
  
  class ConsolesimpleApp(App):
      def build(self):
          return ConsoleUI()
  
  
  if __name__ == '__main__':
      ConsolesimpleApp().run()
  
  GPIO.cleanup()
  </code></pre>
  
  <p>Here's the KV code:</p>
  
  <pre><code># Console.kv
  
  
  &lt;ConsoleUI&gt;:
  
      ToggleButton:
          id: Defrost
          on_press: root.press_callback() 
          Image:
              source: 'rear-window-defrost.png'
  </code></pre>
  
  <p>And the current error message:</p>
  
  <pre><code> AttributeError: 'ConsoleUI' object has no attribute 'state'
  </code></pre>
  
  <p>Thank you for any pointers you can give me!</p>
  
  <p>Bonus question: is it better form to have a distinct callback for each button or is the "if obj.text ==" standard?  Since I won't have text on my buttons...  </p>