Question

Si j'entre les caractères baltes dans textctrl et cliquez sur le bouton test1 J'ai une erreur

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

Bouton test2 fonctionne très bien.

#!/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()

Mise à jour 1

J'ai essayé ce code sur deux ordinateurs Windows 7 Édition Intégrale x64.

Les deux ont python 2.7 et wxPython2.8 win64 unicode pour python 2.7

Dans les deux machines que j'ai la même erreur.

Était-ce utile?

La solution

Remplacer def OnTest1 (self, événement):         self.output_list.InsertStringItem (0, str (self.input_area.GetValue ()). décodage ( 'utf-8'))

avec

def OnTest1 (auto, événement):

    self.output_list.InsertStringItem(0,self.input_area.GetValue())

Autres conseils

ne peut pas reproduire ... Si je tente avec suédois caracters « ÅÄÖ » il semble fonctionner, également lors de l'utilisation « aceeiš » problème locale?

Utilisez-vous la construction unicode de wxPython? Vous ne l'avez pas mentionné votre plate-forme et d'autres informations sur le système.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top