1

I am trying to give a modern look to my tkinter GUI. Here is the syntax I am using without any luck. Any help will be greatly helpful!

from Tkinter import *
my_font=("Segoe UI", 20, "bold")

root =Tk()
root.geometry("800x480)
Label=(root, text="my label", font = my_font).place(x=320, y=10)

root.mainloop()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Veejay
  • 515
  • 3
  • 7
  • 20
  • What error are you getting? – saud Dec 10 '17 at 06:49
  • no errors, but the font is non responsive. So essentially it does not change to segoe like we would expect – Veejay Dec 10 '17 at 07:10
  • 1
    Which OS are you on? Some *nix systems don't have MS fonts installed by default. And `tkinter` does not raise an error when it doesn't find a font. It just reverts to the default font. – saud Dec 10 '17 at 07:22
  • 1
    Ok, then please edit your question with the real code (without typos) and, if possible, share a screenshot of your GUI's output. – Billal Begueradj Dec 10 '17 at 08:26
  • 3
    @Veejay The code you've posted is broken (possibly not corresponding to the code you've tested) — as was mentioned by Billal in a question they've (prematurely?) deleted, the string `"800x480"` was not closed and the syntax for label instantiation is wrong (maybe `Label(...` without the equal sign?). Please [edit] your question so that people can focus on your real issue and not on a red herring. – gboffi Dec 10 '17 at 08:30

1 Answers1

2

My answer is going to address two points

  1. which font families you can use immediately in tkinter and
  2. what you can do if the family you'd like to use is not immediately available.

Available Font Families

Python 2

>>> from Tkinter import Tk
>>> from tkFont import families
>>> Tk(); available = families()   ### Tk() is needed to have a running tcl interpreter
<Tkinter.Tk instance at 0x7f977bcbfb90>
>>> len(available)
3011

Python 3

>>> from tkinter import Tk
>>> from tkinter.font import families
>>> Tk() ; available = families()
<tkinter.Tk object .>
>>> len(available)
68

at this point you can examine the fonts that are available printing, sorting slicing etc the contents of available — nb the names listed are the names that you have to use in the font definition tuple as in your example code.

Adding Fonts

As far as I can tell, this could be done in general as a system dependent (complicated) hack and the only published one is ONLY for Windows.

Said hack (I'll repeat: Windows only) is reported in this SO answer.

I don't know how to proceed in general.


Footnote

3011 font families for Python 2 and 68 for Python 3? yes, Python 2 is the system Python installed by apt on my Debian pc, while Python 3 is Anaconda's one and sees just the fonts that Anaconda installed in its private tree.

gboffi
  • 22,939
  • 8
  • 54
  • 85