wxPython ve Unicode Sorunu

wxPython kullanarak bir veri giriş kutusu ile bir buton hazırlayıp bu veri giriş kutusundan gelen veriyi hiç base64 ile kodlamaya çalıştınız mı ? Az önce Hatırlat'ın kullanıcı oluşturma ve kullanıcı giriş kısımları ile ilgilenirken aldım böyle bir hatayı.

Basit bir örnek yaptım. Aşağıdaki haliyle hata alıyorsunuz.

 
#-*- coding: utf-8 *-*
 
import wx
import base64
 
class Pencere(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(200, 150))
 
        panel = wx.Panel(self,-1)
 
        tasiyici = wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(tasiyici)
 
        self.veri = wx.TextCtrl(panel,-1)
        buton = wx.Button(panel,-1,"Tamam")
        buton.Bind(wx.EVT_BUTTON,self.tamam)
 
        tasiyici.Add(self.veri,0,wx.ALL | wx.EXPAND,5)
        tasiyici.Add(buton,0,wx.ALL | wx.EXPAND,5)
 
    def tamam(self,olay):
        print base64.encodestring(self.veri.GetValue())
 
class MyApp(wx.App):
    def OnInit(self):
        frame = Pencere(None, -1, 'menu1.py')
        frame.Show(True)
        return True
 
app = MyApp(0)
app.MainLoop()
 
 
Traceback (most recent call last):
  File "test.py", line 23, in tamam
    print base64.encodestring(self.veri.GetValue())
  File "/usr/lib/python2.5/base64.py", line 315, in encodestring
    pieces.append(binascii.b2a_base64(chunk))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 0: ordinal not in range(128)

Hata almamak içinse tamam fonksiyonunu aşağıdaki gibi yazmanız gerekiyor.

 
    def tamam(self,olay):
        print base64.encodestring("%s" %(self.veri.GetValue().encode('utf-8')))
 

Tags: ,

Leave a Reply