Welcome, guest | Sign In | My Account | Store | Cart
# Version: 0.8
# Author: Miguel Martinez Lopez
# Uncomment the next line to see my email
# print ("Author's email: %s"% "61706c69636163696f6e616d656469646140676d61696c2e636f6d".decode("hex"))

import platform

OS = platform.system()

def build_mouse_wheel_handler(widget, orient, factor = 1, what="units"):
    view_command = getattr(widget, orient+'view')
    
    if OS == 'Linux':
        def onMouseWheel(event):
            if event.num == 4:
                view_command("scroll",(-1)*factor, what)
            elif event.num == 5:
                view_command("scroll",factor, what) 
            
    elif OS == 'Windows':
        def onMouseWheel(event):        
            view_command("scroll",(-1)*int((event.delta/120)*factor), what) 
    
    elif OS == 'Darwin':
        def onMouseWheel(event):        
            view_command("scroll",event.delta, what)
    
    return onMouseWheel

class Mousewheel_Support(object):    

    # implemetnation of singleton pattern
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Mousewheel_Support, cls).__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self, root, horizontal_factor = 2, vertical_factor=2):
        
        self._active_area = None
        
        if isinstance(horizontal_factor, int):
            self.horizontal_factor = horizontal_factor
        else:
            raise Exception("Vertical factor must be an integer.")

        if isinstance(vertical_factor, int):
            self.vertical_factor = vertical_factor
        else:
            raise Exception("Horizontal factor must be an integer.")

        if OS == "Linux" :
            root.bind_all('<4>', self._on_mouse_wheel,  add='+')
            root.bind_all('<5>', self._on_mouse_wheel,  add='+')
        else:
            # Windows and MacOS
            root.bind_all("<MouseWheel>", self._on_mouse_wheel,  add='+')

    def _on_mouse_wheel(self,event):
        if self._active_area:
            self._active_area.onMouseWheel(event)

    def _on_mouse_enter_scrolling_area(self, widget):
        self._active_area = widget

    def _on_mouse_leave_scrolling_area(self):
        self._active_area = None

    def add_support_to(self, widget, hscrollbar=None, vscrollbar=None, main_direction=None, what="units"):
        if hscrollbar is not None and not hasattr(hscrollbar, 'onMouseWheel'):
            hscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'x', self.horizontal_factor, what)
            hscrollbar.bind('<Enter>', lambda event, scrollbar=hscrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
            hscrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())

        if vscrollbar is not None and not hasattr(vscrollbar, 'onMouseWheel'):
            vscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'y', self.vertical_factor, what)
            vscrollbar.bind('<Enter>', lambda event, scrollbar=vscrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
            vscrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())

        main_scrollbar = vscrollbar if vscrollbar is not None else hscrollbar
        
        if main_scrollbar is not None:
            widget.bind('<Enter>',lambda event: self._on_mouse_enter_scrolling_area(widget))
            widget.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())

            widget.onMouseWheel = main_scrollbar.onMouseWheel

if __name__== '__main__':
    try:
        from Tkinter import Tk, Scrollbar, Text
        from Tkconstants import *
    except ImportError:
        from tkinter import Tk, Scrollbar, Text
        from tkinter.constants import *

    root = Tk()
    
    lorem_ipsum = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. """

    xscrollbar = Scrollbar(root, orient=HORIZONTAL)
    xscrollbar.pack(side=BOTTOM, fill=X)

    yscrollbar = Scrollbar(root)
    yscrollbar.pack(side=RIGHT, fill=Y)

    textarea = Text(root, wrap="none", height=20, width=27,
                xscrollcommand=xscrollbar.set,
                yscrollcommand=yscrollbar.set)
    textarea.pack()
    
    textarea.insert(1.0, "\n\n".join(lorem_ipsum.split(".")))
    
    textarea.configure(yscrollcommand=yscrollbar.set)
    yscrollbar['command']=textarea.yview

    textarea.configure(xscrollcommand=xscrollbar.set)
    xscrollbar['command']=textarea.xview

    mousewheel_support = Mousewheel_Support(root)
    mousewheel_support.add_support_to(textarea, xscrollbar=xscrollbar, yscrollbar=yscrollbar, what="pages")

    root.mainloop()

Diff to Previous Revision

--- revision 18 2017-04-17 19:28:37
+++ revision 19 2017-04-17 20:00:41
@@ -67,26 +67,24 @@
     def _on_mouse_leave_scrolling_area(self):
         self._active_area = None
 
-    def add_support_to(self, widget, xscrollbar=None, yscrollbar=None, what="units"):        
-        widget.bind('<Enter>',lambda event: self._on_mouse_enter_scrolling_area(widget))
-        widget.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
+    def add_support_to(self, widget, hscrollbar=None, vscrollbar=None, main_direction=None, what="units"):
+        if hscrollbar is not None and not hasattr(hscrollbar, 'onMouseWheel'):
+            hscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'x', self.horizontal_factor, what)
+            hscrollbar.bind('<Enter>', lambda event, scrollbar=hscrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
+            hscrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
 
-        if xscrollbar and not hasattr(xscrollbar, 'onMouseWheel'):
-            xscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'x', self.horizontal_factor, what)
+        if vscrollbar is not None and not hasattr(vscrollbar, 'onMouseWheel'):
+            vscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'y', self.vertical_factor, what)
+            vscrollbar.bind('<Enter>', lambda event, scrollbar=vscrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
+            vscrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
 
-        if yscrollbar and not hasattr(yscrollbar, 'onMouseWheel'):
-            yscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'y', self.vertical_factor, what)
+        main_scrollbar = vscrollbar if vscrollbar is not None else hscrollbar
+        
+        if main_scrollbar is not None:
+            widget.bind('<Enter>',lambda event: self._on_mouse_enter_scrolling_area(widget))
+            widget.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
 
-        main_scrollbar = yscrollbar if yscrollbar is not None else xscrollbar
-        
-        if main_scrollbar:
             widget.onMouseWheel = main_scrollbar.onMouseWheel
-
-        for scrollbar in (xscrollbar, yscrollbar):
-            if scrollbar:
-                scrollbar.bind('<Enter>', lambda event, scrollbar=scrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
-                scrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
-
 
 if __name__== '__main__':
     try:

History