【问题标题】:How can I add an item with unicode characters to a QComboBox in python?python - 如何将带有unicode字符的项目添加到python中的QComboBox?
【发布时间】:2018-05-04 22:07:01
【问题描述】:

我正在尝试将前缀单位添加到 python 中的QComboBox,具体取决于参数的范围。问题是当我尝试添加“μ”时,它会显示为“Î1/4”,这不是我想要的。

我目前使用的代码是:

def build_unit_box(self, measure):
    listed = []
    if measure in {'P', 'frep'}:
        for pref in ['', 'k', 'M', 'G']:
            listed.append(str(pref + units.get(measure)))
        exec("%s" % 'self.unit_' + measure + '.addItems(listed)')
    elif measure in {'W', 'lambda', 'tau'}:
        for pref in ['', 'm', u'\u03bc'.encode('utf-8'), 'n']:
            print pref
            listed.append(str(pref + units.get(measure)))
        exec("%s" % 'self.unit_' + measure + '.addItems(listed)')

如果我输入print u'\u03bc',则会打印出正确的字符。

我该如何解决这个问题?

【问题讨论】:

    标签: python-2.7 qt unicode pyqt qcombobox


    【解决方案1】:

    我发现了问题。由于我从另一个函数复制粘贴了一些代码,我将字符类型转换为str,导致编码错误。 它完美地适用于:

    def build_unit_box(self, measure):
        listed = []
        if measure in {'P', 'frep'}:
            for pref in ['', 'k', 'M', 'G']:
                listed.append(str(pref + units.get(measure)))
            exec("%s" % 'self.unit_' + measure + '.addItems(listed)')
        elif measure in {'W', 'lambda', 'tau'}:
            for pref in ['', 'm', u'\u03bc', 'n']:
                print pref
                listed.append(unicode(pref + units.get(measure)))
            exec("%s" % 'self.unit_' + measure + '.addItems(listed)')
    

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2021-04-12
      • 2021-06-14
      • 2023-03-23
      • 2023-03-23
      • 2012-01-15
      • 2012-08-07
      • 1970-01-01
      相关资源
      最近更新 更多