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

为 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()


肥皂起泡泡
浏览 45回答 1
1回答

12345678_0001

对代码进行一些外观更改以使其更具可读性后,我可以通过arrowsize=在配置整体样式时指定选项来更改滚动条的宽度。这是在style.configure()调用中完成的,并显示在下面的注释行中# <----- ADDED THIS.我的想法是基于我在小部件上找到的一些文档ttk.Scrollbar,其中指出虽然它不支持width像 atkinter.Scrollbar这样的选项,但您可以改为:使用样式配置此选项。您可能会发现配置arrowsize是更好的选择;在某些主题中,增加width可能不会增加箭头的大小。我首先尝试指定 a width,但没有任何影响。from tkinter import *from tkinter import ttkroot = 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()结果如下:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python