UNew to Tkinter nicodeEncodeError:Japanese

Discussion in 'Python' started by rajasekaran.natarajan, Dec 22, 2005.

  1. Hi friends,

    I am writing my Japanese study program and a GUIt.

    I am facing a problem in an entry widget where I enter Japanese words.
    The entered Japanese word is accessed by the callback function and used
    for some processes.

    It is working well when I enter english values.
    But giving unicodeEncodeError when I try with the Japanese letter.

    I tried to decode it by but I am getting unicodeEncodeError.

    the snippet is given below

    class showex:
    ....
    .......
    def make gui
    .....
    ....
    self.entStr = StringVar()
    self.entStr.set('Enter a Word Here')

    wordEntry = Entry(self.msgFrame,textvariable=self.entStr)
    #wordEntry.insert(0, 'Enter word Here')
    wordEntry.pack(side=RIGHT)
    wordEntry.focus()
    wordEntry.bind('<Return>',self.search)

    def previous(self):
    if (self.inc > 9):
    self.inc = self.inc -5
    print 'Previous Executed', self.inc

    def next(self):
    self.inc = self.inc + 5
    print 'Next Executed', self.inc

    def search(self):
    word = self.entStr.get()
    word_utf = word.decode('sjis')
    print "Inside search ...", word_utf.encode('utf8')



    I tried
    調ã¹ã‚‹ #printed the Japanese word correctly

    this is working nicely

    What am I doing wrong here

    I would greatly appreciate any help or suggestion.

    Regards

    raj
     
    rajasekaran.natarajan, Dec 22, 2005
    #1
    1. Advertisements

  2. chances are that Tkinter has already decoded the string for you.
    try replacing this with

    def search(self):
    word = self.entStr.get()
    print repr(word)
    print "Inside search ...", word.encode('utf8')

    and let us know what it prints.

    </F>
     
    Fredrik Lundh, Dec 22, 2005
    #2
    1. Advertisements

  3. Thanks a lot for your reply Mr.Lundh

    Yeah you are correct, Tkinter is automatically converting the words to
    unicode, so there is no need to decode again, also there is no need to
    encode in the print statement

    so, your assumption is correct but the encode statement at print gave
    me wrong kanjis [Japanese characters]
    when I remove encode it is printing properly.

    def search(self):
    word = self.entStr.get()
    print repr(word)
    print "Inside search ...", word

    I referred unicode characters printed by repr to confirm. It is
    perfect.

    Thanks again (also for making you book available in the net)

    -raj
     
    rajasekaran.natarajan, Dec 22, 2005
    #3
  4. Thanks a lot for your reply Mr.Lundh

    Yeah you are correct, Tkinter is automatically converting the words to
    unicode, so there is no need to decode again, also there is no need to
    encode in the print statement

    so, your assumption is correct but the encode statement at print gave
    me wrong kanjis [Japanese characters]
    when I remove encode it is printing properly.

    def search(self):
    word = self.entStr.get()
    print repr(word)
    print "Inside search ...", word

    I referred unicode characters printed by repr to confirm. It is
    perfect.

    Thanks again (also for making your book available in the net)

    -raj
     
    rajasekaran.natarajan, Dec 22, 2005
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.