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


import Tkinter as tk
import ttk

import platform


class MouseWheel(object):
    OS = platform.system()
    
    def __init__(self, root, factor = 2):
        
        self.activeArea = None
        
        if type(factor) == int:
            self.factor = factor
        else:
            raise Exception("Factor must be an integer.")

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

    def onMouseWheel(self,event):
        if self.activeArea:
            self.activeArea.onMouseWheel(event)

    def mouseWheel_bind(self, widget):
        self.activeArea = widget

    def mouseWheel_unbind(self):
        self.activeArea = None

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

    def add_scrolling(self, scrollingArea, xscrollbar=None, yscrollbar=None):
        scrollingArea.bind('<Enter>',lambda event: self.mouseWheel_bind(scrollingArea))
        scrollingArea.bind('<Leave>', lambda event: self.mouseWheel_unbind())

        if xscrollbar and not hasattr(xscrollbar, 'onMouseWheel'):
                setattr(xscrollbar, 'onMouseWheel', self.build_function_onMouseWheel(scrollingArea,'x', self.factor) )

        if yscrollbar and not hasattr(yscrollbar, 'onMouseWheel'):
                setattr(yscrollbar, 'onMouseWheel', self.build_function_onMouseWheel(scrollingArea,'y', self.factor) )

        active_scrollbar_on_mouse_wheel = yscrollbar or xscrollbar
        if active_scrollbar_on_mouse_wheel:
            setattr(scrollingArea, 'onMouseWheel', active_scrollbar_on_mouse_wheel.onMouseWheel)

        for scrollbar in (xscrollbar, yscrollbar):
            if scrollbar:
                scrollbar.bind('<Enter>', lambda event, scrollbar=scrollbar: self.mouseWheel_bind(scrollbar) )
                scrollbar.bind('<Leave>', lambda event: self.mouseWheel_unbind())


def test():
    root = tk.Tk()

    xScrollbar = ttk.Scrollbar(root, orient=tk.HORIZONTAL)
    xScrollbar.grid(row=1, column=0, sticky= "ew")

    yScrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL)
    yScrollbar.grid(row=0, column=1,sticky="ns")

    canvas1 = tk.Canvas(root, width=300, height=100,xscrollcommand=xScrollbar.set, yscrollcommand=yScrollbar.set)
    canvas1.grid(row=0, column=0)

    frameWindows= tk.Frame(canvas1)
    frameWindows.pack()

    for i in range(20):
        rowFrame = tk.Frame(frameWindows)
        rowFrame.pack()
        for j in range(8):
            tk.Label(rowFrame, text="Label %s, %s" % (str(i), str(j))).pack(side=tk.LEFT)

    canvas1.create_window(0, 0, window=frameWindows, anchor='nw')

    canvas1.update_idletasks()

    canvas1['scrollregion'] = (0,0,frameWindows.winfo_reqwidth(), frameWindows.winfo_reqheight())
    yScrollbar['command']=canvas1.yview
    xScrollbar['command']=canvas1.xview


    MouseWheel(root).add_scrolling(canvas1,xscrollbar=xScrollbar, yscrollbar=yScrollbar)

    root.mainloop()


if __name__== '__main__':
    test()

Diff to Previous Revision

--- revision 5 2014-06-22 17:05:26
+++ revision 6 2016-03-26 21:39:02
@@ -1,4 +1,4 @@
-# Version: 0.4
+# Version: 0.5
 # Author: Miguel Martinez Lopez
 # Uncomment the next line to see my email
 # print "Author's email: ", "61706c69636163696f6e616d656469646140676d61696c2e636f6d".decode("hex")
@@ -8,112 +8,111 @@
 import ttk
 
 import platform
-os = platform.system()
 
 
 class MouseWheel(object):
-	def __init__(self, root, factor = 2):
-		global os
-		
-		self.activeArea = None
-		
-		if type(factor) == int:
-			self.factor = factor
-		else:
-			raise Exception("Factor must be an integer.")
+    OS = platform.system()
+    
+    def __init__(self, root, factor = 2):
+        
+        self.activeArea = None
+        
+        if type(factor) == int:
+            self.factor = factor
+        else:
+            raise Exception("Factor must be an integer.")
 
