CustomTkinter:给 Python GUI 换个现代皮肤

CustomTkinter:给 Python GUI 换个现代皮肤 文章目录CustomTkinter给 Python GUI 换个现代皮肤CustomTkinter给 Python GUI 换个现代皮肤TomSchimansky 开源的 CustomTkinter在 GitHub 上拿到了 13,396 个 StarCustomTkinter 是一个基于 Tkinter 的 Python UI 库。它提供了一套新的 widget外观现代且支持深度定制。这些 widget 的创建和使用方式与标准 Tkinter 完全一致也能和普通 Tkinter 组件混用。这个库最实用的特性是自动适配系统主题。widget 和窗口颜色会跟随系统外观设置也可以手动指定为 light 或 dark 模式。同时支持 HighDPI 缩放在 Windows 和 macOS 上显示清晰。最终效果在各平台保持一致。安装通过 pip 直接安装pip3 install customtkinter升级现有版本pip3 install customtkinter --upgrade快速上手一个只有单个按钮的示例importcustomtkinter customtkinter.set_appearance_mode(System)customtkinter.set_default_color_theme(blue)appcustomtkinter.CTk()app.geometry(400x240)defbutton_function():print(button pressed)buttoncustomtkinter.CTkButton(masterapp,textCTkButton,commandbutton_function)button.place(relx0.5,rely0.5,anchorcustomtkinter.CENTER)app.mainloop()这段代码在 macOS 上运行的效果是一个居中的现代风格按钮窗口。使用CTkButton替代tkinter.Button其余逻辑与 Tkinter 没有区别。核心 widgetCustomTkinter 覆盖了常用 GUI 组件CTkButton支持图文混排通过compound参数控制布局CTkFrame / CTkScrollableFrame滚动框架支持水平和垂直方向CTkSlider / CTkSwitch / CTkProgressBar表单控件CTkEntry / CTkTextbox文本输入CTkComboBox / CTkOptionMenu下拉选择CTkTabview / CTkSegmentedButton标签页和分段按钮所有 widget 都支持主题色配置内置 blue、dark-blue、green 等预设主题。与现有生态兼容由于 CustomTkinter 的 API 设计和 Tkinter 一致现有项目迁移成本很低。第三方 Tkinter widget如 TkinterMapView也能直接嵌入使用不会破坏整体视觉风格。适用场景如果你正在用 Python 写桌面工具又不想被 Tkinter 的默认外观限制CustomTkinter 是一个直接的替换方案。学习曲线平缓代码改动量小效果提升明显。工具又不想被 Tkinter 的默认外观限制CustomTkinter 是一个直接的替换方案。学习曲线平缓代码改动量小效果提升明显。