How do I change the font size in ttkbootstrap?

375 Views Asked by At

I simply want to increase the size of the font for all objects in my ttkbootstrap project. I've googled extensively and tried reading ttkbootstrap's sparse documentation but I haven't figured out how to do this. I've tried creating a style object, but that seems to introduce more problems and not actually change anything.

Here's some of my code:

import ttkbootstrap as ttk

window = ttk.Window(themename="cyborg")
window.overrideredirect(True)

top_frame = ttk.Frame(window, width=main_win_width-20, height=100)
top_frame.pack(side=ttk.TOP)

bottom_frame = ttk.Frame(window, width=main_win_width-20, height=400)
bottom_frame.pack(side=ttk.BOTTOM)

now = datetime.now()
current_date = now.strftime('%A') + ", " + now.strftime('%B') + " " + now.strftime('%d') + ', ' + now.strftime('%Y')

date_label = ttk.Label(top_frame, text=current_date, padding=20)
date_label.pack(side=ttk.LEFT)

So how do I, for example, make the font size of that label object larger? If there's a simple way to edit the settings of the theme I'm using, that would be ideal.

Also, I read that ttkbootstrap has a cleaner look than tkinter, which is why I'm using it, but I'm finding it difficult to style things. Any recommendations on that front also welcome.

1

There are 1 best solutions below

3
On BEST ANSWER

Here is an example how you can increase/decrease font size of the Label widget:

from datetime import datetime

import ttkbootstrap as ttk

main_win_width = 640
font_size = 12

window = ttk.Window(themename="cyborg")

top_frame = ttk.Frame(window, width=main_win_width - 20, height=100)
top_frame.pack(side=ttk.TOP)

bottom_frame = ttk.Frame(window, width=main_win_width - 20, height=400)
bottom_frame.pack(side=ttk.BOTTOM)

now = datetime.now()
current_date = (
    now.strftime("%A")
    + ", "
    + now.strftime("%B")
    + " "
    + now.strftime("%d")
    + ", "
    + now.strftime("%Y")
)

date_label = ttk.Label(top_frame, text=current_date, padding=20)
date_label.configure(font=("", font_size))
date_label.pack(side=ttk.LEFT)


def change_font_size(how):
    global font_size

    if how == "INC":
        font_size += 2
    elif how == "DEC":
        font_size -= 2

    date_label.configure(font=("", font_size))


button_inc = ttk.Button(
    bottom_frame, text="Increase size", command=lambda: change_font_size("INC")
)
button_inc.pack(side=ttk.BOTTOM)

button_dec = ttk.Button(
    bottom_frame, text="Decrease size", command=lambda: change_font_size("DEC")
)
button_dec.pack(side=ttk.BOTTOM, pady=10)

window.mainloop()

Creates this window:

enter image description here

Clicking on Decrease size/Increase size the Label with change the font size.