ctk-kit
面向 CustomTkinter 的轻量界面基件: 一套深浅色设计令牌、一批常用控件,以及一个会自动分页的懒加载列表。 支持 Python 3.8 – 3.14。
写它的起因很朴素——每个 customtkinter 项目都要重新调一遍颜色、圆角、按钮宽度和滚动列表的 分批渲染,调完还很难看齐。ctk-kit 把这些收成一层薄薄的公共件,项目之间可以直接复用。
pip install ctk-kit
import customtkinter as ctk
from ctk_kit import apply_appearance, primary_button, page_header
apply_appearance("Dark")
app = ctk.CTk()
head, info = page_header(app, "全部记录")
head.pack(fill="x", padx=20, pady=(16, 8))
primary_button(app, "新建", lambda: None).pack()
app.mainloop()
看实际效果:
git clone https://gitee.com/kuangjianke/ctk-kit
cd ctk-kit
python examples/showcase.py
展示程序有八个页面:总览、基件、按钮、输入、布局、列表、主题、表单,把每个公开构建都用一遍。 基件页把 24 个色值连同浅/深两套十六进制值一起列出来,换主题时对着它调最直观。
三层结构
| 层 | 模块 | 内容 |
|---|---|---|
| 令牌 | tokens |
COLOR_*(全部为浅/深二元组)、FONT_*、RADIUS_*、SPACE_* |
| 主题 | theme |
is_dark()、color_of()、apply_appearance()、repaint_native() |
| 组件 | buttons / inputs / dropdown / layout / scroll_list |
见下 |
组件
按钮(buttons)
primary_button(master, text, command, *, width=120, height=38, radius=9, font=FONT_BODY)soft_button(..., width=76, height=30, font=FONT_SMALL, border=False)—— 次要操作danger_button(...)—— 删除一类不可逆操作FlatButton(CTkButton)/SoftButton(FlatButton)—— 卡片里的小按钮,宽度按文字实测 (内部是CTkButton子类,所以有圆角)
输入(inputs)
toolbar_entry(master, placeholder, *, height=40)—— 工具条里的搜索框,聚焦时描边变主色form_entry(master, *, show="", placeholder="", height=38, password=False)segmented(master, values, command=None, *, width=300, height=34, font=FONT_SMALL)switch(master, command=None, *, width=44)
下拉(dropdown)
DropField(master, *, values_provider, command=None, placeholder="", height=36)—— 自绘候选卡片, 不用CTkComboBox,因为系统下拉框在深浅色之间很难统一
布局(layout)
page_header(master, title) -> (head, info)—— 左标题右信息section_title(master, text)、card(master)、divider(master)field_label(master, text, *, width=112)、hint_label(master, text)empty_hint(master, title, detail="")—— 空态scrollable(master)/style_scrollable(frame)—— 统一滚动条的宽度与配色
列表(scroll_list)
-
LazyList(master, render_card, *, key_of=None, first_batch=12, load_more=8, ...)只创建可见的一批卡片,滚到底再补下一批;内容不足以填满视口时自动补齐。 卡片工厂由调用方提供,所以同一个列表能装任何东西:
def make_card(parent, item): card = ctk.CTkFrame(parent, corner_radius=12, border_width=1, border_color=COLOR_BORDER) ctk.CTkLabel(card, text=item["title"], anchor="w").pack(fill="x", padx=16, pady=(14, 0)) return card listing = LazyList(frame, make_card, key_of=lambda item: item["id"]) listing.pack(fill="both", expand=True) listing.render(items)
设计令牌
颜色全部是 (浅色, 深色) 二元组,交给 color_of() 按当前外观取值:
from ctk_kit import color_of
from ctk_kit.tokens import COLOR_ACCENT
color_of(COLOR_ACCENT) # 深色下返回 "#2f6fed"
LazyList、segmented 这类控件需要跟随外观重建的,切主题后调用 apply_appearance(mode),
再重建相关控件即可(repaint_native() 用于原生 tk 控件,它们的 configure 被 CTk 接管过)。
目录
ctk_kit/
├─ tokens.py 设计令牌
├─ theme.py 取色 / 外观切换 / 原生控件重绘
├─ buttons.py 按钮
├─ inputs.py 输入框 / 分段选择 / 开关
├─ dropdown.py 自绘下拉
├─ layout.py 页头、卡片、分隔线、空态、滚动容器
└─ scroll_list.py 懒加载列表
examples/
└─ showcase.py 全组件展示(八个页面,也可当模板改)
测试
pip install -e ".[test]"
pytest
测试跑在真实 Tk 窗口里(窗口全透明,不会在屏幕上闪),覆盖令牌、主题、按钮、输入框、
下拉、布局、懒加载列表,以及 examples/showcase.py 的八个示例页;没有图形环境时相关用例自动 skip。
CI 在 GitHub Actions 上跑 Python 3.8 – 3.14 全矩阵(.github/workflows/ci.yml),
无头环境下用 Xvfb 提供虚拟屏幕;CI 环境变量存在时如果建不出窗口会直接失败,而不是静默跳过。
发布
版本号只写在 pyproject.toml 的 version 字段里,发布流程全自动:
- 改
pyproject.toml的version(例如0.1.0→0.1.1),同步更新CHANGELOG.md; - 提交并
git push到 Gitee; - 镜像自动同步到 GitHub,Actions 跑完测试与构建后,会比较 PyPI 上的最新版本—— 本地版本更新才发布,同版本重推不会报错。
首次发布前需要在 PyPI 的 Publishing 页面给 GitHub 仓库
DashSalter/ctk-kit 添加 Trusted Publisher(workflow 文件名 ci.yml,环境留空),
之后由 pypa/gh-action-pypi-publish 凭 OIDC 换短期令牌上传,仓库里不需要存密码。
已知的坑
这些都是踩过的,记下来省得再踩:
CTkLabel不支持border_width/border_color,想加边框只能裹一层CTkFrame。CTkSegmentedButton同样不接受border_*,传了会抛ValueError。CTkScrollbar.set()内部会调update_idletasks(),而update_idletasks会立刻执行after_idle排的回调——用after_idle排分批渲染会在这个调用里递归建卡,改用after(1, ...)。- 滚动区域变化后,Tk 会回放一次
yscrollcommand(0, 1)。判断「滚到底」不能信回调参数, 要读canvas.yview()。 - customtkinter 接管了
tk.Frame.configure,给原生控件改背景色得走widget.tk.call(widget._w, "configure", "-background", bg),否则会碰到已销毁的子控件。
License
MIT
Metadata
Release files for ctk-kit 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ctk_kit-0.1.0.tar.gz | 36.5 kB | With provenance | Details for ctk_kit-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ctk_kit-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for ctk_kit-0.1.0-py3-none-any.whl |
Total release size: 62.7 kB
Release files / ctk_kit-0.1.0.tar.gz
| Download URL | ctk_kit-0.1.0.tar.gz |
|---|---|
| Size | 36.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ce1934f16ac15815cfe1fed7ad07d239c8a4ffbbef586174440b1a819d0c2e4
|
|
BLAKE2b-256 checksum How to use checksums |
64daeab2a437d9c014576608a1bcc8a4ef202644efe6701a60c0fb6fc5a75c1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.
Transparency logRelease files / ctk_kit-0.1.0-py3-none-any.whl
| Download URL | ctk_kit-0.1.0-py3-none-any.whl |
|---|---|
| Size | 26.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
95f2f2627d4ad4f0d38f39b22467fe2d43abb0b3fbd28427c12dd1370943245a
|
|
BLAKE2b-256 checksum How to use checksums |
a2f6f45c2752c00cc8e704ec08d86bd9b4c418070f8dd2e41cf0571b5ed8a07a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.
Transparency logSecurity for ctk-kit 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for ctk_kit-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.