tkinter.TclError: character U+1f449 is above the range (U+0000-U+FFFF) allowed by Tcl

tkinter.TclError: character U+1f449 is above the range (U+0000-U+FFFF) allowed by Tcl

我正在尝试使用 tweepy 在 Tkinter window 上显示我的 Twitter 时间线。这是代码

import tweepy
import tkinter

consumer_key = 'xxxxxxxxxxxxxx'
consumer_sec ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
acc_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
acc_token_sec = 'xxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key,consumer_sec)
auth.set_access_token(acc_token,acc_token_sec)

api = tweepy.API(auth)

tweets = api.home_timeline()

tkwindow = tkinter.Tk()

for tweet in tweets:
    i = 1
    label = tkinter.Label(tkwindow, text=tweet.author.name + " " + str(tweet.created_at) + "\n" + str(tweet.text))
    if i == 5:
        break
tkwindow.mainloop()

但是我有以下错误

_tkinter.TclError: character U+1f449 is above the range (U+0000-U+FFFF) allowed by Tcl

我知道 tkinter 不能显示一些出现在真实推文中的特殊图标,但实际上,我不想显示那些,我只想显示推文的简单文本,

那么我怎样才能避免这个错误并只显示推文的文本

最简单的方法是去掉多余的字符。这可以在 for 循环的开头使用以下代码来完成:

char_list = [tweet[j] for j in range(len(tweet)) if ord(tweet[j]) in range(65536)]
tweet=''
for j in char_list:
    tweet=tweet+j

问题在 python 错误报告中描述:

我采用了那里发布的解决方案,并对其进行了调整以处理主题行有时以某种方式插入了错误字符的 gmail 邮件:

try:            
    iid = self.lib_tree.insert("", tk.END, tags="unchecked", 
        values=(message_id, subject, sender, epoch, date, labels,
                snippet))
except tk.TclError:
    print('old subject:', subject)
    
    import re

    # From: https://bugs.python.org/issue21084
    astral = re.compile(r'([^\x00-\uffff])')
    s = subject
    new_subject = ""
    for i, ss in enumerate(re.split(astral, s)):
        if not i%2:
            new_subject += ss
        else:
            new_subject += '?'
    print('new_subject:', new_subject)
    continue

加载大约 5,000 封 Gmail 邮件的程序输出:

$ bserve.py
old subject: Mserve June 18/2021
new_subject: Mser?e June 18/2021
old subject: mserve December 7, 2020
new_subject: mser?e December 7, 2020
old subject: Re: WinEunuuchs2Unix
new_subject: Re: WinEunuuchs2Uni?
old subject:  ORDER UPDATE: 2019 Cigarette Light...
new_subject: ? ORDER UPDATE: 2019 Cigarette Light...
old subject:  You've got 18 new ideas waiting for you!
new_subject: ? You'?e got 18 ne? ideas ?aiting for ?ou!
old subject:  Get inspired with these 18 trending ideas
new_subject: ? Get inspired ?ith these 18 trending ideas
old subject:  18 popular Pins for you
new_subject: ? 18 popular Pins for ?ou
old subject:  Get inspired with these 18 trending ideas
new_subject: ? Get inspired ?ith these 18 trending ideas
old subject:  Get inspired with these 18 trending ideas
new_subject: ? Get inspired ?ith these 18 trending ideas
old subject:  You've got 18 new ideas waiting for you!
new_subject: ? You'?e got 18 ne? ideas ?aiting for ?ou!
old subject:  We picked some Puzzle art Pins for you
new_subject: ? We picked some Pu??le art Pins for ?ou
old subject:  INFANTRY Mens LCD Digital Quartz Wrist Watch Tactical Sport Stainless Steel Navy is HOT on eBay, but quantity is limited
new_subject: ? INFANTRY Mens LCD Digital Quart? Wrist Watch Tactical Sport Stainless Steel Na?? is HOT on eBa?, but quantit? is limited
old subject:  POLJOT AVIATOR ATTACK PLANE MEN`S  RUSSIAN MILITARY WATCH is HOT on eBay, but quantity is limited
new_subject: ? POLJOT AVIATOR ATTACK PLANE MEN`S  RUSSIAN MILITARY WATCH is HOT on eBa?, but quantit? is limited

有些是显而易见的原因,例如“”。有些不太明显,例如“Mserve”具有无效的“v”并变为“Mser?e”。

我想当我旅行结束回家时在 Android 中写 mserve 项目电子邮件时出现了故障。