norok2
norok2

Reputation: 26956

Python Tkinter: How to GET font info

I would like to get information on the actual font (especially the size) being used in the Tk menu. I was able to find that the font property of the menu is set to TkMenuFont. However, if I try to inspect its content, it seems I cannot get anything beyond some string representations:

print(dir(menu['font']))

outpus:

 ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', 
   '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 
   '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', 
   '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
   '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
   'string', 'typename']

So, the question is: how to get information on the Tk fonts?

Upvotes: 1

Views: 1101

Answers (1)

Kevin
Kevin

Reputation: 76254

The tkinter.font.Font class appears to be capable of turning menu['font'] into something with inspectable attributes:

import tkinter
from tkinter.font import Font

root = tkinter.Tk()
menu = tkinter.Menu()
font = tkinter.font.Font(font=menu["font"])
print(font.actual())

Result:

{'family': 'Segoe UI', 'size': 9, 'weight': 'normal', 'slant': 'roman', 'underline': 0, 'overstrike': 0}

You can access individual properties with the usual dict indexing syntax, for example:

font.actual()["size"]

Upvotes: 3

Related Questions