tkinter - How to set font for Text?

前端 未结 1 646
暗喜
暗喜 2020-12-16 15:53

I am trying to find the best font for displaying utf-8 characters in a tk.Text.

I let python print all the family names known to tk using this code:

相关标签:
1条回答
  • 2020-12-16 16:43

    When specifying fonts in this manner, use a tuple:

    text.configure(font=("Times New Roman", 12, "bold"))
    

    Even better, you can create your own custom font objects and specify the attributes by name. Note: before you can create a font object you must first create a root window.

    # python 2
    # import Tkinter as tk
    # from tkFont import Font
    
    # python 3
    import tkinter as tk
    from tkinter.font import Font
    
    root = tk.Tk()
    text = tk.Text(root)
    ...
    myFont = Font(family="Times New Roman", size=12)
    text.configure(font=myFont)
    

    The advantage to creating your own fonts is that you can later change any attribute of the font, and every widget that uses that font will automatically be updated.

    myFont.configure(size=14)
    
    0 讨论(0)
提交回复
热议问题