Changing the appearance of a Scrollbar in tkinter (using ttk styles) Changing the appearance of a Scrollbar in tkinter (using ttk styles) tkinter tkinter

Changing the appearance of a Scrollbar in tkinter (using ttk styles)


It looks like you just want to change the trough for a horizontal scrollbar under the Windows theme. The ttk widgets are constructed from a set of elements provided by a styling engine and combined using the declared layout. Under Windows the styling engine is the Windows Visual Styles API which means the programmer doesn't have any control over the colours or images used to draw most of the common elements. The button background, scrollbar trough and buttons and the thumb and even the grip drawn inside the scrollbar thumb are all provided by Windows.

It is possible to take control of this for application customization but at a cost of making your application no longer look standard on the given platform. To do this you have to provide your own UI elements and define new widget layouts. Ultimately this can turn into defining your own theme. The tcl scripts in the ttk library provide good examples to follow and there are even some complete (if old) themes using bitmaps to declare image based theme elements in the original version of ttk which was called 'tile'.

In this specific example to get a Windows horizontal scrollbar with a custom coloured background we need to redefine the layout to use the scrollbar trough from the Tk drawn elements. The elements used in the 'default' theme can be copied in and are defined using style configuration parameters and are then drawn by Tk itself and not passed off to a third party engine. The following code generates a scrollbar like this which uses the standard buttons and thumb provided by the vsapi styling engine but replaces the trough. This imported trough understands the troughcolor style configuration option and so we can define a colour to use now. All scrollbars using this style will use the same colour as the widget itself will not accept a troughcolor option. ie: you can't have one scrollbar be blue and another be red unless you define a new style for each new colour.

enter image description here

from tkinter import *from tkinter.ttk import *def main():    app = Tk()    style = Style()    # import the 'trough' element from the 'default' engine.    style.element_create("My.Horizontal.Scrollbar.trough", "from", "default")    # Redefine the horizontal scrollbar layout to use the custom trough.    # This one is appropriate for the 'vista' theme.    style.layout("My.Horizontal.TScrollbar",        [('My.Horizontal.Scrollbar.trough', {'children':            [('Horizontal.Scrollbar.leftarrow', {'side': 'left', 'sticky': ''}),             ('Horizontal.Scrollbar.rightarrow', {'side': 'right', 'sticky': ''}),             ('Horizontal.Scrollbar.thumb', {'unit': '1', 'children':                 [('Horizontal.Scrollbar.grip', {'sticky': ''})],            'sticky': 'nswe'})],        'sticky': 'we'})])    # Copy original style configuration and add our new custom configuration option.    style.configure("My.Horizontal.TScrollbar", *style.configure("Horizontal.TScrollbar"))    style.configure("My.Horizontal.TScrollbar", troughcolor="red")    # Create and show a widget using the custom style    hs = Scrollbar(app, orient="horizontal", style="My.Horizontal.TScrollbar")    hs.place(x=5, y=5, width=150)    hs.set(0.2,0.3)    app.mainloop()if __name__ == '__main__':    main()


It's easier if you use the clam theme:

import tkinter as tkfrom tkinter import ttkroot = tk.Tk()style = ttk.Style()style.theme_use('clam')# list the options of the style# (Argument should be an element of TScrollbar, eg. "thumb", "trough", ...)print(style.element_options("Horizontal.TScrollbar.thumb"))# configure the stylestyle.configure("Horizontal.TScrollbar", gripcount=0,                background="Green", darkcolor="DarkGreen", lightcolor="LightGreen",                troughcolor="gray", bordercolor="blue", arrowcolor="white")hs = ttk.Scrollbar(root, orient="horizontal")hs.place(x=5, y=5, width=150)hs.set(0.2,0.3)root.mainloop()


This does not appear to be possible in tkinter on Windows.The below answer says so:ScrolledText Scrollbar Color (Python Tkinter)

Scrollbar documentation: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/scrollbar.htmlSupported style fields: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-Scrollbar.html

I tried passing both 'background' and 'troughcolor' unsuccessfully on my Windows machine. I also tried applying the style to the general scrollbar:style.configure('TScrollbar', background="blue")None of my solutions worked.

Also another forum post agrees that you can't style the scrollbar background here:http://www.gossamer-threads.com/lists/python/python/822292