1

如果我在 textctrl 中输入波罗的海字符并单击按钮test1我有一个错误

"InicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: 
                     ordinal not in range(128)"

按钮test2工作正常。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, (-1, -1), wx.Size(450, 300))

        self.panel = wx.Panel(self)

        self.input_area = wx.TextCtrl(self.panel, -1, '',(5,5),(200,200), style=wx.TE_MULTILINE)
        self.output_list = wx.ListCtrl(self.panel, -1, (210,5), (200,200), style=wx.LC_REPORT)
        self.output_list.InsertColumn(0, 'column')
        self.output_list.SetColumnWidth(0, 100)


        self.btn1 = wx.Button(self.panel, -1, 'test1', (5,220))
        self.btn1.Bind(wx.EVT_BUTTON, self.OnTest1)

        self.btn2 = wx.Button(self.panel, -1, 'test2', (100,220))
        self.btn2.Bind(wx.EVT_BUTTON, self.OnTest2)

        self.Centre()

    def OnTest1(self, event):
        self.output_list.InsertStringItem(0,str(self.input_area.GetValue()).decode('utf-8'))

    def OnTest2(self, event):
        self.output_list.InsertStringItem(0,"ąčęėįš".decode('utf-8'))

class MyApp(wx.App):
     def OnInit(self):
         frame = MyFrame(None, -1, 'encoding')
         frame.Show(True)
         return True

app = MyApp(0)
app.MainLoop()

更新 1

我已经在两台 Windows 7 Ultimate x64 计算机上尝试过此代码。

两者都有python 2.7wxPython2.8 win64 unicode for python 2.7

在两台机器上我都有同样的错误。

4

3 回答 3

0

替换 def OnTest1(self, event): self.output_list.InsertStringItem(0,str(self.input_area.GetValue()).decode('utf-8'))

def OnTest1(自我,事件):

    self.output_list.InsertStringItem(0,self.input_area.GetValue())
于 2010-11-20T03:34:08.003 回答
0

无法复制...如果我尝试使用瑞典语字符“åäö”,它似乎可以工作,在使用“ąčęėįš”语言环境问题时也是如此?

于 2010-11-16T10:10:13.733 回答
0

您使用的是 wxPython 的 unicode 版本吗?你没有提到你的平台和其他系统细节。

于 2010-11-16T10:22:35.267 回答