0

I am following the [Kivy programming guide][1]. I am working on step 2 which is supposed to display three buttons, two which flash when the mouse clicks on them and the other should send a message to the console to display. The three buttons appear as the they should and the outer two work correctly but the middle one does not appear to do anything. When pushed, nothing displays in terminal. Here is my code:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.button import Button
class RootWidget(BoxLayout):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.add_widget(Button(text="Me Button"))
        cb=CustomBtn()
        print('hello')
        cb.bind(pressed = self.btn_pressed)
        self.add_widget(cb)
        self.add_widget(Button(text="You Button!"))

    def btn_pressed(self,instance, pos):
        print('pos: position touched from root widget. ') #.format(pos = pos)

    class CustomBtn(Widget):
        pressed = kp.ListProperty([0,0])
        def on_tounch_down(self,touch):
            if self.collide_point(*touch.pos):
                self.pressed = touch.pos
                #touch is conusumed here.Return True to stop event from propagating to other widgets.
                return False
            return super(CustomBtn,self).on_touch_down(touch)
    
    def on_pressed(self,instance,pos):
        print("Botton pressed at {pos}".format(pos=pos))

    class TestApp(App):
        def build(self):
            return RootWidget()
    
    if __name__ == '__main__':
       TestApp().run() 

On a prior step, the instructions call for a callback

def my_callback(dt):
print('My callback is called', dt)
event = Clock.schedule_interval(my_callback, 1 / 30)

which displayed properly in terminal. I am running this from PowerShell on a Windows 10 machine and am not sure if that is the problem. It might also be that the touch events occur so fast that I can't see the print statement (there was a problem with Flask that did that). Any thoughts would be appreciated.
[1]: https://kivy.org/doc/stable/guide/events.html

3
  • Along with several indentation errors, you have a typo in def on_tounch_down(self,touch):. That should be def on_touch_down(self,touch):. Commented Apr 4 at 13:10
  • @JohnAnderson Thanks for the reply. This is my first post to stack so I had some trouble using the editor. Commented Apr 4 at 21:27
  • 1
    @JohnAnderson Thanks that was the problem. Works correct now. Commented Apr 5 at 0:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.