【问题标题】:How to change the width of a tkk Scrollbar in a custom style?如何以自定义样式更改 tkk 滚动条的宽度?
【发布时间】:2020-10-26 14:18:34
【问题描述】:

为 ttk Scrollbar 定义自定义样式时,我很困惑如何更改滚动条的宽度。我注意到,当我从另一个主题复制元素 TScrollbar.grip 时,宽度(厚度)会缩小,但是当我寻找元素选项 style.element_options('My.Vertical.TScrollbar.grip') 时,我会得到 ""

from tkinter import *
from tkinter import ttk

root = Tk()
style = ttk.Style()

# import elements from the 'clam' engine.
style.element_create("My.Vertical.TScrollbar.trough", "from", "clam")
style.element_create("My.Vertical.TScrollbar.thumb", "from", "clam")
style.element_create("My.Vertical.TScrollbar.grip", "from", "clam")

style.layout("My.Vertical.TScrollbar",
   [('My.Vertical.TScrollbar.trough', {'children':
       [('My.Vertical.TScrollbar.thumb', {'unit': '1', 'children':
            [('My.Vertical.TScrollbar.grip', {'sticky': ''})],
       'sticky': 'nswe'})],
   'sticky': 'ns'})])


style.configure("My.Vertical.TScrollbar", gripcount=0, background="#464647",troughcolor='#252526', borderwidth=2,
bordercolor='#252526', lightcolor='#252526', darkcolor='#252526')

container = ttk.Frame(root)
canvas = Canvas(container)
scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview, style="My.Vertical.TScrollbar")
scrollable_frame = ttk.Frame(canvas)
 
scrollable_frame.bind(
    "<Configure>",
    lambda e: canvas.configure(
        scrollregion=canvas.bbox("all")
    )
)

canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)

for i in range(50):
    ttk.Label(scrollable_frame, text="Sample scrolling label").pack()

container.pack()
canvas.pack(side="left", fill="both")
scrollbar.pack(side="right", fill="y")

root.mainloop()

【问题讨论】:

    标签: python tkinter ttk


    【解决方案1】:

    在对您的代码进行一些修饰以使其更具可读性之后,我能够在配置整体样式时通过指定 arrowsize= 选项来更改滚动条的宽度。这是在style.configure() 调用中完成的,并显示在下面带有注释# &lt;----- ADDED THIS.

    的行上

    根据我在 ttk.Scrollbar 小部件上找到的一些文档,我有了使用此选项的想法,该小部件指出,虽然它支持 width 选项,如 tkinter.Scrollbar 确实,您可以:

    使用样式配置此选项。您可能会发现配置arrowsize 是更好的选择;在某些主题中,增加width 可能不会增加箭头的大小。

    (我第一次尝试指定width,但没有任何影响。)

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    style = ttk.Style()
    
    # import elements from the 'clam' engine.
    style.element_create("My.Vertical.TScrollbar.trough", "from", "clam")
    style.element_create("My.Vertical.TScrollbar.thumb", "from", "clam")
    style.element_create("My.Vertical.TScrollbar.grip", "from", "clam")
    
    style.layout("My.Vertical.TScrollbar",
       [('My.Vertical.TScrollbar.trough',
         {'children': [('My.Vertical.TScrollbar.thumb',
                        {'unit': '1',
                         'children':
                            [('My.Vertical.TScrollbar.grip', {'sticky': ''})],
                         'sticky': 'nswe'})
                      ],
          'sticky': 'ns'})])
    
    style.configure("My.Vertical.TScrollbar", gripcount=0, background="#b0b0b0",
                    troughcolor='#252526', borderwidth=2, bordercolor='#252526',
                    lightcolor='#252526', darkcolor='#252526',
                    arrowsize=40)  # <----- ADDED THIS.
    
    container = ttk.Frame(root)
    canvas = Canvas(container)
    scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview,
                              style="My.Vertical.TScrollbar")
    scrollable_frame = ttk.Frame(canvas)
    
    scrollable_frame.bind("<Configure>",
                          lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
    
    canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
    canvas.configure(yscrollcommand=scrollbar.set)
    
    for i in range(50):
        ttk.Label(scrollable_frame, text=f"Sample scrolling label {i}").pack()
    
    container.pack()
    canvas.pack(side="left", fill="both")
    scrollbar.pack(side="right", fill="y")
    
    root.mainloop()
    

    结果如下所示:

    【讨论】:

    • 感谢您的帮助。
    猜你喜欢
    • 2014-01-13
    • 2011-06-03
    • 1970-01-01
    • 2013-08-27
    • 2011-02-03
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多