在wxPython中使用unicode字符

2024-09-25 12:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图插入unicode字符时,wxpython和他的富文本控件有问题。。。\xb2打印顶点“2”,“\u2074”应打印顶点“4”…
编辑:我使用windows vista。。。我尝试了“编码cp1252”和“utf-8”,但结果是一样的…
2编辑: 在vista上它会崩溃,在xp上它会显示一个奇怪的正方形(我猜是在pc无法识别字符的时候…)

以下是源代码:

from __future__ import unicode_literals

import wx
import wx.richtext as rt

class Trial(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Prova', 
                size=(400, 400))

        self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER)
        wx.CallAfter(self.rtc.SetFocus)
        #self.rtc.Freeze()
        self.rtc.BeginFontSize(14)
        self.rtc.WriteText('hei!\xb2') #alright
        self.rtc.WriteText('hi\u2074!')#crash
        self.rtc.EndFontSize()
        
       
if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Trial()
    frame.Show()
    app.MainLoop()

但当我试着运行它时,它会崩溃。。。在

^{pr2}$

那么…我该怎么办?我真的需要展示。。。在

谢谢大家!!在


Tags: importself编辑initunicode字符framewx
2条回答

如果您想要Unicode支持,那么应该使用wxpython的Unicode版本。在

There are two versions of wxPython for each of the supported Python versions on Win32. They are nearly identical, except one of them has been compiled with support for the Unicode version of the platform APIs. Unless you've been told differently, you probably want to get the Unicode build of wxPython.

大多数其他平台也有两个版本。在

如果你传递实际的符号,例如

self.rtc.WriteText("hei!²")

如果忘记设置编码,有时会发生这种情况。把这个放在代码的顶部:

# -*- encoding: utf-8 -*-

在任何代码(包括注释)之前,但在shebang(#!/usr/bin/python)

相关问题 更多 >