Welcome, guest | Sign In | My Account | Store | Cart
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, normal_button, selected_button, padding = None, font=None, command=None):
        Frame.__init__(self, master)

        self._command = command

        self.style_name_selected = 'SelectedPage%s.TButton'%Pagination._new_index
        self.style_name_normal = 'Page%s.TButton'%Pagination._new_index
        
        Pagination._new_index += 1      

        s = Style()
        
        for style_name, style_config in ((self.style_name_normal, normal_button), (self.style_name_selected, 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_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_normal)

        self._selected_button = button
        button.configure(style=self.style_name_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"])


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

    root = Tk()

    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"
        }
    }

    pagination_style2 = {
        "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"
        }
    }

    pagination_style3 = {
        "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"
        }
    }
    
    pagination_style4 = {
        "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"
        }
    }
    
    pagination_style5 = {
        "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"
        }
    }
    #https://codyhouse.co/demo/pagination/index.html#0
    def print_page(page_number):
        print("page number %s"%page_number)

    Label(root, text="Style 1:").pack(padx=20, anchor=W)
        
    pagination = Pagination(root, 1,5, command=print_page, **pagination_style)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 2:").pack(padx=20, anchor=W)
    pagination = Pagination(root, 1,5, command=print_page, **pagination_style2)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 3:").pack(padx=20, anchor=W)
    pagination = Pagination(root, 1,5, command=print_page, **pagination_style3)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 4:").pack(padx=20, anchor=W)
    pagination = Pagination(root, 1,5, command=print_page, **pagination_style4)
    pagination.pack(pady=(5,20), padx=20)
    
    Label(root, text="Style 5:").pack(padx=20, anchor=W)
    pagination = Pagination(root, 1,5, command=print_page, **pagination_style5)
    pagination.pack(pady=(5,20), padx=20)

    root.mainloop()

History