Welcome, guest | Sign In | My Account | Store | Cart
# encoding: utf-8
# Author: Miguel Martínez López

try:
    from Tkinter import Tk
    from ttk import Frame, Button, Style
    from Tkconstants import *
except ImportError:
    from tkinter import Tk
    from tkinter.ttk import Frame, Button, Style
    from tkinter.constants import *

class Pagination(Frame):
    _new_index = 0
    def __init__(self, master, first_page, last_page, command =None):
        if not hasattr(self, "pagination_style"):
            raise Exception("No pagination style defined")
        
        Frame.__init__(self, master)

        self._command = command

        self._style_name_of_selected = 'SelectedPage%s.TButton'%Pagination._new_index
        self._style_name_of_normal = 'Page%s.TButton'%Pagination._new_index

        Pagination._new_index += 1      

        s = Style()

        padding = self.pagination_style.get("padding")
        font = self.pagination_style.get("font")
        
        for style_name, style_config in ((self._style_name_of_normal, self.pagination_style["normal_button"]), (self._style_name_of_selected, self.pagination_style["selected_button"])):
            if "padding" in style_config:
                s.configure(style_name, padding=style_config["padding"])
            elif padding is not None:
                s.configure(style_name, padding=padding)

            if "font" in style_config:
                s.configure(style_name, font=style_config["font"])
            elif font is not None:
                s.configure(style_name, font=font)

            if "background" in style_config and "activebackground" in style_config:
                s.map(style_name, background=[('active', style_config["activebackground"]), ('!active', style_config["background"])])
                
            if "foreground" in style_config and "activeforeground" in style_config:
                s.map(style_name, foreground=[('active', style_config["activeforeground"]), ('!active', style_config["foreground"])])

        self._list_of_buttons = []

        for page_number in range(first_page, last_page+1):
            button = Button(self, text=page_number, style=self._style_name_of_normal, width=0)
            button.configure(command=lambda button=button: self._on_button_selected(button))
            button.pack(side=LEFT)

            self._list_of_buttons.append(button)
        
        self._selected_button = None

    def _on_button_selected(self, button):
        if self._selected_button is not None:
            self._selected_button.configure(style = self._style_name_of_normal)

        self._selected_button = button
        button.configure(style=self._style_name_of_selected)
        
        if self._command is not None:
            self._command(self.selected_page)

    def select_page(self, page_number):
        page_number = int(page_number)
        for button in self._list_of_buttons:
            if int(button["text"]) == page_number:
                self._selected_button = button
                return

    @property
    def selected_page(self):
        return int(self._selected_button["text"])

    page = selected_page

class Pagination1(Pagination):
    pagination_style = {
        "padding":(12,6, 12,6),
        "normal_button": {
            "font": ("Verdana", 12),
            "foreground": "#337ab7", 
            "activeforeground":"#23527c",
            "background": "white", 
            "activebackground": "#eee"
        }, 
        "selected_button": {
            "font":("Verdana", 12, "bold"),
            "foreground":"#fff",
            "activeforeground":"#fff", 
            "background":"#337ab7", 
            "activebackground":"#337ab7"
        }
    }

class Pagination2(Pagination):
    pagination_style = {
        "padding":(12,6, 12,6),
        "normal_button": {
            "font": ("Verdana", 12),
            "foreground": "black", 
            "activeforeground":"black",
            "background": "white", 
            "activebackground": "#ccc"
        }, 
        "selected_button": {
            "font":("Verdana", 12, "bold"),
            "foreground":"white",
            "activeforeground":"#fff", 
            "background":"#f44336", 
            "activebackground":"#f44336"
        }
    }

class Pagination3(Pagination):
    pagination_style = {
        "padding":(12,6, 12,6),
        "normal_button": {
            "font": ("Verdana", 12),
            "foreground": "#717171", 
            "activeforeground":"#717171",
            "background": "#e9e9e9", 
            "activebackground": "#fefefe"
        }, 
        "selected_button": {
            "font":("Verdana", 12, "bold"),
            "foreground":"#f0f0f0",
            "activeforeground":"#f0f0f0", 
            "background":"#616161", 
            "activebackground":"#616161"
        }
    }
    
class Pagination4(Pagination):
    pagination_style = {
        "padding":(12,6, 12,6),
        "normal_button": {
            "font": ("Verdana", 12),
            "foreground": "#feffff", 
            "activeforeground":"#feffff",
            "background": "#3e4347", 
            "activebackground": "#3d4f5d"
        }, 
        "selected_button": {
            "font":("Verdana", 12, "bold"),
            "foreground":"#feffff",
            "activeforeground":"#feffff", 
            "background":"#2f3237", 
            "activebackground":"#2f3237"
        }
    }

class Pagination5(Pagination):
    pagination_style = {
        "padding":(12,6, 12,6),
        "normal_button": {
            "font": ("Verdana", 12),
            "foreground": "#2E4057", 
            "activeforeground":"#2E4057",
            "background": "white", 
            "activebackground": "white"
        }, 
        "selected_button": {
            "font":("Verdana", 12, "bold"),
            "foreground":"white",
            "activeforeground":"white", 
            "background":"#64a281", 
            "activebackground":"#64a281"
        }
    }


if __name__ == "__main__":
    try:
        from ttk import Label
    except ImportError:
        from tkinter.ttk import Label


    def print_page(page_number):
        print("page number %s"%page_number)

    root = Tk()

    Label(root, text="Style 1:").pack(padx=20, anchor=W)
        
    pagination = Pagination1(root, 1, 5, command=print_page)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 2:").pack(padx=20, anchor=W)
    pagination = Pagination2(root, 1, 5, command=print_page)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 3:").pack(padx=20, anchor=W)
    pagination = Pagination3(root, 1,5, command=print_page)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 4:").pack(padx=20, anchor=W)
    pagination = Pagination4(root, 1,5, command=print_page)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 5:").pack(padx=20, anchor=W)
    pagination = Pagination5(root, 1,5, command=print_page)
    pagination.pack(pady=(5,20), padx=20)

    root.mainloop()

Diff to Previous Revision

--- revision 6 2016-11-25 13:26:55
+++ revision 7 2016-11-25 13:33:48
@@ -1,6 +1,5 @@
 # encoding: utf-8
 # Author: Miguel Martínez López
-
 
 try:
     from Tkinter import Tk
@@ -29,6 +28,7 @@
         s = Style()
 
         padding = self.pagination_style.get("padding")
+        font = self.pagination_style.get("font")
         
         for style_name, style_config in ((self._style_name_of_normal, self.pagination_style["normal_button"]), (self._style_name_of_selected, self.pagination_style["selected_button"])):
             if "padding" in style_config:

History