Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify the default font in Python Tkinter

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 line. This line modify only the font used in Entry, or ComboBox:

root.option_add("*Font", "courier 10") 

but not the label of checkbox by example.

I found that a predefined font exist "TkDefaultFont" but I'm unable to change its configuration:

print tkFont.Font(font='TkDefaultFont').configure() tkFont.Font(font='TkDefaultFont').config(family='Helvetica', size=20) tk.TkDefaultFont = tkFont.Font(family="Helvetica",size=36,weight="bold") print tkFont.Font(font='TkDefaultFont').configure() 

return :

{'family': 'DejaVu Sans', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': -12} {'family': 'DejaVu Sans', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': -12}

(no errors, but nothing change !!)

What I'm doing wrong ?

like image 444
ericc Avatar asked Mar 17 '13 15:03

ericc


People also ask

What is the default text font for tkinter?

Output. Running the above code will set the default font as "lucida 20 bold italic" for all the widgets that uses textual information.

How do I change font size in Python GUI?

We can style the widgets using the tkinter. ttk package. In order to resize the font-size, font-family and font-style of Label widgets, we can use the inbuilt property of font('font-family font style', font-size).


2 Answers

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")).

like image 60
Bryan Oakley Avatar answered Sep 24 '22 10:09

Bryan Oakley


Caveat: although the question involves Py2.7, my answer is for Py3. The concepts are exactly the same. But instead of tkinter.font, one would use tkFont for Py2, etc.

If you want to change a default font, or any named font, you have to access the font object via nametofont():

def_font = tkinter.font.nametofont("TkDefaultFont") 

and then config the returned object, like

def_font.config(size=24) 

When you call

myfont = tkinter.font.Font(font="TkDefaultFont") 

you're actually creating a new named font that has the same attributes. To help show this:

str(def_font) gives "TkDefaultFont", and

str(myfont) gives "font1"

Oops.. Forgot to mention... You asked what you were doing wrong. One of the things is in your second line, you create and then config a new named font, but you don't capture it into a variable. If you captured it, you could use that named font. But that still would not modify the default named fonts. You would have to use nametofont() as explained above to accomplish that.

like image 22
Gary02127 Avatar answered Sep 25 '22 10:09

Gary02127