1

I am not able to change the font family of the label in tkinter.

I have written the code to explain the issue. I have also attached a link to show the output.

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Fonts not Changing!")
win.geometry("400x400")

# Times label
label1 = ttk.Label(win, text = "Brown-Times", font = ("Times",15))
label1.grid(row = 0, column = 0, sticky = 'W')

label12 = ttk.Label(win, text = "Brown-times", font = ("times",15))
label12.grid(row = 0, column = 1, sticky = 'W')

# Arial label
label2 = ttk.Label(win, text = "Brown-Arial", font = ("Arial",15))
label2.grid(row = 1, column = 0, sticky = 'W')

label22 = ttk.Label(win, text = "Brown-arial", font = ("arial",15))
label22.grid(row = 1, column = 1, sticky = 'W')

# Courier label
label3 = ttk.Label(win, text = "Brown-courier", font = ("courier",15))
label3.grid(row = 2, column = 0, sticky = 'W')
# Helvetica label
label4 = ttk.Label(win, text = "Brown-Helvetica", font = ("Helvetica",15))
label4.grid(row = 3, column = 0, sticky = 'W')

win.mainloop()

Image Link of the output

https://drive.google.com/open?id=1mcKSEW_Cmn30pFnlLbuaJdki7OlYvkQ9

Edit

I tried with the font families present

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Fonts not Changing!")
win.geometry("400x400")

'''
'fangsong ti', 'fixed', 'clearlyu alternate glyphs', 'courier 10 pitch', 'open look glyph', 'bitstream charter', 'song ti', 'open look cursor', 'newspaper', 'clearlyu ligature', 'mincho', 'clearlyu devangari extra', 'clearlyu pua', 'clearlyu', 'clean', 'nil', 'clearlyu arabic', 'clearlyu devanagari', 'gothic', 'clearlyu arabic extra'
'''

# Times label
label1 = ttk.Label(win, text = "Brown-fangsong ti", font = ("fangsong ti",15))
label1.grid(row = 0, column = 0, sticky = 'W')

label12 = ttk.Label(win, text = "Brown-fixed", font = ("fixed",15))
label12.grid(row = 0, column = 1, sticky = 'W')

# Arial label
label2 = ttk.Label(win, text = "Brown-courier", font = ("courier",15))
label2.grid(row = 1, column = 0, sticky = 'W')

label22 = ttk.Label(win, text = "Brown-clearlyu pua", font = ("clearlyu pua",15))
label22.grid(row = 1, column = 1, sticky = 'W')

# Courier label
label3 = ttk.Label(win, text = "Brown-courier", font = ("courier",15))
label3.grid(row = 2, column = 0, sticky = 'W')
# Helvetica label
label4 = ttk.Label(win, text = "Brown-Helvetica", font = ("Helvetica",15))
label4.grid(row = 3, column = 0, sticky = 'W')

win.mainloop()

But the output is similar enter image description here

Edit 2

I have tried with all the basic fonts available with tcl code as well as python please look at the difference.

TCL code

#!/usr/bin/wish

font create myFont1 -family {fangsong ti} -size 18 -weight bold
pack [label .labelVar1 -font myFont1 -text "Hello World"]

font create myFont2 -family fixed -size 18 -weight bold
pack [label .labelVar2 -font myFont2 -text "Hello World"]

font create myFont3 -family {clearlyu alternate glyphs} -size 18 -weight bold
pack [label .labelVar3 -font myFont3 -text "Hello World"]

font create myFont4 -family {courier 10 pitch} -size 18 -weight bold
pack [label .labelVar4 -font myFont4 -text "Hello World"]

font create myFont5 -family {open look glyph} -size 18 -weight bold
pack [label .labelVar5 -font myFont5 -text "Hello World"]

font create myFont6 -family {bitstream charter} -size 18 -weight bold
pack [label .labelVar6 -font myFont6 -text "Hello World"]

font create myFont7 -family {song ti} -size 18 -weight bold
pack [label .labelVar7 -font myFont7 -text "Hello World"]

font create myFont8 -family {open look cursor} -size 18 -weight bold
pack [label .labelVar8 -font myFont8 -text "Hello World"]

font create myFont9 -family newspaper -size 18 -weight bold
pack [label .labelVar9 -font myFont9 -text "Hello World"]

