我想使用 Sun Valley Dark 主题
sv_ttk.set_theme("dark")
但是,我想更改字体,可能通过使用样式
@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")
似乎每个都在互相覆盖而不是将两者结合起来。 我尝试将
sv_ttk
主题添加到 myStyle
但不知道如何操作。
我尝试更改 sv_ttk
主题的字体大小,但不知道如何更改
我不在乎它是如何完成的,我只是想要具有不同字体大小的深色主题。
查看 sv_ttk 的源代码,我发现它定义了自己的字体对象,您可以将其修改为您想要的任何大小。
例如,此函数可能是使所有字体变大的一种方法:
...
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)
或者,如果您希望能够多次放大或缩小字体,这里有一个函数,可以让您传入正数使字体变大,并传入负数使 ti 变小:
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)
请注意,负字体大小表示以像素为单位的字体大小,正字体大小表示以打印机点为单位的大小。在源代码中,包使用负值来表示以像素为单位的字体大小。