8

I want to download an open source font and use it in my Python Tkinter Program.

How can I tell Tkinter to import a font from a directory or by putting the font in the same folder as the program ?

Note: I have searched for an answer for a while even reading API reference of Tkinter about every font related thing I could find. If there was an obvious answer to this question and I didn't know because maybe I didn't search hard enough or asked the wrong questions I am sorry.

Aprillion
  • 21,510
  • 5
  • 55
  • 89
Buğra Coşkun
  • 171
  • 3
  • 16
  • Will [this](http://stackoverflow.com/questions/31918073/tkinter-how-to-set-font-for-text) help? – Andrew Li Aug 07 '16 at 15:42
  • @AndrewL. Unfortunately It wasn't what I was looking for, I wanted to know If there was a way to import fonts into Tkinter but still thanks ! – Buğra Coşkun Aug 07 '16 at 19:57
  • Check out https://stackoverflow.com/questions/11993290/truly-custom-font-in-tkinter. It works for me on Windows. I haven't tried to find an equivalent for Mac OS X. – GaryMBloom Nov 16 '17 at 18:56

1 Answers1

2

This worked for me on Windows (and I would guess on any platform which supports the pyglet module)

import tkinter as tk
import pyglet, os

pyglet.font.add_file('myFont.ttf')  # Your TTF file name here

root = tk.Tk()
MyLabel = tk.Label(root,text="test",font=('myFont',25)) 

MyLabel.pack()
root.mainloop()
P S Solanki
  • 1,033
  • 2
  • 11
  • 26
  • 1
    Why dont you actually use `font=('myfont',25)` to actually not confuse the OP, and also the ttf font file name should be same as the ttf font name for `pyglet` to work properly, or it will not show the font. – Delrius Euphoria Aug 28 '20 at 11:27
  • 1
    @PSSolanki It doesn't works on Raspberry Pi 4. Can you tell why ? – sodmzs1 Apr 27 '21 at 10:23
  • @sodmzs1 Hey there, I've no idea how rasp pi works so I'd suggest ask this as a separate question :) – P S Solanki Apr 27 '21 at 12:12
  • No it isn't. The question isn't tagged with a specific operating system and this solution doesn't seem to work on Linux at least. And maybe not on any other operating system. – BlackJack Nov 23 '22 at 15:57
  • Then maybe suggest a solution that works on Linux? @BlackJack I specifically said the answer is only tested on Windows and I no longer work with tk (or windows for that matter. – P S Solanki Dec 02 '22 at 10:15
  • It's perfectly okay to say *this* isn't a general solution to the question when it simply isn't. That doesn't mean *I* have to waste time to find one. Because most probably there isn't one unless I or someone else would be willing to develop and test and maintain such a functionality for each platform the Python/Tk combo is generally working on. „Platform“ here may include different Windows versions and differences in Linux like X vs. Wayland. – BlackJack Dec 05 '22 at 13:51
  • Thanks Captain Obvious for pointing out the literal most obvious thing mentioned within the answer that this is not a general solution. I hadn't come across any cross platform solution so I responded what I had because the OP didn't have a specific requirement. I no longer work with tk/windows and hence can't update the answer. While I totally agree that it is `perfectly okay to` point this out, you didn't need to point that out explicitly when I myself mentioned that in the answer itself in the first line. I see your point, but it felt unnecessary. Cheers! – P S Solanki Dec 06 '22 at 16:28