Scroll View and Mouse Wheel

1,213 views
Skip to first unread message

qay

unread,
Oct 11, 2012, 3:38:23 AM10/11/12
to kivy-...@googlegroups.com
Hi Folks,

I whant to use the mouse wheel for scrolling the scroll view.

I tried unsuccessfull to pipe the on_touch_down mouse.scroll.. event into the on_touch_move event.

Any ideas?
Thanks a lot in advance.

Wbr,

Mathieu Virbel

unread,
Oct 11, 2012, 3:53:51 AM10/11/12
to kivy-...@googlegroups.com
Hi,

The scrollview works already with mousewheel (it's a touch event,
.button='scrollup' or 'scrolldown').

Check the showcase examples with filebrowser, or any others scrollview
examples.

Mathieu

Le 11/10/2012 09:38, qay a �crit :
> --
>
>

qay

unread,
Oct 11, 2012, 5:58:52 AM10/11/12
to kivy-...@googlegroups.com
Hi Mathieu,

thanks for the replay. I tried the scrollview example and yes in the example it works. The strange thing is that I see the touch event as mention before but the scroll view is not scrolling.

I try to realise an alpabet scroll widget. At the first glance I would say that I implemented the scroll view like in the kivy example. Therefore I don't know where the problem is at the moment.

Below my code :

import kivy
kivy.require('1.3.0')

from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.app import App

class ScrollAlphabet(BoxLayout):
   
    # Constructor
    def __init__(self, callback):
        self.__height = 32           
        self.__alphabet = []
        self.__alphabet.append(chr(35))
        for i in range(97, 122):
            self.__alphabet.append(chr(i)) 
           
        super(ScrollAlphabet, self).__init__()
        self.__content = BoxLayout(
            orientation = 'horizontal',
            padding = 4,
            spacing = 2,
            cols = len(self.__alphabet),
            rows = 1,
            parent = self               
        )
        self.__dataLayout = self.__createScrollLayout(callback, self.__alphabet)
        self.__content.add_widget(
            self.__createScrollView(
                self.__dataLayout
            )
        )
        self.add_widget(self.__content)
       
    def __createScrollLayout(self, callback, alphabet):
            result = GridLayout(
                rows = 1,
                cols = len(self.__alphabet),
                spacing = 0,
                size_hint_x = None
            )                                           
            #Make sure the height is such that there is something to scroll.
            result.bind(
                minimum_width = result.setter('width')
            )
            result.width = self.__height * len(alphabet)
            for i in range(0, len(alphabet)):
                button = Button(
                    text = alphabet[i],
                    height = self.__height,
                    size_hint_y = None
                )               
                button.bind(on_press = callback)
                result.add_widget(button)
            return result
       
    def __createScrollView(self, layout):
            result = ScrollView(
                do_scroll_y = False,
                do_scroll_x = True,
                size_hint = (1, 1),
            )
            result.add_widget(layout)
            return result

class ScrollViewApp(App):
        def build(self):
            return ScrollAlphabet(None)
       
if __name__ == '__main__':

    ScrollViewApp().run()



qay

unread,
Oct 12, 2012, 9:29:12 AM10/12/12
to kivy-...@googlegroups.com
Hi Mathieu,

I made some testing with the scrollview example and as soon as I switch from horizontal to vertical you can't any longer use the mouse wheel to scroll the scroll view.

Is there a work around this problem?

qay

unread,
Oct 12, 2012, 10:08:42 AM10/12/12
to kivy-...@googlegroups.com


Am Freitag, 12. Oktober 2012 15:29:12 UTC+2 schrieb qay:
Hi Mathieu,

I made some testing with the scrollview example and as soon as I switch from vertical to horizontal you can't any longer use the mouse wheel to scroll the scroll view.

qay

unread,
Oct 15, 2012, 5:23:37 AM10/15/12
to kivy-...@googlegroups.com
After i came up with the idea to look in original code ( i should have done it earlier) I got this.
Not perfect but it's doing the job.


import kivy
kivy.require('1.3.0')

from kivy.uix.scrollview import ScrollView
from kivy.animation import Animation
from kivy.clock import Clock

'''
Scroll View
===========

The :class:`ScrollView` widget provides a scrollable/pannable viewport that is
clipped at the ScrollViews bounding box.
The class deviate from the kivy.uix.scrollview class to support mouse wheel
scrolling in horizontal diraction.

'''

class ScrollView(ScrollView):       

    def on_touch_down(self, touch):
            if self._touch:
                return super(ScrollView, self).on_touch_down(touch)
            if not self.collide_point(*touch.pos):
                return
            # support scrolling !
            if self._viewport and 'button' in touch.profile and \
                    touch.button.startswith('scroll'):
                # distance available to move, if no distance, do nothing
                vp = self._viewport
                scroll_x = self.__scroll_x(vp)
                scroll_y = self.__scroll_y(vp)
                if(
                   scroll_y or
                   scroll_x
                ):
                    if (scroll_y):
                        self.__do_y_scroll(touch, vp.height - self.height)
                    if (scroll_x):
                        self.__do_x_scroll(touch, vp.width - self.width)
                    Clock.unschedule(self._update_animation)
                    return True
   
            self._touch = touch
            uid = self._get_uid()
            touch.grab(self)
            touch.ud[uid] = {
                'mode': 'unknown',
                'sx': self.scroll_x,
                'sy': self.scroll_y,
                'dt': None,
                'time': touch.time_start}
            Clock.schedule_once(self._change_touch_mode, self.scroll_timeout/1000.)
            return True
       
    def __calculateDistance(self, seedValue, touch):
        d = self.scroll_distance / float(seedValue)
        if touch.button == 'scrollup':
            syd = self._scroll_y_mouse - d
        elif touch.button == 'scrolldown':
            syd = self._scroll_y_mouse + d
        self._scroll_y_mouse = min(max(syd, 0), 1)
        return self._scroll_y_mouse

    def __scroll_y(self, vp):
        return (
            (self.do_scroll_y == True) and
            (vp.height > self.height)
        )

    def __scroll_x(self, vp):
        return (
            (self.do_scroll_x == True) and
            (vp.width > self.width)
        )

    def __do_y_scroll(self, touch, distance):
        scroll_y = self.__calculateDistance(distance, touch)
        Animation.stop_all(self, 'scroll_y')
        Animation(scroll_y = scroll_y, d=.3, t='out_quart').start(self)
       
    def __do_x_scroll(self, touch, distance):
        scroll_x = self.__calculateDistance(distance, touch)
        Animation.stop_all(self, 'scroll_x')
        Animation(scroll_x = scroll_x, d=.3, t='out_quart').start(self)


wbr
Reply all
Reply to author
Forward
0 new messages