Skip to main content

Flexbox and CSS Grid-inspired layout utilities for tkinter and customtkinter

Project description

snakeflex

Flexbox and CSS Grid-inspired layout utilities for tkinter and customtkinter.

snakeflex implements Flexbox inspired functionality in the context of building user interfaces with Tkinter and/or CustomTkinter and Python. It works as a drop-in layout layer over standard tkinter and customtkinter widgets, without any external dependencies.

pip install snakeflex

Quick start

import tkinter as tk
from snakeflex import FlexRow, FlexCol, GridFrame, Spacer

root = tk.Tk()

# Horizontal bar
bar = FlexRow(root, justify="space-between", align="center", gap=8)
bar.pack(fill="x")

title = tk.Label(bar, text="My App")
bar.add(title)
bar.add(Spacer())                           # pushes right-hand items to the edge
bar.add(tk.Button(bar, text="Settings"))
bar.add(tk.Button(bar, text="Quit", command=root.quit))

# Vertical panel
panel = FlexCol(root, align="stretch", gap=4)
panel.pack(fill="both", expand=True)

panel.add(tk.Label(panel, text="Header"))
panel.add(tk.Text(panel), grow=1)           # Text widget fills remaining height
panel.add(tk.Label(panel, text="Status"))

root.mainloop()

FlexRow / FlexCol

from snakeflex import FlexRow, FlexCol

Parameters

Parameter Type Default Description
justify str "start" Main-axis alignment: "start" "end" "center" "space-between" "space-around" "space-evenly"
align str "stretch" Cross-axis alignment: "stretch" "start" "end" "center"
gap int 0 Pixels of space between children
fg_color str "transparent" Background colour (customtkinter-compatible alias for bg)
direction str "row" FlexFrame only: "row" "column" "row-reverse" "column-reverse"

.add(widget, *, grow=0, align=None)

row = FlexRow(parent, justify="space-between", gap=8)
row.add(logo)
row.add(slider, grow=1)      # slider expands to fill spare space
row.add(mute_btn)
Parameter Default Description
grow 0 Proportional share of remaining space (like flex-grow)
align None Per-child cross-axis override

Returns the widget for chaining.


GridFrame

from snakeflex import GridFrame

Template syntax

"200px 1fr 2fr"  - fixed 200 px | 1 share | 2 shares of remaining space
"1fr 1fr 1fr"    - three equal columns
"auto 1fr"       - content-sized column + fills the rest
"48px 1fr 28px"  - fixed header | flexible content | fixed footer

Usage

layout = GridFrame(parent,
    columns="220px 1fr",
    rows="48px 1fr 28px",
    gap=4,
)
layout.add(topbar,    col=0, row=0, colspan=2)
layout.add(sidebar,   col=0, row=1)
layout.add(content,   col=1, row=1)
layout.add(statusbar, col=0, row=2, colspan=2)

Responsive breakpoints

layout.on_resize(800,
    narrow=lambda: layout.hide_col(2),   # collapse detail panel
    wide=lambda:   layout.show_col(2),
)

Spacer

An invisible, expanding spacer — equivalent to <div style="flex: 1" />.

bar = FlexRow(parent, justify="start", gap=8)
bar.add(logo)
bar.add(Spacer())      # everything after this is pushed right
bar.add(btn_a)
bar.add(btn_b)

When added to a FlexRow/FlexCol, Spacer defaults to grow=1.


customtkinter compatibility

All containers accept fg_color as an alias for bg and default to "transparent" so they blend seamlessly into CTk windows:

import customtkinter as ctk
from snakeflex import FlexRow

app = ctk.CTk()
bar = FlexRow(app, fg_color="transparent", gap=8)
bar.add(ctk.CTkLabel(bar, text="Hello"))
bar.add(ctk.CTkButton(bar, text="Go"), grow=1)

Installation

pip install snakeflex

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snakeflex-0.2.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snakeflex-0.2.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file snakeflex-0.2.0.tar.gz.

File metadata

  • Download URL: snakeflex-0.2.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for snakeflex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d3201e0eb366e176d06dd3127fcda6ebf64eeef6fdf9a826fbb3a597eb8366b3
MD5 da74c07d1c517116d75dc5f3f9a42183
BLAKE2b-256 582223b1e6b747d02524c17f1fdcf150968bd9fc368c88fc408f7aa5818d896e

See more details on using hashes here.

File details

Details for the file snakeflex-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: snakeflex-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for snakeflex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6348e277c4a38480b0f619eff20d018b6ac169e62ef835deb72becd847432120
MD5 d6e1659eeb617d71c5ea166e6fa2e3a6
BLAKE2b-256 3370afc34cf842bec12236e2d3059ed000399081752e4e6adf8a8c7292f60f15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page