如何将UTF-8字符串从wx.TextCtrl传递到wx.ListCtrl
如果我在 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.7 的 python 2.7 和 wxPython2.8 win64 unicode
在两台机器上我都有相同的错误。
If I enter Baltic characters in textctrl and click button test1 I have an error
"InicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
ordinal not in range(128)"
Button test2 works fine.
#!/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()
Update 1
I have tried this code on two Windows 7 Ultimate x64 computers.
Both have python 2.7 and wxPython2.8 win64 unicode for python 2.7
In both machines I have the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代替
def OnTest1(自身,事件):
self.output_list.InsertStringItem(0,str(self.input_area.GetValue()).decode('utf-8'))
与
def OnTest1(self, event):
Replace
def OnTest1(self, event):
self.output_list.InsertStringItem(0,str(self.input_area.GetValue()).decode('utf-8'))
with
def OnTest1(self, event):
无法重现...如果我尝试使用瑞典字符“åäö”,它似乎可以工作,在使用“ąčęėįš”语言环境问题时也是如此?
can't reproduce... If I try with swedish caracters "åäö" it seems to work, also when using "ąčęėįš" locale problem?
你使用的是 wxPython 的 unicode 版本吗?您没有提及您的平台和其他系统详细信息。
Are you using the unicode build of wxPython? You didn't mention your platform and other system details.