Tkinter support scrollbar look modification

In tkinter we cannot modify the look of scrollbars, there’s some codes that are meant to work (because they are not giving unknown attribute error) but which are changing nothing. Please support that fast as possible. To show you why i want that look at this window i’ve made:

As you see it would perfect if if i could change the scrollbars

See tkinter.ttk — Tk themed widgets — Python 3.9.1 documentation

style.configure(“RW.TLabel”, foreground=“red”, background=“black”)

    scrollbar = Scrollbar(DecodedTextBox, orient="vertical", command=DecodedTextBox.yview, cursor="arrow", style="RW.TLabel")
    scrollbar.pack(fill="y", side="right")
    DecodedTextBox.config(yscrollcommand=scrollbar.set)

    xscrollbar = Scrollbar(DecodedTextBox, orient="horizontal", command=DecodedTextBox.xview, cursor="arrow", style="RW.TLabel")
    xscrollbar.pack(fill="x", side="bottom")
    DecodedTextBox.config(xscrollcommand=xscrollbar.set)

it says unknown option ‘style’ in Scrollbar do you know why pls?

I checked a lots of other forum posts , all are saying that modifying scrollbars isn’t possible even by using ttk. So please support me in order to make them that add that function in the next updates

The documentation is lacking a lot of information, but the following works here:

from tkinter import *
from tkinter.ttk import *

app = Tk()
style = Style()
style.element_create('Fruitsalad.Horizontal.Scrollbar.trough', 'from', 'default')
style.element_create('Fruitsalad.Horizontal.Scrollbar.thumb', 'from', 'default')
style.element_create('Fruitsalad.Horizontal.Scrollbar.leftarrow', 'from', 'default')
style.element_create('Fruitsalad.Horizontal.Scrollbar.rightarrow', 'from', 'default')
style.element_create('Fruitsalad.Horizontal.Scrollbar.grip', 'from', 'default')
style.layout('Fruitsalad.Horizontal.TScrollbar',
    [
        ('Fruitsalad.Horizontal.Scrollbar.trough', {'children':
            [('Fruitsalad.Horizontal.Scrollbar.leftarrow', {'side': 'left', 'sticky': ''}),
             ('Fruitsalad.Horizontal.Scrollbar.rightarrow', {'side': 'right', 'sticky': ''}),
             ('Fruitsalad.Horizontal.Scrollbar.thumb', {'unit': '1', 'children':
                 [('Fruitsalad.Horizontal.Scrollbar.grip', {'sticky': ''})],
            'sticky': 'nswe'})],
            'sticky': 'we'}),
    ])
style.configure('Fruitsalad.Horizontal.TScrollbar',
    troughcolor='green',
    background='red',
    arrowcolor='white')
style.map('Fruitsalad.Horizontal.TScrollbar',
    background=[('pressed', '!disabled', 'pink'), ('active', 'orange')])
scrollbar = Scrollbar(app, orient='horizontal', style='Fruitsalad.Horizontal.TScrollbar')
scrollbar.place(width=150)
scrollbar.set(0.0, 0.5)

app.mainloop()

styled_ttk_scrollbar

2 Likes

thanks i will check that

It still does not work

In anyway even if it was working, Python must add an easier way, why background and foreground not working as meant to, as in other widgets? PLS SHARE THIS POST. THE MORE POPULAR IT IS, THE MORE IT IS CONSIDERED.

For examples you may want to take a look at GitHub - rdbende/Azure-ttk-theme: Azure theme is a beautiful and modern theme for ttk and https://www.reddit.com/r/Python/comments/lps11c/how_to_make_tkinter_look_modern_how_to_use_themes.

I tried azur’s theme on another widget but it didn’t work.

When talking about tk and hence tkinter, issues must include the OS. Peter’s code works as shown on Windows. I would not be surprised is it does not work on macOS, as the latter ignores some tk settings. For instance, button text backgrounds (but not foregrounds) are ignored. So a programmed white on color, as with turtledemo, shows as white on white :wink: … maybe unless one selects the fairly new macOS dark theme.

