Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 Tkinter fonts not working

I am using python 3.3 with tkinter, and the package python3-tk is installed. In most docs the old "import tkFont" is used, which is not working any more.

This is supposed to work:

from tkinter import font
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
font.families()

However, I get this exception on the second line:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/tkinter/font.py", line 92, in __init__
    root.tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'tk'

I checked http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html and http://www.tkdocs.com/tutorial/fonts.html which were the most useful tkinter docs so far.

Unfortunately I still can't figure out what I am doing wrong.

like image 399
percidae Avatar asked Sep 18 '15 20:09

percidae


People also ask

What is Tkfont?

A simple font chooser for Tkinter that allow the user to select the font family among the fonts available on his/her system. The size and style (bold, italic, underline, strikethrough) of the text can be set too.

What is the font style in Python?

Sometimes terminal text can be hard to read, the fontstyle module is package hosted on pypi.org for manipulating text. It can be used to break up the noise with some additional formatting, add colors, font weights and other styles to make it more readable.


1 Answers

You should import font not fonts. Also, if the code you posted is actual code, you are neglecting to create a root window before working with fonts. You must create a root window first.

from tkinter import font
import tkinter as tk
...
root = tk.Tk()
...
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
font.families()
like image 199
Bryan Oakley Avatar answered Sep 29 '22 20:09

Bryan Oakley