Python 如何在自定义样式中更改tkk滚动条的宽度?

Python 如何在自定义样式中更改tkk滚动条的宽度?,python,tkinter,ttk,Python,Tkinter,Ttk,当为ttk滚动条定义自定义样式时,我被如何更改滚动条的宽度所困扰。我注意到,当我从另一个主题复制元素TScrollbar.grip时,宽度(厚度)会缩小,但当我查找元素选项style.element\u选项('My.Vertical.TScrollbar.grip')时,宽度(厚度)会缩小 从tkinter导入* 从tkinter导入ttk root=Tk() style=ttk.style() #从“clam”引擎导入元素。 创建样式元素(“My.Vertical.TScrollbar.dro

当为ttk
滚动条定义自定义样式时,我被如何更改滚动条的宽度所困扰。我注意到,当我从另一个主题复制元素
TScrollbar.grip
时,宽度(厚度)会缩小,但当我查找元素选项
style.element\u选项('My.Vertical.TScrollbar.grip')
时,宽度(厚度)会缩小

从tkinter导入*
从tkinter导入ttk
root=Tk()
style=ttk.style()
#从“clam”引擎导入元素。
创建样式元素(“My.Vertical.TScrollbar.droot”、“from”、“clam”)
style.element\u create(“My.Vertical.TScrollbar.thumb”、“from”、“clam”)
创建样式元素(“My.Vertical.TScrollbar.grip”、“from”、“clam”)
style.layout(“My.Vertical.TScrollbar”,
[('My.Vertical.TScrollbar.tour',{'children':
[('My.Vertical.TScrollbar.thumb',{'unit':'1','children':
[('My.Vertical.TScrollbar.grip',{'sticky':''}],
“粘性”:“nswe”},
'sticky':'ns'})])
style.configure(“My.Vertical.TScrollbar”,gripcount=0,background=“#464647”,troughcolor='#252526',borderwidth=2,
bordercolor='#252526',lightcolor='#252526',darkcolor='#252526')
容器=ttk.Frame(根)
画布=画布(容器)
scrollbar=ttk.scrollbar(容器,orient=“vertical”,command=canvas.yview,style=“My.vertical.TScrollbar”)
可滚动_frame=ttk.frame(画布)
可滚动的_frame.bind(
"",
lambda e:canvas.configure(
scrollregion=canvas.bbox(“全部”)
)
)
canvas.create_window((0,0),window=scrollable_frame,anchor=“nw”)
configure(yscrollcommand=scrollbar.set)
对于范围(50)内的i:
ttk.Label(可滚动的_框架,text=“示例滚动标签”).pack()
container.pack()
canvas.pack(side=“left”,fill=“both”)
滚动条包装(side=“right”,fill=“y”)
root.mainloop()

在对代码进行了一些外观更改以使其更具可读性之后,我能够在配置整体样式时通过指定一个
箭头大小=
选项来更改滚动条的宽度。这是在
style.configure()
调用中完成的,并显示在下面的行中,带有注释
#感谢您的帮助。
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()
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()