tkhtmlview HTMLLabel更改字体大小?

问题描述 投票:0回答:2

我想在我的一个 Tkinter 项目中使用 HTMLLabel,但我不喜欢字体大小被硬编码为 14。我希望它是 10。

添加

font=("Calibri", 10)
不会渲染字体系列或大小。

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()

我注意到类模块的

FONT_SIZE
方法中有一个变量
Defs
html_parser.py
:

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

如果我将

FONT_SIZE = 14
更改为
FONT_SIZE = 10
在源模块中,它可以在任何地方工作。

有谁知道如何在脚本中将字体大小设置为 10,而不编辑导入的

html_parser.py
模块,因为我认为以这种方式编辑模块不是适当的做法?

python tkinter html-parser
2个回答
2
投票

检查按样式设置的字体大小。查看结果:

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()

0
投票

我有一个类似的问题,我想将字体系列更改为 Courier,使用 style 属性在 html 中更改它没有做任何事情

因此,更改了 Defs 类中的 DEFAULT_TEXT_FONT_FAMILY 常量

from tkhtmlview.html_parser import Defs

Defs.DEFAULT_TEXT_FONT_FAMILY = ("Courier")

只有这个有效

© www.soinside.com 2019 - 2024. All rights reserved.