Python 如何将UTF-8字符串从wx.TextCtrl传递到wx.ListCtrl
如果我在textctrl中输入波罗的语字符,然后单击按钮test1,我会出错Python 如何将UTF-8字符串从wx.TextCtrl传递到wx.ListCtrl,python,utf-8,wxpython,listctrl,textctrl,Python,Utf 8,Wxpython,Listctrl,Textctrl,如果我在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.Fr
"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
我已经在两台Windows7Ultimate x64计算机上尝试了此代码
它们都有Python2.7和wxPython2.8 win64 unicode用于Python2.7
在这两台机器中我都有相同的错误。无法复制。。。如果我尝试使用瑞典语字符“åäö”,它似乎会起作用,在使用“ąęėįš”语言环境问题时也是如此?您使用的是wxPython的unicode版本吗?您没有提到您的平台和其他系统详细信息。替换 def OnTest1(自身、事件): self.output\u list.InsertStringItem(0,str(self.input\u area.GetValue()).decode('utf-8')) 与 def OnTest1(自身、事件):
我使用的是wxPython2.8-win64-unicode-2.8.11.0-py27和python-2.7.amd64您的示例对我很有用。GTK--2.6.31-22-generic#68 Ubuntu SMP周二10月26日16:37:17 UTC 2010 x86_64 GNU/LinuxUbuntu 9.10,Python 2.6.4 In[4]:wx.version()Out[4]:'2.8.10.1(gtk2 unicode)'
self.output_list.InsertStringItem(0,self.input_area.GetValue())