为 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()
12345678_0001
相关分类