font create myFont10 -family {clearlyu ligature} -size 18 -weight bold
pack [label .labelVar10 -font myFont10 -text "Hello World"]

font create myFont11 -family mincho -size 18 -weight bold
pack [label .labelVar11 -font myFont11 -text "Hello World"]

font create myFont12 -family {clearlyu devangari extra} -size 18 -weight bold
pack [label .labelVar12 -font myFont12 -text "Hello World"]

font create myFont13 -family {clearlyu pua} -size 18 -weight bold
pack [label .labelVar13 -font myFont13 -text "Hello World"]

font create myFont14 -family clearlyu -size 18 -weight bold
pack [label .labelVar14 -font myFont14 -text "Hello World"]

font create myFont15 -family clean -size 18 -weight bold
pack [label .labelVar15 -font myFont15 -text "Hello World"]

font create myFont16 -family nil -size 18 -weight bold
pack [label .labelVar16 -font myFont16 -text "Hello World"]

font create myFont17 -family {clearlyu arabic} -size 18 -weight bold
pack [label .labelVar17 -font myFont17 -text "Hello World"]

font create myFont18 -family {clearlyu devanagari} -size 18 -weight bold
pack [label .labelVar18 -font myFont18 -text "Hello World"]

font create myFont19 -family gothic -size 18 -weight bold
pack [label .labelVar19 -font myFont19 -text "Hello World"]

font create myFont20 -family {clearlyu arabic extra} -size 18 -weight bold
pack [label .labelVar20 -font myFont20 -text "Hello World"]




puts [font families]

enter image description here

Python code

from tkinter import *
from tkinter import ttk

win = Tk()
win.title("Fonts not Changing!")
win.geometry("400x400")

fontList = ["{fangsong ti}", "fixed", "{clearlyu alternate glyphs}", "{courier 10 pitch}", "{open look glyph}", "{bitstream charter}", "{song ti}", "{open look cursor}", "newspaper", "{clearlyu ligature}", "mincho", "{clearlyu devangari extra}", "{clearlyu pua}", "clearlyu", "clean", "nil", "{clearlyu arabic}", "{clearlyu devanagari}", "gothic", "{clearlyu arabic extra}"]

label = [ttk.Label(win) for i in range(0,len(fontList))]

for i in range(0,len(fontList)):
    label[i].config(text = "Brown-" + fontList[i], font = (fontList[i],18,"bold"))
    label[i].grid(row = i, column = 0, sticky = 'W')

win.mainloop()

enter image description here

When these same fonts are ran using python it's acting weird as you can see how most of the fonts which were looking different using TCL codes are becoming same font with python(even though some fonts change).

I also removed tcl/tk and reinstalled(8.6 version) them even then it's of no use. Please anyone help...!

Star Rider
  • 389
  • 2
  • 15
  • I'm surprised that this code runs at all, because a `font=` option is not at all how you set the font for the Ttk `Label` widget - you use `style=` instead, to refer to a `Style` object that itself contains the font and other details. Your code would work just fine with ordinary Tkinter `Label` widgets. – jasonharper Jan 13 '19 at 16:44
  • 1
    Your code works for me on Windows with Python 3.6.5 . Maybe tkinter doesn't find the right font on your operating system? Try running `tkinter.font.families()` and see if the fonts you want are in the list. – jfhr Jan 13 '19 at 16:55
  • Are you sure you have all of those different fonts installed? Your code works fine on my mac. If tkinter can't find a font that you specify, it will fall back to a default. – Bryan Oakley Jan 13 '19 at 17:17
  • @jasonharper: not true. `font` is a valid option for ttk labels. – Bryan Oakley Jan 13 '19 at 17:19
  • I am getting a doubt is it a problem related to my OS. Does ubuntu support tkinter properly? I have the fonts installed at least i have times new roman in my fonts folder. – Star Rider Jan 13 '19 at 17:47

2 Answers2

3

It seems that the issue lies with the Anaconda3's python as per this post and most probably they are not going to solve this issue too.
Since I wanted to change the font at any cost. I installed python3.6 into my system. Then installed dependent libraries to run the above codes and typed sudo python3 filename.py this time things worked out properly.

Conclusion

If you want to use tkinter to it's full potential use your system's python instead of anaconda's python.

Star Rider
  • 389
  • 2
  • 15
0

Had the same issue, the solution for me here was to execute the file as a super user, so I think this problem has to do with linux permissions.