简体   繁体   中英

Method to display emoji chars to IDLE/Terminal? UnicodeEncodeError - Non-BMP character not supported in Tk

I'm trying to print this example code

print ("Hello \U0001F914")

Now if I run this code on a MacOS terminal, it prints with the thinking emoji and if I run this on a linux terminal it does the same with alternative emoji. When I try to run this on IDLE, I run into this error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    print ("Hello \U0001F914")
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 6-6: Non-BMP character not supported in Tk

Is there a way to let it print on interpreters that can handle emojis and on interpreters that cannot show emojis, just replace it with something or not show it at all without it crashing?

My thinking right now is it to use the OS library and possibly get info about what the user is running then changing the executed coding accordingly.

If anyone could come up with a solution this would be much appreciated! Thanks

This is not ordinarily possible - and would require a too-extraordinary feat to be actually feasible.

The fact is simply that TCL, the technology stack supporting tkinter does not do Unicode beyond the BMP, that is, any character with odepoint larger than 0xffff.

The only workaround for that would be either to rewrite all the TCL part to support it, or to add a layer of code that would intercept all strings being passed to some of the text handling methods, and replace these characters by a pre-rendered inline image (and putting it properly in the text stream would require some work - I am not even sure if it is possible to have an inline image in tknter).

So, if you need to use these characters in a GUI created with Python, the way to go will be using Qt or GTK for the GUI;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM