UNew to Tkinter nicodeEncodeError:Japanese

  • Thread starter rajasekaran.natarajan
  • Start date
R

rajasekaran.natarajan

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
 
F

Fredrik Lundh

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.

chances are that Tkinter has already decoded the string for you.
def search(self):
word = self.entStr.get()
word_utf = word.decode('sjis')
print "Inside search ...", word_utf.encode('utf8')

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>
 
R

rajasekaran.natarajan

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]
def search(self):
word = self.entStr.get()
print repr(word)
print "Inside search ...", word.encode('utf8')

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
 
R

rajasekaran.natarajan

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]
def search(self):
word = self.entStr.get()
print repr(word)
print "Inside search ...", word.encode('utf8')

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top