为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

肥皂起泡泡 2024-01-04 17:01:46
为 ttk 定义自定义样式时Scrollbar,我很困惑如何更改滚动条的宽度。我注意到,当我TScrollbar.grip从另一个主题复制元素时,宽度(厚度)会缩小,但是当我查找元素选项时,style.element_options('My.Vertical.TScrollbar.grip')我得到了"".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="#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()
查看完整描述

1 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

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

我的想法是基于我在小部件上找到的一些文档ttk.Scrollbar,其中指出虽然它支持width像 atkinter.Scrollbar这样的选项,但您可以改为:

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

我首先尝试指定 a 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()

结果如下:

https://img1.sycdn.imooc.com/659674200001384504320303.jpg


查看完整回答
反对 回复 2024-01-04
  • 1 回答
  • 0 关注
  • 42 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信