繁体   English   中英

如何仅更改tkinter中的文本小部件的字体大小(而不是家庭)

[英]How to only change font size of text widget in tkinter(not family)

我正在尝试在tkinter中创建文本编辑器。

我正在更改标记属性上的字体大小和字体系列,但是问题是当我更改字体系列时,所选文本的大小将恢复为默认值,而当我更改大小时,字体系列将恢复为默认值。 我尝试了很多事情

def changeFontSize2():
     textPad.tag_add("bt2", "sel.first", "sel.last")
     textPad.tag_config("bt2",font = (False,2))

def changeFontSize6():
     textPad.tag_add("bt6", "sel.first", "sel.last")
     textPad.tag_config("bt6",font = (False,6))

def changeFontFamily1():
     textPad.tag_add("btArial", "sel.first", "sel.last")
     textPad.tag_config("btArial",font = ("Arial",False))

def changeFontFamily2():
      textPad.tag_add("btCourierNew", "sel.first", "sel.last")
      textPad.tag_config("btCourierNew",font = ("Courier New",False))

如果要将字体配置为与现有字体完全一样,但稍有更改,则可以基于现有字体创建新的字体对象,然后配置该新字体对象的属性。

例如:

...
import tkinter.font as tkFont
...

def changeFontSize2():
    textPad.tag_add("bt2", "sel.first", "sel.last")

    new_font = tkFont.Font(font=textPad.cget("font"))
    size = new_font.actual()["size"]
    new_font.configure(size=size+2)
    textPad.tag_config("bt2", font=new_font)

但是 ,完全按照上面的步骤进行操作会导致内存泄漏,因为每次调用该函数时都会创建新的字体对象。 最好是预先创建所有字体,或者想出一种动态创建字体然后缓存它们的方法,因此您只创建一次每种字体。

例:

fonts = {}
...
def changeFontSize2():
    name = "bt2"
    textPad.tag_add("bt2", "sel.first", "sel.last")

    if name not in fonts:
        fonts[name] = tkFont.Font(font=textPad.cget("font"))
        size = fonts[name].actual()["size"]
        fonts[name].configure(size=size+2)
        textPad.tag_configure(name, font=fonts[name])

我的建议是在程序开始时一次创建字体和标签,而不是即时创建它们。

fonts = {
    "default": tkFont.Font(family="Helvetica", size=12),
    "bt2": tkFont.Font(family="Helvetica", size=14),
    "btArial", tkFont.Font(family="Arial", size=12),
    ...
}
textPad.configure(font=fonts["default"])
textPad.tag_configure("bt2", font=fonts["bt2"])
textPad.tag_configure("btArial", font=fonts["btArial"])
...
def changeFontSize2():
    textPad.tag_add("bt2", "sel.first", "sel.last")

tkinter字体对象非常强大。 创建字体并使用它之后,如果重新配置字体(例如: fonts["bt2"].configure(family="Courier") ),则使用该字体的每个位置都会立即更新为使用新配置。

如您所说,有很多方法可以达到预期的效果。 由于许多小时后没有显示其他选项,因此无论如何,这是可行的。 我将字体的格式集中到一个函数中,并使用来自各个按钮的不同参数来调用它。

def setSelTagFont(family, size):
    curr_font = textPad.tag_cget('sel', "font")
    # Set default font information
    if len(curr_font) == 0:
        font_info = ('TkFixedFont', 10)
    elif curr_font.startswith('{'):
        font_info = curr_font.replace('{','').replace('} ','.').split('.')
    else:
        font_info = curr_font.split()

    # Apply requested font attributes
    if family != 'na':
        font_info = (family, font_info[1])
    if str(size) != 'na':
        font_info = (font_info[0], size)
    textPad.tag_config('sel', font=font_info)
    print("Updated font to:", font_info)

root = Tk()
textPad = Text(root)
textPad.pack()
textPad.insert(1.0, "Test text for font attibutes")
textPad.tag_add("sel", 1.0, END + '-1c')
textPad.focus_set()
frame = Frame(root)
frame.pack()
Button(frame, text="Size 2", command=lambda: setSelTagFont('na', 2)).pack(side='left',padx=2)
Button(frame, text="Size 6", command=lambda: setSelTagFont('na', 6)).pack(side='left',padx=2)
Button(frame, text="Family 1", command=lambda: setSelTagFont('Arial', 'na')).pack(side='left',padx=2)
Button(frame, text="Family 2", command=lambda: setSelTagFont('Courier New', 'na')).pack(side='left',padx=2)
root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM