简体   繁体   English

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

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

I have a customised scrollbar with ttk to remove arrows, I was wondering how I can change the colour of the scrollbar.我有一个带有 ttk 的自定义滚动条来删除箭头,我想知道如何更改滚动条的颜色。 My code generates a list of buttons with names from a directory that can be scrolled through.我的代码从可以滚动的目录中生成一个带有名称的按钮列表。

My code:我的代码:

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()

Any help is GREATLY appreciated.任何帮助是极大的赞赏。

You can use style.configure() to change the color of ttk.Scrollbar , for example:您可以使用style.configure()更改ttk.Scrollbar的颜色,例如:

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

However not all the themes support changing color of scrollbar, you may need to use another theme that supports it by calling style.theme_use() :然而并不是所有的主题都支持改变滚动条的颜色,你可能需要通过调用style.theme_use()来使用另一个支持它的主题:

style.theme_use("alt") # "alt" theme supports changing color of scrollbar

The simple answer you can't reference .您无法参考的简单答案。 You can use a tk.Canvas to create something that looks like a scrollbar like what someone did here .您可以使用tk.Canvas创建看起来像滚动条的东西,就像有人在这里所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM