Kivy Tutorial #7: Understanding Touch and Mouse Inputs

Posted by


Welcome to Kivy Tutorial #7 – Touch Input/Mouse Input! In this tutorial, we will be exploring how to work with touch input and mouse input in Kivy.

Kivy provides a simple way to handle touch input on touch-enabled devices and mouse input on desktop devices. We can use these input events to create interactive and responsive user interfaces in our Kivy applications.

Let’s get started with an example to demonstrate how touch input and mouse input work in Kivy.

  1. Creating a Basic Kivy App:
    First, let’s create a basic Kivy app that displays a label and responds to touch input events. Here is the code for our Kivy app:
from kivy.app import App
from kivy.uix.label import Label

class TouchApp(App):
    def build(self):
        label = Label(text='Touch me!', font_size=50)
        label.bind(on_touch_down=self.on_touch_down)
        label.bind(on_touch_up=self.on_touch_up)
        return label

    def on_touch_down(self, instance, touch):
        if instance.collide_point(*touch.pos):
            instance.text = 'Touched!'

    def on_touch_up(self, instance, touch):
        instance.text = 'Touch me!'

if __name__ == '__main__':
    TouchApp().run()

In this code, we have created a simple Kivy app that displays a label with the text ‘Touch me!’. We have bound the on_touch_down and on_touch_up events to methods that update the label text when the label is touched.

  1. Running the Kivy App:
    Save the code to a file named touch_app.py and run the app using the following command:
python touch_app.py

You should see a window appear with a label that says ‘Touch me!’. When you click on the label, the text will change to ‘Touched!’, and when you release the click, it will change back to ‘Touch me!’.

  1. Handling Mouse Input:
    If you are running the app on a desktop device, you can use the mouse to interact with the app. The on_touch_down and on_touch_up events will be triggered when you click on the label with the mouse.

  2. Multi-touch Input:
    Kivy also supports multi-touch input on touch-enabled devices. You can handle multiple touch events simultaneously by checking the is_double_tap and is_triple_tap attributes of the touch object.

  3. Conclusion:
    In this tutorial, we have learned how to work with touch input and mouse input in Kivy. We have created a basic Kivy app that responds to touch events and mouse clicks. You can further explore touch input and mouse input by experimenting with different widgets and event handlers in Kivy.

I hope you found this tutorial helpful! Stay tuned for more tutorials on Kivy and happy coding!

0 0 votes
Article Rating

Leave a Reply

22 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@DavlatDavlat-z9g
9 days ago

5:50
add in the end of function

return super().on_touch_down(touch)

@ndnlejuste
9 days ago

Thanks

@Schadowshame
9 days ago

just call the super function to get the "on_touch_XXX" working

class Touch(Widget):
btn = ObjectProperty(None)

def on_touch_down(self, touch):
print("Mouse Down", touch)
return super(Touch, self).on_touch_down(touch)

def on_touch_move(self, touch):
print("Mouse Move", touch)
return super(Touch, self).on_touch_move(touch)

def on_touch_up(self, touch):
print("Mouse Up", touch)
return super(Touch, self).on_touch_up(touch)

@DOTAFRICA
9 days ago

Thx for your time and effort bro
Your really making life easier for alot of people!!

@lakshmivallabh1340
9 days ago

Awesome and really needed bro thanks. But 1 doubt bro, I want to block the all the touch functions temporarily when some content is loading like, I have added an MDSpinner when its fetching some details from database and at that time, I don't want my users to type in the text fields or click the buttons on the screen. So, how can we do that?

@davidjohnmorandarte2764
9 days ago

Thanks for this bro, really Helpful stuff

@alondramariedh
9 days ago

Do you have videos about how to do these things but without using kivy language? it's just that I have problem with kivy language and I tried to do the things you have done in a file .py but it doesn't work, I guessed I just don't know enough how to do it.

@gabemorris5370
9 days ago

Does it bother anybody else that he leaves the "import kivy" when it never gets used?

@gopalmondal9078
9 days ago

Whenever I am using the ontouch move function then the button is not pressable so how can I solve this

@manojganesh1706
9 days ago

Can't we use pynput library instead and my app crashes? what should i do? i even gave the library in the requirements also while converting kv to apk but i still get error? Pls tell me how to use python libraries for creating apps.

@antoniofuller2331
9 days ago

I just hope that I'm not lazy

@atharvasaney3376
9 days ago

how do i do this:
1. user will type some input (can be anything for eg username)
2. after clicking submit, the username will be printed on the kivy app itself, inside another box

please help

@maxharlan6982
9 days ago

Thanks for this one Tim! Just got the homescreen layout of my first app done, looking forward to seeing how to make multiple pages link together!

@user-kp8hx1mf8i
9 days ago

Everyone does the paint example, nobody demonstrates how to modify an existing application with working buttons for a touch screen.

Anyone know any good resources for accomplishing this?

@MrBizarre6000
9 days ago

hey my button's position and size is not changing after this touch method can you help?

@jamesberesford7310
9 days ago

To deal with the opacity change, I found adding this to the .kv file code works…. (But instead of opacity I played with colours, but I think to do the opacity, you would just use btn.opacity=0.4, etc… – Happy to answer any qu's!)
Button:

text: "Goodbye world"

size_hint: .2, .2

pos_hint: {"x": 0.4, "top": 0.4}

background_normal: ""

background_color: .3, 1, -.7, 1

id: btn2

on_press:

print("Pressed button 2")

btn2.background_color = RGB = 1, -1, -.7, 1

on_release:

print("Released button 2")

btn2.background_color = RGB = .3, 1, -.7, 1

@satoshinakamoto171
9 days ago

i have corona . 🙁

@nikanorsakaria1704
9 days ago

Make a video Creating a Desktop App or Android with Python , where you include functional contact details, some photos in it and text , should be three pages atleast ,,, it might be helpful when doing my project

@joakimjocka8022
9 days ago

i think you need to update this , i copied it word for word and its just showing black , no button at all an also no spos updates

@सुरेलसंवादिनी
9 days ago

Thank You Tim for your tutorials…..
I love your tutorials being so straight to the point and so promising to the viewer…..
And I love the idea of that full GUI application

22
0
Would love your thoughts, please comment.x
()
x