如何将具有 unicode 字符的项目添加到 python 中的 QComboBox?

How can I add an item with unicode characters to a QComboBox in python?

我正在尝试将前缀单位添加到 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',则会打印出正确的字符。

我该如何解决这个问题?

我发现了问题。因为我从另一个函数复制粘贴了一些代码,所以我将字符类型转换为 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)')