ragardner / tksheet

Python tkinter table widget for displaying tabular data
https://pypi.org/project/tksheet/
MIT License
400 stars 48 forks source link

Looking to customize scrollbar and scrollbar frame bg #213

Closed infiniteTesch closed 6 months ago

infiniteTesch commented 6 months ago

As the topic suggest im looking for a way to accomplish this. Can't seem to find customizable variables inside the tksheet class. Anyone knows?

Best regards InfiniteTesch

ragardner commented 6 months ago

Thanks for your question and sorry for the late reply,

I think I have found a way to do this, the test code I have been using sits inside sheet.py __init__ function:

style = ttk.Style()
for orientation in ("Vertical", "Horizontal"):
    style.element_create(f"Tksheet.{orientation}.TScrollbar.trough", "from", "default")
    style.element_create(f"Tksheet.{orientation}.TScrollbar.thumb", "from", "default")
    style.element_create(f"Tksheet.{orientation}.TScrollbar.grip", "from", "default")

    # in case you want to remove the arrow buttons uncomment below:

    # style.layout(
    #     f"Tksheet.{orientation}.TScrollbar",
    #     [
    #         (
    #             f"Tksheet.{orientation}.TScrollbar.trough",
    #             {
    #                 "children": [
    #                     (
    #                         f"Tksheet.{orientation}.TScrollbar.thumb",
    #                         {
    #                             "unit": "1",
    #                             "children": [(f"Tksheet.{orientation}.TScrollbar.grip", {"sticky": ""})],
    #                             "sticky": "nswe",
    #                         },
    #                     )
    #                 ],
    #                 "sticky": "ns" if orientation == "Vertical" else "ew",
    #             },
    #         )
    #     ],
    # )
    style.configure(
        f"Tksheet.{orientation}.TScrollbar",
        gripcount=0,
        borderwidth=2,
        background="#b0b0b0",
        troughcolor="red",
        bordercolor="#252526",
        lightcolor="#252526",
        darkcolor="#252526",
    )

Those colours are just to test it out

I will hopefully be adding the Sheet initialization and set_options() parameters for scrollbar colours in the next update

Edit: I forgot to mention you also have to set the style parameter for the scrollbars if using the above code

self.yscroll = ttk.Scrollbar(
    self,
    command=self.MT.set_yviews,
    orient="vertical",
    style="Tksheet.Vertical.TScrollbar",
)
self.xscroll = ttk.Scrollbar(
    self,
    command=self.MT.set_xviews,
    orient="horizontal",
    style="Tksheet.Horizontal.TScrollbar",
)

Edit 2: You also have to use the style.map() function it seems, something like shown in this stackoverflow answer

Edit 3: This seems to clash with the dropdown boxes scrollbars and cause an error

ragardner commented 6 months ago

@infiniteTesch I have now added the ability to change scroll bar appearance in version 7.1.0

documentation here: https://github.com/ragardner/tksheet/wiki/Version-7#scrollbar-appearance

Thanks again for your suggestion!