TkinterEP / python-tkextrafont

C-extension for Tcl to allow loading custom font files into Tcl/Tk applications, packaged for Python's Tkinter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I set a font size?

karimawi opened this issue ยท comments

I've been scouring the internet for ways to implement custom OpenType fonts into a Tkinter GUI, when I tried tkextrafont, at first I was using this form:

import tkinter as tk
from tkextrafont import Font
import os
cwd = os.path.dirname(os.path.realpath(__file__))

window = tk.Tk()
font = Font(file=cwd+r'\Quicksand-Medium.ttf', family="Quicksand")
tk.Label(window, text="This is Quicksand", font=(font, 20)).pack() #<<<<<<
#                                               ^^^^^^^^^^
window.mainloop()

which doesn't work for some reason, it just renders default font.
I was actually going nuts, it took me ages to realize that specifying a font size will break it, after I got that, I started looking for another way to specify a font size and style (i.e bold, italic, underline), but couldn't find any so, I could use some help ๐Ÿ˜…

I spent whole 3 hours trying to find a solution for this, and I literally just found out I can just use the name parameter and be able to set font size and style just like normal, hopefully this helps someone though.

This should work

  import tkinter as tk
  from tkextrafont import Font
  import os
  cwd = os.path.dirname(os.path.realpath(__file__))
  
  window = tk.Tk()
  font = Font(file=cwd+r'\Quicksand-Medium.ttf', family="Quicksand",name='Quicksand Medium')
  tk.Label(window, text="This is Quicksand", font=(Quicksand Medium, 20)).pack() 
  window.mainloop()

omg, I just realized it's the family that I'm using not the name, this is a demonstration of how dumb a human being can get ๐Ÿ™ƒ ,
anyway, I still hope that I've helped someone out there, maybe?

Thanks for this absolutely great library, this saved my project and I can't be grateful enough ๐Ÿ™๐Ÿป .