亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以自定義樣式更改 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 關注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號