Search code examples
pythontkinterunicodechess

Chess pieces Unicode tkinter font issues


I'm working on a chess-based game in Python 3 using tkinter windows. I'm using the Unicode characters for chess pieces. However, they have two different looks (as seen here: https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode). I want to use the top one, but no matter what font I use, the ugly bottom version appears. What am I doing wrong?

Edit: It would be more convenient to use a monospaced font


Solution

  • The problem is likely that tkinter can't find the font you specified, or the font you specified doesn't have the unicode glyphs, so it's substituting in a font that has the glyphs.

    Here's a little program that will show you all of the fonts on your system, and what the chess pieces look like:

    import sys
    if sys.version_info.major is 3:
        import tkinter as tk, tkinter.font as tkFont
    else:
        import Tkinter as tk, tkFont
    
    root = tk.Tk()
    text = tk.Text(root, tabs=(200,))
    vsb = tk.Scrollbar(root, command=text.yview)
    text.configure(yscrollcommand=vsb.set)
    vsb.pack(side="right", fill="y")
    text.pack(side="left", fill="both", expand=True)
    
    pieces = u"\u2654\u2655\u2656\u2657\u2658\u2659\u265A\u265B\u265C\u265D\u265E\u265F\n"
    for count, family in enumerate(sorted(tkFont.families())):
        font = tkFont.Font(family=family, size=18)
        tag = "font-%s" % count
        text.tag_configure((tag,), font=font)
        text.insert("end", family)
        text.insert("end", "\t" + pieces, tag)
    
    root.mainloop()
    

    enter image description here