pythontkinterttkttkthemes

How to use the Sun Valley Dark Theme but change the font size?


I want to use the Sun Valley Dark theme

sv_ttk.set_theme("dark")

However, I want to change the fonts, possibly by using a style

@staticmethod
def getStdFont():
  return Font(family="Courier", size=16)

stdFont = getStdFont()

s = ttk.Style()
s.theme_create( "MyStyle", parent="alt", settings=
    {
        "TNotebook": {"configure": {"padding": [2, 2], "font": (stdFont) } },
        "TNotebook.Tab": {"configure": {"padding": [2, 2], "font": (stdFont) } },
        "TLabel": {"configure": {"font": (stdFont) } },
        "TButton": {"configure": {"font": (stdFont) } }
    })
s.theme_use("MyStyle")

It seems as if each are overwriting each other rather than combining the two. I tried to add the sv_ttk theme to myStyle but couldn't figure out how. I tried to change the font size of the sv_ttk theme but couldn't figure out how

I don't care which way it's done, I just want the dark theme with different font sizes.


Solution

  • Looking at the source code for sv_ttk I see that it defines its own font objects, which you can modify to be whatever size you want.

    For example, this function might be one way to make all of the fonts bigger:

    ...
    from tkinter.font import nametofont
    ...
    def big_font():
        nametofont("SunValleyCaptionFont").configure(size=-18)
        nametofont("SunValleyCaptionFont").configure(size=-18)
        nametofont("SunValleyBodyFont").configure(size=-20)
        nametofont("SunValleyBodyStrongFont").configure(size=-18)
        nametofont("SunValleyBodyLargeFont").configure(size=-22)
        nametofont("SunValleySubtitleFont").configure(size=-22)
        nametofont("SunValleyTitleFont").configure(size=-32)
        nametofont("SunValleyTitleLargeFont").configure(size=-42)
        nametofont("SunValleyDisplayFont").configure(size=-72)
    

    Or, if you want to be able to make the font bigger or smaller multiple times, here's a function that lets you pass in a positive number to make the font bigger, and a negative number to make ti smaller:

    def resize_font(incr):
        '''Change the size of SunValley fonts by the incr factor '''
        for font_name in (
            "Caption", "Body", "BodyStrong", "BodyLarge",
            "Subtitle", "Title", "TitleLarge", "Display"
        ):
            font = nametofont(f"SunValley{font_name}Font")
            size = font.cget("size")
            font.configure(size=size-incr)
    

    Note that negative font sizes represent font sizes in pixels, and a positive size represents a size in printers points. In the source code the package uses negative values to represent font sizes in pixels.