-		if os == "Linux" :
-			root.bind_all('<4>', self.onMouseWheel,  add='+')
-			root.bind_all('<5>', self.onMouseWheel,  add='+')
-		else:
-			# Windows and MacOS
-			root.bind_all("<MouseWheel>", self.onMouseWheel,  add='+')
+        if self.OS == "Linux" :
+            root.bind_all('<4>', self.onMouseWheel,  add='+')
+            root.bind_all('<5>', self.onMouseWheel,  add='+')
+        else:
+            # Windows and MacOS
+            root.bind_all("<MouseWheel>", self.onMouseWheel,  add='+')
 
-	def onMouseWheel(self,event):
-		if self.activeArea:
-			self.activeArea.onMouseWheel(event)
+    def onMouseWheel(self,event):
+        if self.activeArea:
+            self.activeArea.onMouseWheel(event)
 
-	def mouseWheel_bind(self, widget):
-		self.activeArea = widget
+    def mouseWheel_bind(self, widget):
+        self.activeArea = widget
 
-	def mouseWheel_unbind(self):
-		self.activeArea = None
+    def mouseWheel_unbind(self):
+        self.activeArea = None
 
-	@staticmethod
-	def build_function_onMouseWheel(widget, orient, factor = 1):
-		view_command = getattr(widget, orient+'view')
-		if os == 'Linux':
-			def onMouseWheel(event):
-				if event.num == 4:
-					view_command("scroll",(-1)*factor,"units" )
-				elif event.num == 5:
-					view_command("scroll",factor,"units" ) 
-				
-		elif os == 'Windows':
-			def onMouseWheel(event):		
-				view_command("scroll",(-1)*int((event.delta/120)*factor),"units" ) 
-		
-		elif os == 'Darwin':
-			def onMouseWheel(event):		
-				view_command("scroll",event.delta,"units" ) 			
-		
-		return onMouseWheel
-		
+    def build_function_onMouseWheel(self, widget, orient, factor = 1):
+        view_command = getattr(widget, orient+'view')
+        if self.OS == 'Linux':
+            def onMouseWheel(event):
+                if event.num == 4:
+                    view_command("scroll",(-1)*factor,"units" )
+                elif event.num == 5:
+                    view_command("scroll",factor,"units" ) 
+                
+        elif self.OS == 'Windows':
+            def onMouseWheel(event):        
+                view_command("scroll",(-1)*int((event.delta/120)*factor),"units" ) 
+        
+        elif self.OS == 'Darwin':
+            def onMouseWheel(event):        
+                view_command("scroll",event.delta,"units" )             
+        
+        return onMouseWheel
+        
 
-	def add_scrolling(self, scrollingArea, xscrollbar=None, yscrollbar=None):
-		scrollingArea.bind('<Enter>',lambda event: self.mouseWheel_bind(scrollingArea))
-		scrollingArea.bind('<Leave>', lambda event: self.mouseWheel_unbind())
+    def add_scrolling(self, scrollingArea, xscrollbar=None, yscrollbar=None):
+        scrollingArea.bind('<Enter>',lambda event: self.mouseWheel_bind(scrollingArea))
+        scrollingArea.bind('<Leave>', lambda event: self.mouseWheel_unbind())
 
-		if xscrollbar and not hasattr(xscrollbar, 'onMouseWheel'):
-				setattr(xscrollbar, 'onMouseWheel', self.build_function_onMouseWheel(scrollingArea,'x', self.factor) )
+        if xscrollbar and not hasattr(xscrollbar, 'onMouseWheel'):
+                setattr(xscrollbar, 'onMouseWheel', self.build_function_onMouseWheel(scrollingArea,'x', self.factor) )
 
-		if yscrollbar and not hasattr(yscrollbar, 'onMouseWheel'):
-				setattr(yscrollbar, 'onMouseWheel', self.build_function_onMouseWheel(scrollingArea,'y', self.factor) )
+        if yscrollbar and not hasattr(yscrollbar, 'onMouseWheel'):
+                setattr(yscrollbar, 'onMouseWheel', self.build_function_onMouseWheel(scrollingArea,'y', self.factor) )
 
