如何使用 ttk.style tkinter 更改滚动条的颜色

How to change the colour of scrollbar with ttk.style tkinter

我有一个带有 ttk 的自定义滚动条来删除箭头,我想知道如何更改滚动条的颜色。我的代码生成了一个按钮列表,其中的名称来自可以滚动浏览的目录。

我的代码:

import tkinter as tk
from tkinter import ttk, messagebox
import os

bgCOL = "#2F3342"
sideCOL = '#4B5169'
txtCOL = '#8490BA'
btmCOL = '#575B66'
scrollCOL = '#373b4f'

yVal = 0

root = tk.Tk()

# Sets a new style for the scollbar to remove the arrows
style = ttk.Style(root)
style.layout('arrowless.Vertical.TScrollbar', [('Vertical.Scrollbar.trough', 
        {'children': [('Vertical.Scrollbar.thumb', {'expand': '1', 'sticky': 'nswe'})], 'sticky': 'ns'})])


canvas = tk.Canvas(root, width=348, height=665, bg=scrollCOL, highlightthickness=0)
scroll_y = ttk.Scrollbar(root, orient="vertical", command=canvas.yview, style='arrowless.Vertical.TScrollbar')
frame2 = tk.Frame(canvas, bg=scrollCOL)



for file in os.listdir('userSounds/data'):

    yVal = yVal + 1
    button = tk.Button(frame2, font=('Bahnschrift', 16), bd=0, highlightthickness=0, bg=sideCOL, fg='white',
        text=file.replace('.txt', ''), justify='left')   

    button.grid(row=yVal, column =0, pady=5, padx=9)
    button['width'] =27


# Puts the frame in the canvas
canvas.create_window(0, 0, anchor='nw', window=frame2)

# Makes sure everything is displayed before configuring the scrollregion
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'), yscrollcommand=scroll_y.set)
                
canvas.pack(side='left')
scroll_y.pack(fill='y', side='right')

root.mainloop()

非常感谢任何帮助。

简单的回答你不会reference. You can use a tk.Canvas to create something that looks like a scrollbar like what someone did here

您可以使用style.configure()来改变ttk.Scrollbar的颜色,例如:

style.configure("arrowless.Vertical.TScrollbar", troughcolor="blue") # change blue to whatever color you want

然而并非所有主题都支持更改滚动条的颜色,您可能需要通过调用 style.theme_use():

使用另一个支持它的主题
style.theme_use("alt") # "alt" theme supports changing color of scrollbar