Skip to content
Advertisement

Kivy: how to handle touch events triggered from on_touch_down method of ModalView

Is there a way to make it so when my ModalView/ Popup widget is active but none of the buttons behind it will be triggered?

Below is an example of a ModalView/popup widget that will notify the user they entered something incorrect. I want the user to be able to click anywhere and the ModalView will disappear. The problem is that when this ModalView is visiable the buttons in the background are still active.

In this case, the BaseBoxLayout has a Label on top and Button on the bottom. I click on the button to trigger the ModalView/popup. If the user clicks on the top, the ModalView/popup disappears – as designed by my on_touch_down method.

However, if the user clicks anywhere in the bottom half (when the ModalView/popup is already triggered) then the ModalView/popup gets removed but then added again because the button in the background is still active.

How do I prevent this?

JavaScript

Advertisement

Answer

As kivy gives you maximum flexibility in dispatching touch events, you can decide and implement that as you want.

Let’s consider some typical situations you might come across in this scenario.

  1. Your ModalView (I’ll call it popup) needs to be dismissed as soon as you press (corresponds to on_touch_down event) anywhere on the screen (window). But you don’t want any underlying widget to interfere at this point. Here, this means you don’t want to dispatch the touch event further down the widget tree. You can do this by returning True from method on_touch_down which will digest the event and stop it propagate further.
JavaScript
  1. You want the popup to be dismissed as before but also you don’t want to override any default behavior. Call the superclass method. This is more preferable than in 1.
JavaScript
  1. Perhaps your popup will not cover whole window. In that case you want it to be dismissed only if touch is within its physical boundary otherwise not. Go as in 1 or 2. But what if the touch is outside its physical boundary ? Return True to digest the event. If you don’t handle that youself, call super (but remember it will dismiss the popup unless you set auto_dismiss to False).
JavaScript

There might be countable (if not infinite) no. of situations you can face here. So better stop here.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement