【问题标题】:Can't display unicode character for subscript 2 on Windows无法在 Windows 上显示下标 2 的 unicode 字符
【发布时间】:2012-02-03 01:23:22
【问题描述】:

我在 QLabel 中显示来自 xml 文件的 unicode 字符串。我需要在 QLabel 中显示 H2O,但将数字“2”作为下标(Unicode 字符 U+2082)。 QLabel 可以接受一个 html 字符串,但我不能把这个 html 放到 xml 中。

它在 Linux 中正确显示,但在 Windows 中显示一些垃圾而不是下标 2。我尝试了许多不同的方法(包括更改字体系列),但它们都不能在 Windows 中工作。为什么?

【问题讨论】:

  • 你使用什么代码来产生这个结果?您确定在标签中显示之前已正确解码文本吗?
  • 只需使用 self.lb_input_unit.setText(input_unit),其中 input_unit 是直接取自 xml 的 unicode 字符串,无需任何解码。 xml 包含 unicode 中的所有字符串,并且它们都在浏览器中正确显示。 Windows 上的 Python 有一些东西。正如我所说,下标 2 在 Linux 上显示良好。

标签: python windows unicode pyqt


【解决方案1】:

这可能是字体问题,而不是 Python。并非 Windows 中的所有字体都有 U+2082。您需要选择包含此字符的正确字体。

例如“Arial Unicode MS”有这个。考虑下面的例子:

import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()

unicode_font = QtGui.QLabel(u"Unicode Font: H\u2082O")
unicode_font.setStyleSheet("font-family: 'Arial Unicode MS', Arial, sans-serif; font-size: 15px;") 

normal_font = QtGui.QLabel(u"Normal Font: H\u2082O")
normal_font.setStyleSheet("font-family: Arial, sans-serif; font-size: 15px;")


layout = QtGui.QVBoxLayout()
layout.addWidget(unicode_font)
layout.addWidget(normal_font)
widget.setLayout(layout)
widget.show()
sys.exit(app.exec_())

在 Win 7 32 位上它给出:

【讨论】:

  • 不,它不起作用。我有 self.lb_input_unit = QLabel(u"H\u2082O") self.lb_input_unit.setStyleSheet("font-family:: Arial Unicode MS;") 但它仍然没有显示 2
  • @user665327:如果您刚刚粘贴的代码与您的脚本中的代码完全相同,则存在拼写错误。注意::。除此之外,请检查您是否确实拥有该字体。您也可以通过charmap.exe 查找合适的字体。
猜你喜欢
  • 1970-01-01
  • 2011-12-06
  • 2013-08-08
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 2023-04-01
  • 2013-06-05
相关资源
最近更新 更多