-		active_scrollbar_on_mouse_wheel = yscrollbar or xscrollbar
-		if active_scrollbar_on_mouse_wheel:
-			setattr(scrollingArea, 'onMouseWheel', active_scrollbar_on_mouse_wheel.onMouseWheel)
+        active_scrollbar_on_mouse_wheel = yscrollbar or xscrollbar
+        if active_scrollbar_on_mouse_wheel:
+            setattr(scrollingArea, 'onMouseWheel', active_scrollbar_on_mouse_wheel.onMouseWheel)
 
-		for scrollbar in (xscrollbar, yscrollbar):
-			if scrollbar:
-				scrollbar.bind('<Enter>', lambda event, scrollbar=scrollbar: self.mouseWheel_bind(scrollbar) )
-				scrollbar.bind('<Leave>', lambda event: self.mouseWheel_unbind())
+        for scrollbar in (xscrollbar, yscrollbar):
+            if scrollbar:
+                scrollbar.bind('<Enter>', lambda event, scrollbar=scrollbar: self.mouseWheel_bind(scrollbar) )
+                scrollbar.bind('<Leave>', lambda event: self.mouseWheel_unbind())
 
 
 def test():
-	root = tk.Tk()
+    root = tk.Tk()
 
-	xScrollbar = ttk.Scrollbar(root, orient=tk.HORIZONTAL)
-	xScrollbar.grid(row=1, column=0, sticky= "ew")
+    xScrollbar = ttk.Scrollbar(root, orient=tk.HORIZONTAL)
+    xScrollbar.grid(row=1, column=0, sticky= "ew")
 
-	yScrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL)
-	yScrollbar.grid(row=0, column=1,sticky="ns")
+    yScrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL)
+    yScrollbar.grid(row=0, column=1,sticky="ns")
 
-	canvas1 = tk.Canvas(root, width=300, height=100,xscrollcommand=xScrollbar.set, yscrollcommand=yScrollbar.set)
-	canvas1.grid(row=0, column=0)
+    canvas1 = tk.Canvas(root, width=300, height=100,xscrollcommand=xScrollbar.set, yscrollcommand=yScrollbar.set)
+    canvas1.grid(row=0, column=0)
 
-	frameWindows= tk.Frame(canvas1)
-	frameWindows.pack()
+    frameWindows= tk.Frame(canvas1)
+    frameWindows.pack()
 
-	for i in range(20):
-		rowFrame = tk.Frame(frameWindows)
-		rowFrame.pack()
-		for j in range(8):
-			tk.Label(rowFrame, text="Label %s, %s" % (str(i), str(j))).pack(side=tk.LEFT)
+    for i in range(20):
+        rowFrame = tk.Frame(frameWindows)
+        rowFrame.pack()
+        for j in range(8):
+            tk.Label(rowFrame, text="Label %s, %s" % (str(i), str(j))).pack(side=tk.LEFT)
 
-	canvas1.create_window(0, 0, window=frameWindows, anchor='nw')
+    canvas1.create_window(0, 0, window=frameWindows, anchor='nw')
 
-	canvas1.update_idletasks()
+    canvas1.update_idletasks()
 
-	canvas1['scrollregion'] = (0,0,frameWindows.winfo_reqwidth(), frameWindows.winfo_reqheight())
-	yScrollbar['command']=canvas1.yview
-	xScrollbar['command']=canvas1.xview
+    canvas1['scrollregion'] = (0,0,frameWindows.winfo_reqwidth(), frameWindows.winfo_reqheight())
+    yScrollbar['command']=canvas1.yview
+    xScrollbar['command']=canvas1.xview
 
 
-	MouseWheel(root).add_scrolling(canvas1,xscrollbar=xScrollbar, yscrollbar=yScrollbar)
+    MouseWheel(root).add_scrolling(canvas1,xscrollbar=xScrollbar, yscrollbar=yScrollbar)
 
-	root.mainloop()
+    root.mainloop()
 
 
 if __name__== '__main__':
-	test()
+    test()

History