1 回答

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()
結果如下:
添加回答
舉報