【问题标题】:Text Widget does not display Unicode Characters文本小部件不显示 Unicode 字符
【发布时间】:2018-01-10 17:14:12
【问题描述】:

我正在开发一个区域语言文本编辑器,它从文本小部件中获取区域 Unicode 字符(我使用的脚本是 Gurmukhi,Unicode 兼容字体是 Raavi)并将其打印在终端屏幕上。

现在当我将一些字符串复制并粘贴到文本小部件时出现问题,它会转换为如图所示的框,但它会在终端窗口上打印完美的字符串。

虽然我已经尝试过 codecsencodingdecoding 功能,但是,这也是徒劳的。

我在 Tkinter 中找不到与 Text 小部件的 Unicode 输入机制相关的任何答案。

我们如何在文本小部件中显示完美的 unicode 字符串 ਸਤਵਿੰਦਰ?

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter.font as tkFont
from tkinter import *

def retrieve_input(self):
    inputValue = content_text.get("1.0", "end-1c")
    print(inputValue)

root = Tk()
root.call('encoding', 'system', 'utf-8')
customFont = tkFont.Font(family='Raavi', size=17)
root.title("Unicode Handling")
root.geometry('400x200+150+200')
content_text = Text(root, wrap='word', font=customFont)
content_text.configure(font=customFont)
content_text.focus_set()
content_text.pack(expand='yes', fill='both')
scroll_bar = Scrollbar(content_text)
content_text.configure(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=content_text.yview)
scroll_bar.pack(side='right', fill='y')
root.bind("<space>", retrieve_input)
root.mainloop()

这里是输入和输出:

【问题讨论】:

  • 您确定您使用的“Raavi”字体支持 unicode 吗?使用字符串不是配置字体的正确方法。也许小部件正在回退到不支持您的字符的默认字体。
  • 我已尝试使用 [此处] (stackoverflow.com/questions/31918073/…) 给出的解决方案,使用输入字体处理 Unicode 文本。据我所知,Python 使用编解码器处理 Unicode 文本,但它的编码和解码功能不起作用。
  • 要显示一个字符,字体必须有一个字形。它可以 100% 正确编码或解码,但如果字体不支持该字符,则小部件将无法正确显示该字符。第一步是确保使用合适的字体。
  • @BryanOakley 提到的字体与 Unicode 兼容,但仍显示相同的行为。 Tkinter 是否支持印度语脚本?

标签: python unicode tkinter


【解决方案1】:

我们如何在文本小部件中显示完美的 unicode 字符串 ਸਤਵਿੰਦਰ?

假设您安装了正确的字体,您的代码应该可以工作。我已将您的代码简化为一个更简单的示例:

import tkinter.font as tkFont
from tkinter import *

root = Tk()
customFont = tkFont.Font(family='Raavi', size=17)
content_text = Text(root, font=customFont, width=20, height=4)
content_text.pack(expand='yes', fill='both')
content_text.insert("end", 'ਸਤਵਿੰਦਰ')

root.mainloop()

在我的系统上,结果如下:

当我打印customFont.actual() 的结果时,我得到以下信息(我没有安装“Ravii”字体,因此 tkinter 将替换一个备用字体,这可能在您的系统上有所不同):

{
    'slant': 'roman', 
    'underline': 0, 
    'size': 17, 
    'family': 'DejaVu Sans', 
    'overstrike': 0, 
    'weight': 'normal'
}

要查看您安装的 tkinter 将识别的所有字体系列的列表,请运行以下代码:

from tkinter import font, Tk

root = Tk()
print(font.families())

【讨论】:

  • 我试图在我的系统上运行您给定的代码,但它显示相同的框而不是文本。不知道为什么?
  • 我得到以下结果:{'underline': 0, 'weight': 'normal', 'family': 'fixed', 'slant': 'roman', 'size': 17, 'overstrike':0}
  • @programmer_123:这意味着您没有正确指定字体,因此 tkinter 正在回退到默认值。它告诉您,尽管您提出了要求,但它实际使用的是一个名为“fixed”的字体系列。
  • 是的,我知道了('fangsong ti', 'fixed', 'clearlyu alternate glyphs', 'courier 10 pitch', 'open look glyph', 'bitstream Charter', 'song ti' , 'open look cursor', 'newspaper', 'clearlyu ligature', 'mincho', 'clearlyu devangari extra', 'clearlyu pua', 'clearlyu', 'clean', 'nil', 'clearlyu arabic', 'clearlyu devanagari', 'gothic', 'clearlyu arabic extra') 在运行 print(font.families()) 之后,它不包含 Raavi。我们如何在家族中插入字体?
  • @programmer_123:我无法在评论中回答。
猜你喜欢
  • 1970-01-01
  • 2011-10-08
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多