为什么 tcl/tkinter 只支持 BMP 字符?

Ale*_*ite 5 python windows unicode tkinter tcl

我正在尝试在基于 tkinter 和 tcl 构建的 gui 中查询和显示 utf-8 编码字符。但是,我发现 tkinter 无法显示 4 字节字符,即大于 U+FFFF 的 unicode 代码点。为什么会这样?实现非 BMP 字符对 tcl 有什么限制?

我无法通过我的 gui 查询非 BMP 字符,但是如果它们出现在结果中,我可以复制/粘贴字符并通过 unicode-table.com 查看字符/代码点,尽管我的系统没有显示它。因此,该字符似乎显示为代码点 U+FFFD,但存储在具有正确代码点的视图中。

我在 Windows 7 上运行 Python 3.6.4 脚本。

更新:这是我在某些上下文中得到的错误,其中 4 字节 unicode 代码点超出了 BMP 字符的范围并且无法由 Tcl 处理

 File "Project/userInterface.py", line 569, in populate_tree
    iids.append(self.detailtree.insert('', 'end', values=entry))
  File "C:\Program Files (x86)\Python36-32\Lib\tkinter\ttk.py", line 1343, in insert
    res = self.tk.call(self._w, "insert", parent, index, *opts)
_tkinter.TclError: character U+1f624 is above the range (U+0000-U+FFFF) allowed by Tcl
Run Code Online (Sandbox Code Playgroud)

我通过使用正则表达式将超出范围的 unicode 字符替换为替换字符来处理此问题。

  for item in entries:
        #handles unicode characters that are greator than 3 bytes as tkinter/tcl cannot handle/display them
        entry = list(item)
        for i, col in enumerate(entry):
            if col and isinstance(col, str):
                re_pattern = re.compile(u'[^\u0000-\uD7FF\uE000-\uFFFF]', re.UNICODE)
                filtered_string = re_pattern.sub(u'\uFFFD', col) #replaces \u1000 and greater with the unknow character
                if filtered_string != col:
                    entry[i] = filtered_string
        entry = tuple(entry)
        iids.append(self.detailtree.insert('', 'end', values=entry))
Run Code Online (Sandbox Code Playgroud)