0

I have a customised scrollbar with ttk to remove arrows, I was wondering how I can change the colour of the scrollbar. 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.

ahmedquran12
  • 164
  • 1
  • 6
  • 19
  • Does this answer your question? [How ro change a Tkinter ScrolledText widget's Scrollbar color?](https://stackoverflow.com/questions/6965260/how-ro-change-a-tkinter-scrolledtext-widgets-scrollbar-color) – TheLizzard Feb 25 '21 at 12:17
  • 1
    You can use `style.configure("arrowless.Vertical.TScrollbar", troughcolor="blue")` to change the `ttk.Scrollbar` color. You may need to use another theme using `style.theme_use("alt")` to change the theme because not all the themes support changing color of scrollbar. – acw1668 Feb 25 '21 at 15:44
  • This worked. Are you able to add it as an answer so I can mark the question as solved? – ahmedquran12 Feb 25 '21 at 21:08
  • A [similar question](https://stackoverflow.com/q/28375591/5722359) was asked in 2015. [@Aivars's answer](https://stackoverflow.com/a/46917680/5722359) given in 2017 is simple to understand. – Sun Bear Feb 17 '23 at 09:40
  • Does this answer your question? [Changing the appearance of a Scrollbar in tkinter (using ttk styles)](https://stackoverflow.com/questions/28375591/changing-the-appearance-of-a-scrollbar-in-tkinter-using-ttk-styles) – Sun Bear Feb 17 '23 at 09:43

2 Answers2

1

You can use style.configure() to change the color of ttk.Scrollbar, for example:

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("alt") # "alt" theme supports changing color of scrollbar
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • I tried this but it doesn't work. I used different theme sincluding alt. I use Python 3.8. Has this been tested and verified as working? – Robin Oct 22 '21 at 17:43
0

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.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31