By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

How to have non-ASCII text on Text widget (Tkinter)

P: 2
Hello all,

I am making a small GUI with tkinter. On it I have a Text widget, that should be able to accept input text from the user. The problem is that when the input text is not-ASCII (e.g. ) the program crashes with the error below.

Could anybody please help me to see how to solve it? Thank you.

Below I paste a simple code that can be used for testing, and the error that I get.

As you can see non-ASCII text can be inserted with the insert method, but as soon as it receives non-ASCII text from the user in the GUI, it crashes.

CODE-----
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from Tkinter import *
  4. import tkMessageBox
  5. import sys
  6.  
  7. class App(object):
  8.     def __init__(self,parent):
  9.  
  10. # Frame
  11.         fSize = 10
  12.         self.f = Frame(parent,width=fSize,height=fSize)
  13.         self.f.pack(padx=15,pady=15)
  14.  
  15.         self.tTale = Text(self.f, height=15, width=30, bg="grey")
  16.         self.tTale.pack()
  17.         self.tTale.insert(END, u'bl')
  18.  
  19. #exit button
  20.         Button(text="quit", command=quit) .pack(side=BOTTOM)
  21.  
  22. root = Tk()
  23. root.title('Test')
  24. root.call('encoding', 'system', 'utf-8')
  25. app = App(root)
  26.  
  27. root.mainloop()
  28.  
ERROR-----
2014-02-03 23:20:03.513 Python[82405:1107] An uncaught exception was raised
2014-02-03 23:20:03.524 Python[82405:1107] -[__NSCFConstantString characterAtIndex:]: Range or index out of bounds
2014-02-03 23:20:03.525 Python[82405:1107] (
0 CoreFoundation 0x00007fff8f59741c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8e1b4e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f5972cc +[NSException raise:format:] + 204
3 CoreFoundation 0x00007fff8f47ebcb -[__NSCFString characterAtIndex:] + 91
4 Tk 0x00000001083b9f5d TkpInitKeymapInfo + 825
5 Tk 0x00000001083bfecb Tk_MacOSXSetupTkNotifier + 861
6 Tcl 0x000000010829bccd Tcl_DoOneEvent + 311
7 _tkinter.so 0x00000001082194a3 init_tkinter + 4450
8 Python 0x0000000107f3414d PyEval_EvalFrameEx + 8080
9 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
10 Python 0x0000000107f388c8 _PyEval_SliceIndex + 929
11 Python 0x0000000107f354d4 PyEval_EvalFrameEx + 13079
12 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
13 Python 0x0000000107f31a24 PyEval_EvalCode + 54
14 Python 0x0000000107f50c2c PyParser_ASTFromFile + 306
15 Python 0x0000000107f50cd3 PyRun_FileExFlags + 137
16 Python 0x0000000107f50821 PyRun_SimpleFileExFlags + 718
17 Python 0x0000000107f61363 Py_Main + 2995
18 libdyld.dylib 0x00007fff8be895fd start + 1
)
2014-02-03 23:20:03.526 Python[82405:1107] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8f59741c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8e1b4e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f5972cc +[NSException raise:format:] + 204
3 CoreFoundation 0x00007fff8f47ebcb -[__NSCFString characterAtIndex:] + 91
4 Tk 0x00000001083b9f5d TkpInitKeymapInfo + 825
5 Tk 0x00000001083bfecb Tk_MacOSXSetupTkNotifier + 861
6 Tcl 0x000000010829bccd Tcl_DoOneEvent + 311
7 _tkinter.so 0x00000001082194a3 init_tkinter + 4450
8 Python 0x0000000107f3414d PyEval_EvalFrameEx + 8080
9 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
10 Python 0x0000000107f388c8 _PyEval_SliceIndex + 929
11 Python 0x0000000107f354d4 PyEval_EvalFrameEx + 13079
12 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
13 Python 0x0000000107f31a24 PyEval_EvalCode + 54
14 Python 0x0000000107f50c2c PyParser_ASTFromFile + 306
15 Python 0x0000000107f50cd3 PyRun_FileExFlags + 137
16 Python 0x0000000107f50821 PyRun_SimpleFileExFlags + 718
17 Python 0x0000000107f61363 Py_Main + 2995
18 libdyld.dylib 0x00007fff8be895fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
Feb 3 '14 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
It must be something specific about your platform or version of Tcl/Tk. I was able to insert ر without consequence.
Feb 4 '14 #2

Expert 100+
P: 618
I am guessing that your version of Tkinter/TCL/Tk is too old to handle this. It runs fine on my machine also. Without the "coding utf-8" line the error is
SyntaxError: Non-ASCII character '\xc3' in file
which is how it should be handled, i.e. Python errors before it ever gets to the Tkinter interface.

You can also try
self.tTale.insert(END, u'bl'.encode("utf-8"))
and see if that works. If memory serves, versions of TCL 8.4 and up handle unicode. To print the version:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     import Tkinter as tk     ## Python 2.x
  3. except ImportError:
  4.     import tkinter as tk     ## Python 3.x
  5. print tk.TclVersion, tk.TkVersion 
Feb 4 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.