KIVY python:ScrollView中的滑块

时间:2017-10-24 15:23:14

标签: slider kivy

我创建了一个滚动视图,其中我放了一些标签和2个滑块。 滚动效果很好,但我无法用鼠标更改滑块的值... 请运行此代码并查看:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.slider import Slider
from kivy.uix.textinput import TextInput

class Home(BoxLayout):
    def __init__(self, **kwargs):
        super(Home, self).__init__(**kwargs)

        self.layout = GridLayout(cols=1, padding=5, spacing=20, size_hint=(1, None))
        self.layout.bind(minimum_height=self.layout.setter('height'))

        for i in range(50):
            if i%25==0:
                btn = Slider(min=1, max=10, value=4)
            else:
                btn = Label(text=str(i), color=(0,0,0,1), size=(32, 32), size_hint=(1, None))
            self.layout.add_widget(btn)

        self.scrll = ScrollView(size_hint=(1, .6), pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
        self.scrll.add_widget(self.layout)
        self.add_widget(self.scrll)

class MyAppli(App):

    def build(self):
        Window.clearcolor = (1,1,1,1)
        return Home()

if __name__ == '__main__':
    MyAppli().run()

1 个答案:

答案 0 :(得分:1)

好的当你使用滑块时,你应该重新定义on_touch_down,on_touch_up和on_touch_move方法来处理这些事件:

- main.py:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import NumericProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.slider import Slider

class Home(BoxLayout):

   def __init__(self, **kwargs):
      super(Home, self).__init__(**kwargs)
      self.b = []
      self.layout = GridLayout(cols=1, padding=5, spacing=20, size_hint=(1, None))
      self.layout.bind(minimum_height=self.layout.setter('height'))

      for i in range(50):
         if i % 25 == 0:
            self.b.append(MySlider(min=1, max=10, value=4, height=32, size_hint=(1, None)))
         else:
            self.b.append(Label(text=str(i), color=(0,0,0,1), height=32, size_hint=(1, None)))
         self.layout.add_widget(self.b[i])

      self.scrll = ScrollView(size_hint=(1, .6), pos_hint={'center_x': .5, 'center_y': .5}, do_scroll_x=False)
      self.scrll.add_widget(self.layout)
      self.add_widget(self.scrll)

   def update(self, *args):
      for i in range(50):
         if i % 25 == 0:
            self.b[i].begin = self.b[i].pos[0]
            self.b[i].len = self.b[i].size[0]


class MySlider(Slider):

       def on_touch_down(self, touch):
          if self.collide_point(*touch.pos):
             super(MySlider, self).on_touch_down(touch)

       def on_touch_up(self, touch):
          if self.collide_point(*touch.pos):
             super(MySlider, self).on_touch_up(touch)

       def on_touch_move(self, touch):
          if self.collide_point(*touch.pos):
             super(MySlider, self).on_touch_move(touch)

class MyAppli(App):

   def build(self):
      Window.clearcolor = (1,1,1,1)
      return Home()

if __name__ == '__main__':
   MyAppli().run()

- 一些输出: first second

我希望这有帮助!