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.
- 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.
- Running the Kivy App:
Save the code to a file namedtouch_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!’.
-
Handling Mouse Input:
If you are running the app on a desktop device, you can use the mouse to interact with the app. Theon_touch_down
andon_touch_up
events will be triggered when you click on the label with the mouse. -
Multi-touch Input:
Kivy also supports multi-touch input on touch-enabled devices. You can handle multiple touch events simultaneously by checking theis_double_tap
andis_triple_tap
attributes of the touch object. - 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!
5:50
add in the end of function
return super().on_touch_down(touch)
Thanks
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)
Thx for your time and effort bro
Your really making life easier for alot of people!!
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?
Thanks for this bro, really Helpful stuff
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.
Does it bother anybody else that he leaves the "import kivy" when it never gets used?
Whenever I am using the ontouch move function then the button is not pressable so how can I solve this
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.
I just hope that I'm not lazy
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
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!
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?
hey my button's position and size is not changing after this touch method can you help?
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
i have corona . 🙁
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
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
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