TkRouter

TkRouter is a declarative routing system for building multi-page Tkinter applications. It supports parameterized routes, query strings, animated transitions, history navigation, and more.

PyPI License


โœจ Features

  • ๐Ÿ”€ Route matching with parameters (/users/<id>)
  • โ“ Query string parsing (/logs?level=debug)
  • ๐Ÿ”„ Animated transitions between views (slide, fade)
  • ๐Ÿงญ Singleton router instance (create_router(), get_router())
  • ๐Ÿ” Route guards with redirect support
  • โช History navigation (.back(), .forward(), .go())
  • ๐Ÿงฉ Built-in widgets: RouteLinkButton, RouteLinkLabel
  • ๐ŸŽจ Compatible with both tk.Frame and ttk.Frame

๐Ÿ“ฆ Installation

pip install tkrouter

Quickstart

You can start a new Tkinter router-based app in two ways:

๐Ÿš€ Option 1: Use the CLI

tkrouter-create

This generates a ready-to-run main.py file with a minimal working app.


๐Ÿงช Option 2: Use this snippet

from tkinter import Tk
from tkrouter import create_router, get_router, RouterOutlet
from tkrouter.views import RoutedView
from tkrouter.widgets import RouteLinkButton

class Home(RoutedView):
    def __init__(self, master):
        super().__init__(master)
        RouteLinkButton(self, "/about", text="Go to About").pack()

class About(RoutedView):
    def __init__(self, master):
        super().__init__(master)
        RouteLinkButton(self, "/", text="Back to Home").pack()

ROUTES = {
    "/": Home,
    "/about": About,
}

root = Tk()
outlet = RouterOutlet(root)
outlet.pack(fill="both", expand=True)
create_router(ROUTES, outlet).navigate("/")
root.mainloop()

๐Ÿงช Examples

Run these from the terminal (installed via pip) or with python -m tkrouter.examples.NAME.

Script Description
tkrouter-demo-minimal Basic two-page example
tkrouter-demo-admin Sidebar layout with query param routing (/logs?level=error)
tkrouter-demo-unified Flat URL routes (/dashboard/stats) with transitions
tkrouter-demo-guarded Simulated login with protected route and redirect (/secret โ†’ /login)

๐Ÿงญ Route Config

ROUTES = {
    "/": HomePage,
    "/users/<id>": {
        "view": UserDetailPage,
        "transition": slide_transition,
        "guard": is_logged_in,
        "redirect": "/login"
    }
}

โœ… Supports:

  • <id> dynamic parameters
  • ?key=value query parameters
  • Route guards and redirects
  • Per-route transitions

๐Ÿงฑ Router API

from tkrouter import create_router, get_router

router = create_router(routes, outlet)
router.navigate("/users/123?tab=details")
router.back()
router.on_change(lambda path, params: print(path, params))

๐Ÿงฉ Routed Widgets

from tkrouter.widgets import RouteLinkButton, RouteLinkLabel

RouteLinkButton(parent, "/dashboard")
RouteLinkLabel(parent, "/users/<id>", params={"id": 3})

Also available:

  • bind_route(widget, path, params)
  • @with_route(path, params) decorator

๐Ÿ”„ Transitions

from tkrouter.transitions import slide_transition, simple_fade_transition

Custom transitions supported โ€” just pass a function like:

def my_transition(outlet, view_class, params, duration=300): ...

โš ๏ธ Exceptions

from tkrouter.exceptions import RouteNotFoundError, NavigationGuardError

โœ… Compatibility

  • Python 3.8+

๐Ÿ“„ License

MIT ยฉ Israel Dryer