Search code examples
pythonsliderkivy

Kivy Slider Touch Input Area Too Large


I am trying to create a game using Kivy, and when I tried to use the Slider widget, I could not get it to stop reacting to nearby touch inputs. I looked online and could not find any way to control the area the widget reacts to. Any help would be appreciated! Below is a very simple Kivy Slider app I made to try and isolate a solution.

Test.py:

import kivy
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Window.size = (375,375)
Builder.load_file('Test.kv')
sm=ScreenManager()
class SliderScreen(Screen):
    pass    
class Test(App):
    def build(self):
        return sm
sm.add_widget(SliderScreen(name='sliderscreen'))
Test().run()

Test.kv:

<SliderScreen>:
    FloatLayout:
        Slider:
            pos_hint:{'x':.15,'y':.275}
            size_hint:.75,.25
            value_track:True
            background_width: 10
            value_track_color: 0,.4,.8,1
            min:0
            sensitivity: 'handle'
            max: 300

Thank you in advance!


Solution

  • If anyone is still looking for the answer, if you don't want to visually modify your widget is to override the collide_point() method, which handles touch input.

    To do so, you need to create a custom widget class as follow :

    class CustomSlider(Slider):
        def collide_point(self, x, y):
            return self.x <= x <= self.right and self.y+40 <= y <= self.top-40
    

    What this does is, it changes the condition that validates the touch inside the Widget. The modification checks for a smaller area than the actual widget. If these value don't work for you, feel free to experiment, the code is pretty self-explanatory.