Ihsan, all caps shouting is a bit rude.

Fantastic - thanks Peter Suter for that solution. Can confirm it does work on MacOS. Here is my variant which works for both horizontal and vertical bars:

def make_scrollbar_styles(
        troughcolor='black',
        background='grey',
        arrowcolor='white',
) -> Tuple[str, str]:
    """
    Style the scrollbars.  Usage:
        parent_frame = ... # tk.Frame(...) or tk.Tk() or whatever you're using for the parent
        hstyle, vstyle = make_scrollbar_styles()
        vbar = ttk.Scrollbar(parent_frame, orient='vertical', style=vstyle)
        hbar = ttk.Scrollbar(parent_frame, orient='horizontal', style=hstyle)
    """
    style = Style()

    for is_hori in (True, False):
        v = "Horizontal" if is_hori else "Vertical"
        style.element_create(f'CustomScrollbarStyle.{v}.Scrollbar.trough', 'from', 'default')
        style.element_create(f'CustomScrollbarStyle.{v}.Scrollbar.thumb', 'from', 'default')
        style.element_create(f'CustomScrollbarStyle.{v}.Scrollbar.leftarrow', 'from', 'default')
        style.element_create(f'CustomScrollbarStyle.{v}.Scrollbar.rightarrow', 'from', 'default')
        style.element_create(f'CustomScrollbarStyle.{v}.Scrollbar.grip', 'from', 'default')
        style.layout(
            f'CustomScrollbarStyle.{v}.TScrollbar',
            [(f'CustomScrollbarStyle.{v}.Scrollbar.trough', {
                'children': [
                    # Commenting in these 2 lines adds arrows (at least horizontally)
                    # (f'CustomScrollbarStyle.{v}.Scrollbar.leftarrow', {'side': 'left', 'sticky': ''}) if is_hori else (f'CustomScrollbarStyle.{v}.Scrollbar.uparrow', {'side': 'top', 'sticky': ''}),
                    # (f'CustomScrollbarStyle.{v}.Scrollbar.rightarrow', {'side': 'right', 'sticky': ''})  if is_hori else (f'CustomScrollbarStyle.{v}.Scrollbar.downarrow', {'side': 'bottom', 'sticky': ''}),
                    (f'CustomScrollbarStyle.{v}.Scrollbar.thumb', {
                        'unit': '1',
                        'children': [(f'CustomScrollbarStyle.{v}.Scrollbar.grip', {'sticky': ''})],
                        'sticky': 'nswe'}
                     )
                ],
                'sticky': 'we' if is_hori else 'ns'}),
             ])
        style.configure(f'CustomScrollbarStyle.{v}.TScrollbar', troughcolor=troughcolor, background=background, arrowcolor=arrowcolor)
        # Comment in the following to customize disable/active colors, whatever that means
        # style.map(f'CustomScrollbarStyle.{v}.TScrollbar', background=[('pressed', '!disabled', disabledcolor), ('active', 'orange')])
    return "CustomScrollbarStyle.Horizontal.TScrollbar", "CustomScrollbarStyle.Vertical.TScrollbar"

1 Like

Hello,

can the documentation be updated so that it matches/reflects the workaround? Per Peter’s code, it is definitely not how the Scrollbar documentation explains it. From a users point of view, it makes no sense to have documentation on a feature if it clearly does not work as documented. Btw, I am using Windows 11, Python 3.11.5, tkinter ver. 8.6.

Is documentation on a feature first tested/vetted by other developers to verify its validity before being published?

Just as an fyi …, I actually came upon this issue because I was attempting to edit some
of the scroll bar features and noticed that the edits were having no affect on the scrollbar
features. I did reference the documentation including other tutorial websites that reference the python docs. After doing further searches on the topic, I came upon this thread.

No, because what you posted is incomplete and will not run far enough to show the error it did. But perhaps you are passing a ttk style to the tk Scrollbar.

Of course, we do so regularly. But someone must post a link to a specific section of a specific page. Then specify what is thought to be wrong, with valid evidence, or thought to be missing. Note that the tkinter docs include the tk and ttk docs by reference.

