You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ttkbootstrap Meter组件示例运行报错:PIL.Image无CUBIC属性

解决ttkbootstrap Meter组件运行时的PIL.Image.CUBIC错误

复制ttkbootstrap官方Meter组件示例代码运行失败,触发错误:AttributeError: module 'PIL.Image' has no attribute 'CUBIC'. Did you mean: 'BICUBIC'?

使用的代码

import ttkbootstrap as ttk
from ttkbootstrap.constants import *

app = ttk.Window()

meter = ttk.Meter(
    metersize=180,
    padding=5,
    amountused=25,
    metertype="semi",
    subtext="miles per hour",
    interactive=True,
)
meter.pack()

# update the amount used directly
meter.configure(amountused = 50)

# update the amount used with another widget
entry = ttk.Entry(textvariable=meter.amountusedvar)
entry.pack(fill=X)

# increment the amount by 10 steps
meter.step(10)

# decrement the amount by 15 steps
meter.step(-15)

# update the subtext
meter.configure(subtext="loading...")

app.mainloop()

完整报错回溯信息

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\ttkbootstrap\gui-intro\meter\main.py", line 6, in <module>
    meter = ttk.Meter(
            ^^^^^^^^^^
  File "C:\Users\User\PycharmProjects\ttkbootstrap\venv\Lib\site-packages\ttkbootstrap\widgets.py", line 718, in __init__
    self._setup_widget()
  File "C:\Users\User\PycharmProjects\ttkbootstrap\venv\Lib\site-packages\ttkbootstrap\widgets.py", line 759, in _setup_widget
    self._draw_meter()
  File "C:\Users\User\PycharmProjects\ttkbootstrap\venv\Lib\site-packages\ttkbootstrap\widgets.py", line 856, in _draw_meter
    img.resize((self._metersize, self._metersize), Image.CUBIC)
                                                   ^^^^^^^^^^^
AttributeError: module 'PIL.Image' has no attribute 'CUBIC'. Did you mean: 'BICUBIC'?

Process finished with exit code 1

解决方案

  • 问题根源:ttkbootstrap旧版本中使用了PIL已废弃的Image.CUBIC常量,PIL当前版本统一使用Image.BICUBIC作为该插值方法的名称。
  • 临时修复(手动修改源码):
    1. 找到ttkbootstrap安装路径下的widgets.py文件(路径参考报错信息中的路径)
    2. 打开文件后定位到报错行(约第856行),将Image.CUBIC替换为Image.BICUBIC
    3. 保存文件后重新运行代码
  • 永久修复(升级版本):执行以下命令升级ttkbootstrap到最新版本,官方已修复该兼容性问题:
pip install --upgrade ttkbootstrap

内容的提问来源于stack exchange,提问作者Mirko Ruhl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 12:35:04