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

配置ttkbootstrap的Sash样式时触发AttributeError错误求助

解决ttkbootstrap配置Sash样式引发的AttributeError错误

配置ttkbootstrap的PanedWindow窗格窗口Sash样式时,触发如下错误:

Traceback (most recent call last):
  File "e:\Progetti Software\EnviroBlend\v0.4\new_window", line 66, in <module>   
    create_root()
  File "e:\Progetti Software\EnviroBlend\v0.4\new_window", line 33, in create_root
    style.configure('Sash', sashrelief='flat')
  File "c:\Users\palom\AppData\Local\Programs\Python\Python312\Lib\site-packages\ttkbootstrap\style.py", line 516, in configure
    ttkstyle = Bootstyle.update_ttk_widget_style(None, style)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\palom\AppData\Local\Programs\Python\Python312\Lib\site-packages\ttkbootstrap\style.py", line 5049, in update_ttk_widget_style
    builder_method = builder.name_to_method(method_name)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\palom\AppData\Local\Programs\Python\Python312\Lib\site-packages\ttkbootstrap\style.py", line 1086, in name_to_method
    func = getattr(StyleBuilderTTK, method_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'StyleBuilderTTK' has no attribute ''

测试代码:

from tkinter import *
import ttkbootstrap as ttk

import config

def closeWindow():
    config.programIsClosed=True
    config.root.destroy()

def create_root():
    config.root = ttk.Window(themename='darkly')
    config.root.title('Enviroblend')
    config.root.iconbitmap('icon.ico')
    config.root.state('zoomed')
    config.root.protocol('WM_DELETE_WINDOW', closeWindow)

    config.width_screen = config.root.winfo_screenwidth()
    config.height_screen = config.root.winfo_screenheight()
    
    config.root.minsize(int(config.width_screen*0.6), int(config.height_screen*0.6))
    
    config.top_frame = ttk.Frame(config.root, bootstyle='dark',    
                                 height=int(config.height_screen*0.025))
    config.top_frame.pack(fill=X, expand=True, side=TOP, anchor=N)
    
    main_frame = ttk.Frame(config.root, bootstyle='dark', height=int(config.height_screen*0.975))
    main_frame.pack(fill=X, expand=True, side=BOTTOM, anchor=S)
    
    style = ttk.Style()
    
    style.configure('Sash', sashrelief='flat', gripcount=15)
    style.configure('custom.TPanedwindow', background='red')
    main_panel = ttk.PanedWindow(main_frame, orient=HORIZONTAL, width=int(config.width_screen), 
                                 height=int(config.height_screen*0.975),      
                                 style='custom.TPanedwindow')
    main_panel.grid()
    
    left_frame = ttk.Frame(main_frame, bootstyle='dark')
    left_frame.grid(row=0, column=0)
    
    right_frame = ttk.Frame(main_frame, bootstyle='dark')
    right_frame.grid(row=0, column=1)
    
    main_panel.grid_columnconfigure(0, weight=1)
    main_panel.grid_columnconfigure(1, weight=8)
    
    main_panel.add(left_frame, weight=1)
    main_panel.add(right_frame, weight=8)
    
    config.root.style.configure('long.TNotebook', tabposition='nw')
    leftTabs = ttk.Notebook(left_frame, style='long.TNotebook')
    leftTabs.pack(fill="both", expand=YES)
    config.files_frame = ttk.Frame(leftTabs, bootstyle='dark')
    config.files_frame.pack(fill="both", expand=True)
    config.prefs_frame = ttk.Frame(leftTabs, bootstyle='dark')
    config.prefs_frame.pack(fill="both", expand=True)
    config.scenes_frame = ttk.Frame(leftTabs, bootstyle='dark')
    config.scenes_frame.pack(fill="both", expand=True)
    leftTabs.add(config.files_frame, text="Files", compound=LEFT)
    leftTabs.add(config.prefs_frame, text="Preferiti")
    leftTabs.add(config.scenes_frame, text="Scenes")
    
    config.root.mainloop()

create_root()

问题原因及解决方法

  • 错误原因:ttkbootstrap中Sash是PanedWindow的内部组件,无法单独通过style.configure('Sash', ...)进行样式配置,必须将Sash的样式参数绑定到PanedWindow的自定义样式中。
  • 修改步骤:
    1. 删除单独配置Sash样式的代码行:style.configure('Sash', sashrelief='flat', gripcount=15)
    2. 将Sash的参数合并到PanedWindow的样式配置中:
      style.configure('custom.TPanedwindow', background='red', sashrelief='flat', gripcount=15)
      
    3. 额外修正:main_panel.grid_columnconfigure是错误用法,PanedWindow不支持grid布局的列配置,已经通过add方法的weight参数控制窗格比例,所以删除这两行代码;同时将main_panel.grid()改为main_panel.pack(fill=BOTH, expand=True),更适配PanedWindow的布局特性。

修改后的完整代码

from tkinter import *
import ttkbootstrap as ttk

import config

def closeWindow():
    config.programIsClosed=True
    config.root.destroy()

def create_root():
    config.root = ttk.Window(themename='darkly')
    config.root.title('Enviroblend')
    config.root.iconbitmap('icon.ico')
    config.root.state('zoomed')
    config.root.protocol('WM_DELETE_WINDOW', closeWindow)

    config.width_screen = config.root.winfo_screenwidth()
    config.height_screen = config.root.winfo_screenheight()
    
    config.root.minsize(int(config.width_screen*0.6), int(config.height_screen*0.6))
    
    config.top_frame = ttk.Frame(config.root, bootstyle='dark',    
                                 height=int(config.height_screen*0.025))
    config.top_frame.pack(fill=X, expand=True, side=TOP, anchor=N)
    
    main_frame = ttk.Frame(config.root, bootstyle='dark', height=int(config.height_screen*0.975))
    main_frame.pack(fill=X, expand=True, side=BOTTOM, anchor=S)
    
    style = ttk.Style()
    
    # 将Sash样式参数合并到PanedWindow的自定义样式中
    style.configure('custom.TPanedwindow', background='red', sashrelief='flat', gripcount=15)
    main_panel = ttk.PanedWindow(main_frame, orient=HORIZONTAL, width=int(config.width_screen), 
                                 height=int(config.height_screen*0.975),      
                                 style='custom.TPanedwindow')
    main_panel.pack(fill=BOTH, expand=True)
    
    left_frame = ttk.Frame(main_panel, bootstyle='dark')
    right_frame = ttk.Frame(main_panel, bootstyle='dark')
    
    main_panel.add(left_frame, weight=1)
    main_panel.add(right_frame, weight=8)
    
    config.root.style.configure('long.TNotebook', tabposition='nw')
    leftTabs = ttk.Notebook(left_frame, style='long.TNotebook')
    leftTabs.pack(fill="both", expand=YES)
    config.files_frame = ttk.Frame(leftTabs, bootstyle='dark')
    config.files_frame.pack(fill="both", expand=True)
    config.prefs_frame = ttk.Frame(leftTabs, bootstyle='dark')
    config.prefs_frame.pack(fill="both", expand=True)
    config.scenes_frame = ttk.Frame(leftTabs, bootstyle='dark')
    config.scenes_frame.pack(fill="both", expand=True)
    leftTabs.add(config.files_frame, text="Files", compound=LEFT)
    leftTabs.add(config.prefs_frame, text="Preferiti")
    leftTabs.add(config.scenes_frame, text="Scenes")
    
    config.root.mainloop()

create_root()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 09:33:21