This does not say whether information is missing from the tcl/ttk Scollbar and [Style](https://Tcl8.6.13/Tk8.6.13 Documentation > Tk Commands > ttk_style) pages or only the tkinter general widget and Style sections. Both style docs reference a 2004 doc and it may have information that should be added to the tkinter doc. (I am going to read it to see.)

Peter does not say that the docs are erroneous.

1 Like

Is this thread not valid evidence? From the posted scrollbar examples, it has been shown that the features are not editable. The other two users had to use a rather lengthy and detailed workaround
code just to edit the color features of the scroll bar. New users to python I am sure would have a hard
time coming up with a similar workaround on their own.

It would help if you provided specific examples of the issue you’re seeing and what you want changed. You resurrected this one after ten months of silence, and that last message was after another year of silence.

Thanks for explaining some background. I will try to explain the “documentation is lacking a lot of information” comment:

I don’t 100% remember the specifics since this was some years ago and I don’t use tkinter that much. I think there were several problems that maybe explain the general user frustration:

The Python tkinter.ttk docs has only few small examples. These examples are perhaps tantalizingly close to the reader’s goal, so the reader assumes this is the applicable docs page about this topic. But how to generalize them is not clear at all. There’s no list of widgets or properties or what exactly the style database should contain.

Apparently these are also OS specific (but that’s not mentioned either as far as I can see). If that’s the case that maybe explains the frustration of trying out various snippets from forums etc. just to have them likely not work on your OS.

Python programmers who are not familiar with Tcl probably have a very difficult time to understand Tcl docs, so “including them by reference” doesn’t really work IMO. (Luckily for example the re docs don’t just point to the Perl regex docs and call it a day. :wink: )

Examples of what is missing: Search for these words (from my old code snippet above) on the Python ttk docs page:
TScrollbar, trough, thumb, leftarrow, rightarrow, troughcolor, arrowcolor, …
How would the reader know that these can / have to be used? I don’t remember where I found them, but I don’t think they are anywhere in the Python docs.

Unfortunately it is difficult for most people to help out here, as it’s just not clear where to even find the missing information. Unlike other Python libraries you can not easily read (or even find) the relevant source code to look up the values if I remember correctly.

I see you link to a page that contains some of the terms, so maybe that is the best authoritative source, and maybe it is where I eventually found them back then. Maybe it’s obvious to you to look there, I only remember that it was not the first place (or in the first 10 places) that I looked for some reason.

Maybe it would help if there were direct links to specific tables for each widget with detailed explanations “here is the list of property names” (and specific examples about how to read them from a Python perspective). Maybe you would argue this explanation already exists? I think for most Python programmers it would be much preferable to find the information directly in the Python docs in a way that makes sense in a Python context. I appreciate that this would be a huge task, maybe too much work to be realistic, but it would definitely improve the docs. Thanks for reading.

2 Likes

Of a specific, fixable error in the doc? By itself, no. (I already said that there are omissions.)

The specific claim in the OP’s code post is that Scrollbar(…, style=‘blah’) failed with “unknown option ‘style’”. Two commentators, experiments I have run, and tests in test.test_ttk run daily indicate otherwise for a ttk Scrollbar. To get that error, ‘style’ must have been misspelled or Scrollbar must have been a tk Scrollbar.

However, because of the vague claims that multiple people have problems, I did some specific, repeatable experiments that I can report on a tracker issue.

from tkinter.ttk import Style, Scrollbar
s = Style()
s.configure('TScrollbar', background='red')
sc = Scrollbar()
sc.pack()
# There is no red.  Something does seem wrong.
s.theme_use('default')
# Most of the scrollbar turns bright red, just as Ilhan wanted.

Using any other theme available on Windows removes the red. When using ‘default’, sorted(Style().element_names()) indicates that the default theme mostly builds widgets from generic elements like ‘background’, trough’, ‘thumb’, and arrows. The other themes I checked has specific elements for each of horizontal and vertical widgets.

As far as I could discover with a github search, no one has reported this difficulty on the CPython issue tracker. I intend to do so.

1 Like