Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文本小部件不显示Unicode字符_Python_Unicode_Tkinter - Fatal编程技术网

Python 文本小部件不显示Unicode字符

Python 文本小部件不显示Unicode字符,python,unicode,tkinter,Python,Unicode,Tkinter,我正在开发一个区域语言文本编辑器,它从文本小部件中获取区域Unicode字符(我使用的脚本是Gurmukhi,Unicode兼容字体是Raavi),并将其打印到终端屏幕上 现在问题出现了,当我将一些字符串复制并粘贴到文本小部件中时,它会被转换到如图所示的框中,但它会在终端窗口上打印出完美的字符串 虽然我尝试了编码和解码功能,但是,这也是徒劳的 我在Tkinter中找不到任何与文本小部件的Unicode输入机制相关的答案 如何显示完美的unicode字符串ਸਤਵਿੰਦਰ 文本小部件 这是我的密码:

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

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

虽然我尝试了
编码
解码
功能,但是,这也是徒劳的

我在Tkinter中找不到任何与文本小部件的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()
#/usr/bin/env python
#-*-编码:utf-8-*-
将tkinter.font作为tkFont导入
从tkinter进口*
def检索_输入(自身):
inputValue=content\u text.get(“1.0”,“end-1c”)
打印(输入值)
root=Tk()
root.call('encoding'、'system'、'utf-8')
customFont=tkFont.Font(family='Raavi',size=17)
root.title(“Unicode处理”)
根几何体('400x200+150+200')
content\u text=text(root,wrap='word',font=customFont)
content\u text.configure(font=customFont)
content\u text.focus\u set()
content\u text.pack(expand='yes',fill='both')
滚动条=滚动条(内容文本)
content\u text.configure(yscrollcommand=scroll\u bar.set)
滚动\u bar.config(命令=content\u text.yview)
滚动条。包装(侧面为“右”,填充为“y”)
root.bind(“,检索\u输入)
root.mainloop()
以下是输入和输出:

如何显示完美的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将替换备用字体,这可能与您的系统不同):

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

from tkinter import font, Tk

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

您确定您使用的“Raavi”字体支持unicode吗?使用字符串不是配置字体的正确方法。也许小部件正在退回到不支持您的字符的默认字体。我尝试使用[here]()给出的解决方案来处理Unicode文本,使用字体作为输入。据我所知,Python使用编解码器处理Unicode文本,但其编码和解码功能不起作用。要显示字符,字体必须有一个字形。它可以100%正确编码或解码,但是如果字体不支持字符,那么小部件就不可能正确显示字符。第一步是确保使用合适的字体。@BryanOakley上述字体与Unicode兼容,但仍显示相同的行为。Tkinter支持Indic脚本吗?我试图在我的系统上运行给定的代码,但它显示相同的框而不是文本。我不知道为什么?我得到了以下结果:{'underline':0,'weight':'normal','family':'fixed','Slante':'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'、'Paper'、'clearlyu连字'、'mincho'、'clearlyu devangari extra'、'clearlyu pua'、'clearlyu Lyu阿拉伯语extra'、'clearlyu'、'nil'、'运行
print(font.families())
后,它不包含Raavi。我们如何在族中插入字体?@programmer\u 123:我无法在评论中回答这个问题。
from tkinter import font, Tk

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