How to add drag behavior in kivy widget ?
In this article, we will develop a GUI window using kivy framework of python, and we will add a Label having drag behavior on this window
Note: You can follow the same pattern for implementing drag events on other widgets also.
Actually what we are doing is we are using are just using the concept of inheritance, and we are making a new widget(DraggableLabel) by combining the properties of Kivy Label widget and Kivy’s DragBehaviour class. And from the code, you can check that we have defined three properties
- drag_rectangle: it is the area in which we want to enable dragging you can see from the code the have used ourselves with the values of this property because we want to enable dragging in the whole Layout.
- drag_timeout: it specifies the time after which dragging will be disabled from the Label if Label is not dragged, but it will come into play again if we start dragging.
- drag_distance: Distance to move before dragging the DragBehavior, in pixels. As soon as the distance has been traveled, the DragBehavior will start to drag, and no touch event will be dispatched to the children.
And after defining properties we are simply just using our newly created widget as we use others.
Below is the Implementation:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course