# Version: 0.11
# Author: Miguel Martinez Lopez
# Uncomment the next line to see my email
# print("Author's email: ", "61706c69636163696f6e616d656469646140676d61696c2e636f6d".decode("hex"))
try:
from Tkinter import Canvas, Frame
from ttk import Scrollbar
from Tkconstants import *
except ImportError:
from tkinter import Canvas, Frame
from tkinter.ttk import Scrollbar
from tkinter.constants import *
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, xscrollbar=None, yscrollbar=None, main_direction=None, what="units"):
if xscrollbar is not None and not hasattr(xscrollbar, 'onMouseWheel'):
xscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'x', self.horizontal_factor, what)
xscrollbar.bind('<Enter>', lambda event, scrollbar=xscrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
xscrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
if yscrollbar is not None and not hasattr(yscrollbar, 'onMouseWheel'):
yscrollbar.onMouseWheel = build_mouse_wheel_handler(widget,'y', self.vertical_factor, what)
yscrollbar.bind('<Enter>', lambda event, scrollbar=yscrollbar: self._on_mouse_enter_scrolling_area(scrollbar) )
yscrollbar.bind('<Leave>', lambda event: self._on_mouse_leave_scrolling_area())
main_scrollbar = yscrollbar if yscrollbar is not None else xscrollbar
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
class Scrolling_Area(Frame):
def __init__(self, master, mousewheel_speed = 2, scroll_horizontally=True, scroll_vertically=True, outer_background=None, inner_frame=Frame, **kw):
Frame.__init__(self, master)
if outer_background:
self.configure(background=outer_background)
self._canvas = Canvas(self, highlightthickness=0)
self._canvas.grid(row=0, column=0, sticky="nsew")
if scroll_vertically:
self.yscrollbar = Scrollbar(self, orient=VERTICAL)
self.yscrollbar.grid(row=0, column=1,sticky=N+S)
self._canvas.configure(yscrollcommand=self.yscrollbar.set)
self.yscrollbar['command']=self._canvas.yview
else:
self.yscrollbar = None
if scroll_horizontally:
self.xscrollbar = Scrollbar(self, orient=HORIZONTAL)
self.xscrollbar.grid(row=1, column=0, sticky=E+W)
self._canvas.configure(xscrollcommand=self.xscrollbar.set)
self.xscrollbar['command']=self._canvas.xview
else:
self.xscrollbar = None
self.rowconfigure( 0, weight=1)
self.columnconfigure(0, weight=1)
self.innerFrame= inner_frame(self._canvas, **kw)
self.innerFrame.pack()
self._canvas.create_window(0, 0, window=self.innerFrame, anchor='nw', tags="inner_frame")
self._canvas.bind('<Configure>', self._on_configure_canvas)
Mousewheel_Support(self).add_support_to(self._canvas, xscrollbar=self.xscrollbar, yscrollbar=self.yscrollbar)
def _on_configure_canvas(self, event):
width = max(self.innerFrame.winfo_reqwidth(), event.width)
height = max(self.innerFrame.winfo_reqheight(), event.height)
self._canvas.configure(scrollregion="0 0 %s %s" % (width, height))
self._canvas.itemconfigure("inner_frame", width=width, height=height)
if __name__== '__main__':
try:
from Tkinter import Tk, Label
except ImportError:
from tkinter import Tk, Label
root = Tk()
scrolling_area = Scrolling_Area(root)
scrolling_area.pack(expand=1, fill=BOTH)
for i in range(20):
rowFrame = Frame(scrolling_area.innerFrame)
rowFrame.pack()
for j in range(8):
Label(rowFrame, text="Label %s, %s" % (str(i), str(j))).pack(side="left")
root.mainloop()
Diff to Previous Revision
--- revision 20 2017-04-17 20:03:04
+++ revision 21 2017-04-17 20:12:55
@@ -1,7 +1,18 @@
-# Version: 0.8
+# Version: 0.11
# Author: Miguel Martinez Lopez
# Uncomment the next line to see my email
-# print ("Author's email: %s"% "61706c69636163696f6e616d656469646140676d61696c2e636f6d".decode("hex"))
+# print("Author's email: ", "61706c69636163696f6e616d656469646140676d61696c2e636f6d".decode("hex"))
+
+try:
+ from Tkinter import Canvas, Frame
+ from ttk import Scrollbar
+
+ from Tkconstants import *
+except ImportError:
+ from tkinter import Canvas, Frame
+ from tkinter.ttk import Scrollbar
+
+ from tkinter.constants import *
import platform
@@ -86,38 +97,69 @@
widget.onMouseWheel = main_scrollbar.onMouseWheel
+class Scrolling_Area(Frame):
+
+ def __init__(self, master, mousewheel_speed = 2, scroll_horizontally=True, scroll_vertically=True, outer_background=None, inner_frame=Frame, **kw):
+ Frame.__init__(self, master)
+
+ if outer_background:
+ self.configure(background=outer_background)
+
+ self._canvas = Canvas(self, highlightthickness=0)
+ self._canvas.grid(row=0, column=0, sticky="nsew")
+
+ if scroll_vertically:
+ self.yscrollbar = Scrollbar(self, orient=VERTICAL)
+ self.yscrollbar.grid(row=0, column=1,sticky=N+S)
+
+ self._canvas.configure(yscrollcommand=self.yscrollbar.set)
+ self.yscrollbar['command']=self._canvas.yview
+ else:
+ self.yscrollbar = None
+
+ if scroll_horizontally:
+ self.xscrollbar = Scrollbar(self, orient=HORIZONTAL)
+ self.xscrollbar.grid(row=1, column=0, sticky=E+W)
+
+ self._canvas.configure(xscrollcommand=self.xscrollbar.set)
+ self.xscrollbar['command']=self._canvas.xview
+ else:
+ self.xscrollbar = None
+
+ self.rowconfigure( 0, weight=1)
+ self.columnconfigure(0, weight=1)
+
+ self.innerFrame= inner_frame(self._canvas, **kw)
+ self.innerFrame.pack()
+
+ self._canvas.create_window(0, 0, window=self.innerFrame, anchor='nw', tags="inner_frame")
+
+ self._canvas.bind('<Configure>', self._on_configure_canvas)
+
+ Mousewheel_Support(self).add_support_to(self._canvas, xscrollbar=self.xscrollbar, yscrollbar=self.yscrollbar)
+
+ def _on_configure_canvas(self, event):
+ width = max(self.innerFrame.winfo_reqwidth(), event.width)
+ height = max(self.innerFrame.winfo_reqheight(), event.height)
+
+ self._canvas.configure(scrollregion="0 0 %s %s" % (width, height))
+ self._canvas.itemconfigure("inner_frame", width=width, height=height)
+
if __name__== '__main__':
try:
- from Tkinter import Tk, Scrollbar, Text
- from Tkconstants import *
+ from Tkinter import Tk, Label
except ImportError:
- from tkinter import Tk, Scrollbar, Text
- from tkinter.constants import *
-
+ from tkinter import Tk, Label
+
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()
+ scrolling_area = Scrolling_Area(root)
+ scrolling_area.pack(expand=1, fill=BOTH)
- 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")
+ for i in range(20):
+ rowFrame = Frame(scrolling_area.innerFrame)
+ rowFrame.pack()
+ for j in range(8):
+ Label(rowFrame, text="Label %s, %s" % (str(i), str(j))).pack(side="left")
root.mainloop()