Vinman75
Vinman75

Reputation: 55

tkhtmlview HTMLLabel change font size?

I want to use HTMLLabel in one of my Tkinter projects', but I dislike that the font size is hardcoded to 14. I want it to be 10 instead.

Adding the font=("Calibri", 10) does nothing to render font family or size.

from tkinter import *
from tkhtmlview import HTMLLabel

html = "Some text! Always size 14 :("


root = Tk()
root.title('html')
root.geometry('300x300')

my_label = HTMLLabel(root, html=html, state="normal", font=("Calibri", 10))

my_label.pack(pady=20,
              padx=20,
              fill="both",
              expand=True)

root.mainloop()

enter image description here

I have noticed there is a variable FONT_SIZE in the Defs method of the class module html_parser.py:

class Defs():
    DEFAULT_TEXT_FONT_FAMILY = (
        "Segoe ui", "Calibri", "Helvetica", "TkTextFont")
    FONT_SIZE = 14
    }

If I change FONT_SIZE = 14 to FONT_SIZE = 10 in the sourced module, it works everywhere.

Does anyone know how to set my font size to 10 in my script without editing the imported html_parser.py module, as I would imagine it's not an appropriate practice to edit a module this way?

Upvotes: 1

Views: 1108

Answers (2)

Vansh Aggarwal
Vansh Aggarwal

Reputation: 1

I had a similar problem, i wanted to change the font-family to Courier, changing it in the html using the style attribute didn't do anything

So, changed the DEFAULT_TEXT_FONT_FAMILY constant in the Defs class along with writing it in the html inside the style tag

from tkhtmlview.html_parser import Defs

Defs.DEFAULT_TEXT_FONT_FAMILY = ("Courier")

Write in both the places and it will work

Upvotes: 0

Danyel 80be
Danyel 80be

Reputation: 46

Check setting the font size by the style. See the result:

from tkinter import *
from tkhtmlview import HTMLLabel
r=Tk()
r.title('Alles Normal')
r.geometry('280x430')
html_text='''
<h5 style="text-align: center;"><u>TOO</u></h5>
<p style="font-size: 12px;">Hello, my friends!
Are you fine!</p>
Yes, of course!
<p style="font-size: 18px;">I'm happy</p>
<p style="font-size: 25px;">Very happy</p>
'''
ml=HTMLLabel(r, html=html_text)
ml.fit_height()
ml.pack(pady=10, padx=10, fill='both', expand=True)
r.mainloop()

Upvotes: 3

Related Questions