Home > front end >  tkhtmlview HTMLLabel change font size?
tkhtmlview HTMLLabel change font size?

Time:02-03

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?

CodePudding user response:

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()
  •  Tags:  
  • Related