954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help needed for displaying extended charactors in Tk()

I have a problem is phasing the extended charactors in TK(). Any help would be apreciated.

a='itself ‘a parody’.'
>>> a "return"
'itself \x91a parody\x92.'

>>> def display(data):

root = Tk()
scrollbar = Scrollbar(root)
myTextWidget= Text(root,yscrollcommand=scrollbar.set)
myTextWidget.insert(0.0, data)
scrollbar.config(command=myTextWidget.yview)
scrollbar.pack(side=RIGHT, fill=Y)
myTextWidget.pack(side=LEFT, expand=0, fill=BOTH)


>>> display(a)

I ended up geting " itself ムa parodyメ." as my result and I need the result to be "itself ‘a parody’."

dav_yip
Newbie Poster
2 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Doesn't look like tcl knows how to handle the higher ascii characters properly, even with adding encoding strings to the code.

Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

Try a="itself ‘a parody’."

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Hi,

I ended up geting " itself ムa parodyメ." as my result and I need the result to be "itself ‘a parody’."

to get the actual characters you need to specify the character encoding. Make sure when use/deal with non-ascii character you specify the encoding type.

So label displaying the text you wanted...

# -*- coding: UTF-8 -*-

from Tkinter import *
root = Tk()
Label(root, text = "itself ‘a parody’.").pack()
root.mainloop()


There are many encodings that Python supports. I have used UTF-8(UNICODE Tranformation Format).

To learn/know more about this you can go through the following link. http://www.amk.ca/python/howto/unicode


kath.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

Thanks for the advice. It seems that I have to spend more time in encoding to get this to work.

The strange thing that I do not understand is when I do the below it is ok.

>>> text = "itself ‘a parody’."
>>> print text
itself ‘a parody’.

But when I display via Tkinter I will have a difference result like " itself ムa parodyメ.".

To me it is really strange that even a print statment can manage the characters but not via Tkinter...

dav_yip
Newbie Poster
2 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 
Try a="itself ‘a parody’."

The problem is that US keyboards don't have a left and right singlequote character, so you need to escape the hex value of the character. This works in Python ...

x = 'itself \x91a parody\x92.'
print x  # itself ‘a parody’.


If you feed string x to Tkinter's tcl language you get this --> "itself a parody."

In the USA at least it looks like you have to use the alt-key and keypad thingy and type 0145 and 0146 to construct your string. I do that with spanish characters.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks for the advice. It seems that I have to spend more time in encoding to get this to work.

The strange thing that I do not understand is when I do the below it is ok.

>>> text = "itself ‘a parody’." >>> print text itself ‘a parody’.

But when I display via Tkinter I will have a difference result like " itself ムa parodyメ.".

To me it is really strange that even a print statment can manage the characters but not via Tkinter...


Well even i am not quite sure about it. But Tkinter is a Python module for GUI. It is object-oriented layer on top of Tcl/Tk.

Tcl is a scripting language which comes with graphical extension called Tk. Through so called “bindings”, Tk can be used under other languages, such as Perl, Python, and Ruby.

And python installation packages available in forms, i.e, ansi and unicode. If you have used unicode format, then your IDE will print what you said above. Because it uses utf-8 as its default encoding.

So when used with Tkinter, undelying Tcl/Tk might interpret differently.

someone correct me if i am wrong somewhere..

thank you,
kath.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You