Python – Adding double tap on any widget in kivy
Last Updated :
24 Feb, 2021
Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on an android device even we can use it on Raspberry Pi also. It is an open-source Python library for the rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. Its graphic engine is built over OpenGL ES 2 and has fast graphics pipeline. If you are new to kivy you can learn from this link.
In this article, we will develop a GUI window using kivy framework of python, and we will add an image on this window(you can follow the same pattern for implementing double-tap event on other widgets also) and we will add a double-tap event on this image.
A basic approach to implement double-tap on image widget
- Import image widget from kivy package
- Import touchbehaviour from kivy package
- Import kivy app
- Import boxlayout from kivy package
- Add widget
- Extend the class
- Return layout
- Run an instance of the class
Code:
Python3
from kivy.uix.image import Image
from kivymd.uix.behaviors import TouchBehavior
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class ImageWithDoubleTouch(Image, TouchBehavior):
def on_double_tap( self , instance, * args):
print ( "wow!! you have double clicked an image named " + self .source)
class MainWindow(BoxLayout):
pass
class uiApp(App):
def build( self ):
return MainWindow()
uiApp().run()
|
kivy Code:
#window which contains our image
<MainWindow>:
BoxLayout:
# adding our ImageWithDoubleTouch widget that we have created in main.py file
ImageWithDoubleTouch:
# adding path of image file
source:”gfg.png”
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...