Displaying emojis/symbols in Python using tkinter lib
Asked Answered
M

1

6

Does Tkinter support 16bit range characters, because I can not print emojis in the gui, when printing in python terminal, it works fine

>> print("😊")
>> 😊

but in tkinter, it shows this error:

_tkinter.TclError: character U+1f618 is above the range (U+0000-U+FFFF) allowed by Tcl
Moist answered 17/12, 2017 at 13:46 Comment(2)
I found out that if use the javascript format for this emoji : emoji = Label(labelframe, text= u'\uD83D\uDE05') it shows the emoji 😊 in tkinter, however, how can i convert the emojis to javascript format in python, i used this online converter : r12a.github.io/apps/conversion to get this \uD83D\uDE05, any ideas? – Moist
I would really appreciate if there are better solutions – Moist
J
5

There's a bug in Tkinter that it doesn't transparently map this for you, and definitely a bug in the underlying libraries (Tcl and Tk) that the string is not accepted as is and needs intervention at all. The state is that the underlying libraries currently require that the non-BMP characters in strings be encoded as surrogate pairs.

A little searching here provides code for actually doing this encoding.

Jard answered 17/12, 2017 at 17:1 Comment(3)
I know it's a bug because I know there's an issue number for it. Can't be bothered to look it up. Alas, it's an unfortunately messy problem to truly fix because it breaks a bunch of non-trivial assumptions. But at least you should have a reasonable workaround now… – Jard
would you recommend using another lib for gui, such as PyQt5 ? – Moist
You're totally asking the wrong guy about that. I don't ever choose to use either Qt or GTK in my applications, but I'm deeply biased having written parts of Tk. – Jard

© 2022 - 2024 β€” McMap. All rights reserved.