Modify the default font in Python Tkinter

后端 未结 2 794
花落未央
花落未央 2020-12-03 09:56

I\'m working on a GUI in Python2.7, with Tkinter, and I have an annoying problem.

I would like to define the default font used by all the widgets, if possible in one

2条回答
  •  既然无缘
    2020-12-03 10:50

    Tkinter has several built-in fonts -- TkDefaultFont, TkTextFont, TkFixedFont, etc. These are all what are called "named fonts". They are remarkably powerful -- change one of these and all widgets that use them will change as well.

    To change one of these fonts, get a handle to it and then use the configure method to change. For example, to change the size of TkDefaultFont to 48 you would do this:

    default_font = tkFont.nametofont("TkDefaultFont")
    default_font.configure(size=48)
    

    That's it. You don't have to do anything else -- everything that uses TkDefaultFont will instantly notice the change.

    In your question you imply you want TkDefaultFont font to be used by everything. To do that you can use option_add as you've shown:

    root.option_add("*Font", default_font)
    

    Note, however, that option_add only affects widgets created after you've called option_add, so you need to do it before creating any other widgets.

    Also note that you can give the font name to option_add if you don't want to bother with getting the font instance first (ie: root.option_add("*Font", "TkDefaultFont")).

提交回复
热议问题