在 wxPython 中使用 unicode 字符

发布于 2024-09-26 01:44:09 字数 1962 浏览 2 评论 0原文

当我尝试插入 unicode 字符时,我对 wxpython 和他的富文本控件有问题... \xb2 打印顶点'2','\u2074' 应该打印顶点'4'...
编辑:我使用 Windows Vista...并且我尝试了“编码 cp1252”和“utf-8”,但结果相同...
2编辑: 在vista上它崩溃了,在xp上它显示了一个奇怪的方块(我猜这是当电脑无法识别该字符时...)

这是源代码:

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()

但是当我尝试运行它时,它崩溃了...

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    frame = display.Trial()
  File "C:\Users\expert\Desktop\display.py", line 15, in __init__
    self.rtc.WriteText('hi\u2074!')
  File "C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx\richtext.py", line 2507, in WriteText
    return _richtext.RichTextCtrl_WriteText(*args, **kwargs)
  File "C:\Python26\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2074' in position 4: character maps to <undefined>

所以。 ..我应该怎么办?我真的需要展示它...

谢谢大家!!

i have a problem with wxpython and his rich text control, when I try to insert unicode characters... \xb2 prints an apex '2', '\u2074' should print an apex '4'...
edit: I use windows vista... and I tried 'coding cp1252 ' and 'utf-8' but with the same result...
2edit:
on vista it crashs, on xp it shows a strange square (i guess it's when the pc doesn't recognize the character...)

here is the source code:

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()

but when I try to run it, it crashs...

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    frame = display.Trial()
  File "C:\Users\expert\Desktop\display.py", line 15, in __init__
    self.rtc.WriteText('hi\u2074!')
  File "C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx\richtext.py", line 2507, in WriteText
    return _richtext.RichTextCtrl_WriteText(*args, **kwargs)
  File "C:\Python26\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2074' in position 4: character maps to <undefined>

so...what should I do? I really need to show it...

thanks everybody!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失去的东西太少 2024-10-03 01:44:09

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

wxPython 有两个版本
每个受支持的 Python 版本
在 Win32 上。它们几乎一模一样,
除了其中之一已被编译
支持 Unicode 版本
平台API。除非你已经
被告知的方式不同,你可能
想要获得 Unicode 版本
wxPython。

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

如果您传递实际符号,则效果很好,例如

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

If you want Unicode support then you should be using the unicode version of wxpython.

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.

Most other platforms also have two versions.

It works fine if you pass the actual symbols e.g

self.rtc.WriteText("hei!²")
何以畏孤独 2024-10-03 01:44:09

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

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

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

This can sometimes occur if you forget to set your encoding. Put this at the top of the code:

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

before any code including comments but after the shebang (#!/usr/bin/python)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文